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的原型继承比类 ...
随机推荐
- POJ-2421-Constructing Roads(最小生成树 普利姆)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26694 Accepted: 11720 Description The ...
- 分支push不上去的问题
还原一下现场,我在自己的项目里面,从master里面checkout的一个分支,当我在我这个分支里面进行 push代码的操作,我突然发现我的代码不能执行push的操作,如图 这个原因是由于远端的仓库没 ...
- 局域网访问不到linux下的tomcat
问题描述: CentOS安装完成Tomcat后,访问本地:http://localhost:8080/正确.但局域网内无法访问,而且服务器可ping通 经查原因为防火墙开启: [root@localh ...
- python eval()函数的妙用和滥用
eval()函数十分强大,官方demo解释为:将字符串str当成有效的表达式来求值并返回计算结果: >>> s='8*8' >>> eval(s) 64 >& ...
- 【转】odoo11新功能及绿色版汇总
昆山-Jeffery 11:34:00 ,odoo11 新功能: 评论:看到截图,感觉美工上又有所提高 官方的发布说明:https://www.odoo.com/nl_NL/page/odoo-11- ...
- Mysql综合练习作业50题
#作业库create database db8 charset utf8; #年级表create table class_grade(gid int not null primary key auto ...
- KMP python实现
首先去 https://blog.csdn.net/starstar1992/article/details/54913261/ 这里看下思想: 然后代码实现,一定要多调试几遍方能看懂: def ge ...
- nexys4-DDR开发板温度传感器ADT7420U
1. 用这个板子做个什么功能来学习?板子上有个温度传感器,看下官方是否有例程,板子售价1780元,相当的贵,下面是I2C接口, 看下芯片的引脚图 2. 资料下载地址,得注册账号 https://ref ...
- 【题解搬运】PAT_A1020 树的遍历
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- App测试基本流程详解
1 APP测试基本流程 1.1流程图 1.2测试周期 测试周期可按项目的开发周期来确定测试时间,一般测试时间为两三周(即15个工作日),根据项目情况以及版本质量可适当缩短或延长测试时间. 1.3测试资 ...