一、编写动物类animal,受保护数据成员name(名称、,string),age(年龄,int),公有函数成员void show(),输出“Animal,名称,年龄";公有派生鱼类fish加兽类beast,鱼类增加受保护数据
成员velocity(速度,int),公有函数成员void show(),输出“Fish,名称,年龄,速度";兽类增加受保护数据成员aPpetite(食量,int),公有函数成员void show(),输出"Beast,名称,年龄,食量";鱼类和兽类再公有两栖动物类amphibious,无添加数据成员,有公有函数成员voi d show(),输出“Fish,名称,年龄,速度"(第一行),"Beast,名称,年龄,食量“(第二行)
二、流程图
三、源代码。
Animal.h
#pragma once
#include <string>
#include <iostream>
using namespace std;
class animal
{
protected:
string name;
int age;
public:
animal();
animal(string name, int age);
void setn();
void seta();
~animal();
void show();
};
Fish.h
#pragma once
#include "animal.h"
class fish : virtual public animal
{
protected:
int velocity;
public:
fish();
fish(string nam, int ag, int v);
~fish();
void show();
void setv();
};
Beast.h
#pragma once
#include "animal.h"
class beast : virtual public animal
{
public:
beast();
~beast();
void show();
beast(string n, int a, int ap);
void setap();
protected:
int appetite;
};
Amphibious.h
#pragma once
#include "beast.h"
#include "fish.h"
class amphibious : virtual public fish, virtual public beast
{
public:
amphibious();
amphibious(string n, int a, int ap, int v);
~amphibious();
void show();
};
Animal.cpp
#include "animal.h"
animal::animal()
{
}
void animal::show()
{
cout << "Animal " << name << ", " << age << endl;
}
animal::animal(string nam, int ag) :name(nam), age(ag)
{
}
animal::~animal()
{
}
void animal::setn()
{
cin >> name;
}
void animal::seta()
{
cin >> age;
}
Fish’.cpp
#include "fish.h"
fish::fish()
{
}
fish::fish(string nam, int ag, int v) :animal(nam, ag), velocity(v)
{
}
void fish::show()
{
cout << "Fish," << name << " " << age << ", " << velocity << endl;
}
void fish::setv()
{
cin >> velocity;
}
fish::~fish()
{
}
Beast.cpp
#include "beast.h"
beast::beast()
{
}
void beast::show()
{
cout << "Beast," << name << " " << age << ", " << appetite<<endl;
}
beast::beast(string n, int a, int ap) :animal(n, a), appetite(ap){}
beast::~beast()
{
}
void beast::setap()
{
cin >> appetite;
}
Amphibious.cpp
#include "amphibious.h"
amphibious::amphibious()
{
}
void amphibious::show()
{
cout << "Fish " << name << " " << age << " " << velocity << endl;
cout << "Beast " << name << " " << age << " " << appetite << endl;
}
amphibious::amphibious(string n, int a, int ap, int v) :fish(n, a, v), beast(n,a,ap)
{
}
amphibious::~amphibious()
{
}
源:
#include <iostream>
#include <string>
#include<iostream>
#include "amphibious.h"
using namespace std;
int main()
{
fish a;
beast b;
a.setn();
b.setn();
a.seta();
b.seta();
a.setv();
b.setap();
a.show();
b.show();
}
四、代码实现。
标签:应用,show,int,void,beast,animal,基类,fish From: https://www.cnblogs.com/luoqingci/p/17327758.html