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的原型继承比类 ...
随机推荐
- EF更新时出错,An error occurred while updating the entries. See the inner exception for details
在使用EF进行更新数据时出错,报出的异常是 "An error occurred while updating the entries. See the inner excep ...
- iOS中如何根据UIView获取所在的UIViewController
原理 Responder Chain 事件的响应者链 大概的传递规则就是从视图顶层的UIView向下到UIViewController再到RootViewController再到Window最后到Ap ...
- html5特效库
Swiper是纯javascript打造的滑动特效插件,面向手机.平板电脑等移动终端.能实现触屏焦点图.触屏Tab切换.触屏多图切换等常用效果. delaunay.js是一款能在图片中形成无数个三角形 ...
- win10 java环境变量配置
首先,你应该已经安装了 Java 的 JDK 了(如果没有安装JDK,请跳转到此网址:http://www.oracle.com/technetwork/java/javase/downloads/i ...
- 第一篇 Python安装与环境变量的配置
开发语言有很多种,为什么选Python? 先对各种开发语言做个初识和分类如下:高级语言:Python Java.PHP C# Go ruby C++... ---> 字节码低级语言:C.汇编 - ...
- redux使用过程中遇到的两个致命的关键点
一.在reducer中,返回的state必须是全新的对象,否则,redux不会执行listening方法,因为redux会认为state没有更新过,没必要重新渲染view. 出现问题的例子: cons ...
- Java中的while(true)
while(true)是一个无限循环 在内部用break或return退出循环,否则一直循环
- lintcode-91-最小调整代价
91-最小调整代价 给一个整数数组,调整每个数的大小,使得相邻的两个数的差不大于一个给定的整数target,调整每个数的代价为调整前后的差的绝对值,求调整代价之和最小是多少. 注意事项 你可以假设数组 ...
- Linux之JDK在线安装及配置
1.查找java相关得列表 yum -y list java*2.在线安装 yum -y install java-1.6.0-openjdk*3.查看安装目录 ls -l /usr/lib/jv ...
- 【PHP】- session_cache_limiter(private,must-revalidate)是什么意思
session_cache_limiter(private,must-revalidate)是什么意思 表义一: 指定会话页面所使用的缓冲控制方法: 当session_cache_limiter('p ...