c++(类继承)示例[仅用于弱弱的博主巩固知识点用哦,不好勿喷]
2024-08-26 12:44:57
测试代码:
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 ;
}
运行结果:
最新文章
- 用RMAN备份EBS数据库的脚本
- SSH三大框架笔面试总结
- 基于 jQuery 实现垂直滑动的手风琴效果
- 【英语】Bingo口语笔记(6) - 表示“迷茫”
- jquery实现ajax,返回json数据
- 用生活例子来解释Java synchronized块
- 学习java随笔第二篇:java开发工具——Eclipse
- Mongodb数据库命令端经常使用操作
- krpano音量控制(我们已经转移到krpano中国网站 krpano360.com)
- redis缓存雪崩、缓存穿透、数据库和redis数据一致性
- Codeforces 461D. Appleman and Complicated Task 构造,计数
- Using 1-Wire device with Intel Galileo
- Hibernate的使用技巧②
- BZOJ 3483 SGU505 Prefixes and suffixes(字典树+可持久化线段树)
- 算法(第四版)学习笔记之java实现希尔排序
- IOS企业开发者帐号申请
- python-创建进程的三种方式
- java - day13 - ImplementDemo
- oracle数据结构
- SpringMVC_基本配置 --跟海涛学SpringMVC(和自己在项目中的实际使用的对比)