c++(类继承)示例[仅用于弱弱的博主巩固知识点用哦,不好勿喷]
测试代码:
Animals.h:
#pragma once
#include<string>
class Animals
{
protected:
std::string Food;
int Weight;
int Intake;
double Velocity;
public:
Animals(const std::string &fd="none",int w=,int n=,double v=0.0);
virtual void Speaking();
void Swimming();
void show(int)const;
virtual ~Animals();
}; class Fish :Animals
{
private:
std::string Colour;
public:
Fish(const std::string &cr = "none", const std::string &fd = "none", int w = , int n = , double v = 0.0);
Fish(const std::string &cr, Animals &tp);
void Appear()const;
void show()const;
void Speaking(); };
Animals.cpp:
#include "stdafx.h"
#include "Animals.h"
#include<iostream> Animals::Animals(const std::string &fd, int w, int n,double v):Food(fd),Weight(w),Intake(n), Velocity(v)
{} void Animals::Speaking()
{
std::cout << "......" << std::endl;
} void Animals::Swimming()
{
std::cout <<"++++++"<< Velocity << "m/s" <<"++++++"<< std::endl;
} void Animals::show(int p) const
{
std::cout << "I want eatting ";
for (int i = ; i < p; i++)
std::cout << Food<<"! ";
std::cout << std::endl;
} Animals::~Animals()
{
} Fish::Fish(const std::string & cr, const std::string & fd, int w, int n, double v) :Animals(fd,w,n,v)
{
Colour = cr;
} Fish::Fish(const std::string & cr, Animals & tp):Animals(tp),Colour(cr)
{} void Fish::Speaking()
{
std::cout << "Booo Booo!" << std::endl;
} void Fish::Appear() const
{
std::cout << Colour << std::endl;
} void Fish::show() const
{
std::cout << "Food: " << Food << std::endl;
std::cout << "Weight: " << Weight << std::endl;
std::cout << "Intake: " << Intake << std::endl;
std::cout << "Velocity: " << Velocity <<" m/s"<< std::endl;
std::cout << "Colour: " << Colour << std::endl;
}
ConsoleApplication.cpp:
#include "stdafx.h"
#include "Animals.h"
#include<iostream>
using namespace std;
int main()
{
Animals p0("shrimp", , , 1.0);
Fish b0("Red and White", "Coral", , , 0.5);
p0.show();
p0.Speaking();
p0.Swimming();
cout << endl ;
b0.show();
cout << "Now I must speaking: ";
b0.Speaking();
cout << endl ;
Fish b1("Blue", p0);
b1.show();
cout << "Now I must speaking: ";
b1.Speaking();
return ;
}
运行结果:

c++(类继承)示例[仅用于弱弱的博主巩固知识点用哦,不好勿喷]的更多相关文章
- C++类继承示例
C++的子类与孙子类都实现了虚函数时,孙子类的实现会覆盖掉子类的实现. 继承的最主要的应用就是把不同的类放到一个数组中,然后遍历调用同名函数. 实例如下: #include <iostream& ...
- java新手笔记14 类继承示例
1.Person package com.yfs.javase; public class Person { private String name; private int age; private ...
- c++中的类(class)-----笔记(类继承)
1,派生类继承了基类的所有成员函数和数据成员(构造函数.析构函数和操作符重载函数外). 2,当不指明继承方式时,默认为私有继承. 3,基类的私有成员仅在基类中可见,在派生类中是不可见的.基类的私有成员 ...
- C++基础——类继承
一.前言 好吧,本系列博客已经变成了<C++ Primer Plus>的读书笔记,尴尬.在使用C语言时,多通过添加库函数的方式实现代码重用,但有一个弊端就是原来写好的代码并不完全适用于现 ...
- 项目里出现两个配置类继承WebMvcConfigurationSupport时,为什么只有一个会生效(源码分析)
为什么我们的项目里出现两个配置类继承WebMvcConfigurationSupport时,只有一个会生效.我在网上找了半天都是说结果的,没有人分析源码到底是为啥,博主准备讲解一下,希望可以帮到大家! ...
- C++——类继承
类库:类库由类声明和实现构成.类组合了数据表示和类方法,因此提供了比函数库更加完整的程序包. 类继承:从已有的类派生出新的类,派生类继承了原有类(称为基类)的特征,包括方法. 通过类继承可以完成的工作 ...
- C++基础——类继承中方法重载
一.前言 在上一篇C++基础博文中讨论了C++最基本的代码重用特性——类继承,派生类可以在继承基类元素的同时,添加新的成员和方法.但是没有考虑一种情况:派生类继承下来的方法的实现细节并不一定适合派生类 ...
- iOS学习——iOS 整体框架及类继承框架图
整理自:IOS 整体框架类图值得收藏 一 整体框架 在iOS开发过程中,对iOS的整理框架的了解和学习是必不可少的一个环节,今天我们就好好来了解一下iOS的整体框架.首先贴一个关于iOS的框架介绍:i ...
- JavaScript中的类继承
JavaScript是一个无class的面向对象语言,它使用原型继承而非类继承.这会让那些使用传统面向对象语言如C++和Java的程序员们感到困惑.正如我们所看到的,JavaScript的原型继承比类 ...
随机推荐
- Servlet生命周期与线程安全
上一篇介绍了Servlet初始化,以及如何处理HTTP请求,实际上在这两个过程中,都伴随着Servlet的生命周期,都是Servlet生命周期的一部分.同时,由于Tomcat容器默认是采用单实例多线程 ...
- octave简易操作
语言以分号;结尾if for while等语句后用,来承接关系if ,elseif ,else ,end;for i=1:10,end;while a>3 ,end; while true, ...
- 【WPF】创建基于模板的WPF控件(经典)
原文:[WPF]创建基于模板的WPF控件(经典) WPF可以创建两种控件,它们的名字也很容易让人混淆:用户控件(User Control)和定制控件(Customer Control),之所以如此命名 ...
- Uber:中国市场两年内不考虑盈利,每年补贴10亿美金
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- LeetCode:14. Longest Commen Prefix(Easy)
1. 原题链接 https://leetcode.com/problems/longest-common-prefix/description/ 2. 题目要求 给定一个字符串数组,让你求出该数组中所 ...
- javascript对象转为字符串
function getStringTime(time){ //年 year = time.getFullYear(); //月 month = time.getMonth() if(String(m ...
- 使用LinqToExcel和EPPlus操作excel
1.使用LinqToExcel LinqToExcel是一个.net framework平台下开源项目,它主要实现了LINQ的语法查询Excel电子表格.类型之前的LINQToXXX如果你是LINQ语 ...
- Python连接符的种类和使用区别
python的连接符主要有 加号(+).逗号(,).空格( ) .反斜线(\).join()的方式. 加号(+),demo如下: #注意,+只能连接字符串,如果一个是字符串一个是数字就会报错 pr ...
- ActiveRecord-连接多张表之单表继承
ActiveRecord-连接多张表之单表继承 1. 基本概念 Rails提供了两种机制,可以将复杂的面向对象模型映射为关系模型,即所谓的单表继承(single-table inheritance)和 ...
- c++调用Python基础功能
c++调用Python首先安装Python,以win7为例,Python路径为:c:\Python35\,通过mingw编译c++代码.编写makefile文件,首先要添加包含路径:inc_path ...