编写一C++程序。
1、定义基类CRole(角色),包含一个protected类型的数据成员char *name,有一个带参构造传递name,析构函数删除name。
2、由CRole类派生出3个子类: CPlane(飞机类)、CTank(坦克类)、子弹类(CBullet),用函数printInfo分别把三者的信息输出到屏幕上(用cout分别输出其类名+对象名即可,对象名在各自构造函数中设置)。
3、然后创建一个数组,该数组中要包括3架飞机、2部坦克,10颗子弹,
4、在main函数最后用printInfo输出所有的对象信息。
5、可以自行增加其他功能。
l 要求:先画出类图,再完成代码。
类图:
头文件(youxi.h)如下:
#pragma once
class CRole { //角色
protected:
char *name;//名字
char *type;//类型
float money;//金钱
public:
CRole(const char* na, const char* ty, float m);//构造函数,给属性赋值
virtual ~CRole();//析构函数
virtual void printInfo(); //打印输出
};
class CPlane :public CRole { //飞机
protected:
float fight_speed;//飞行速度
float fight_height;//飞行高度
public:
CPlane(const char* na, const char* ty, float m, float f_s, float f_h);//构造函数,给属性赋值
~CPlane();//析构函数
void printInfo();//打印输出
};
class CTank :public CRole { //坦克
protected:
float oil_mass;//载油量
int armoured;//装甲数量
public:
CTank(const char* na, const char* ty, float m, float oil_m, int arm);//构造函数,给属性赋值
~CTank();
void printInfo();//打印输出
};
class CBullet :public CRole { //子弹
protected:
float harm;//伤害
int cbullet_amount;//子弹数量
public:
CBullet(const char* na, const char* ty, float m, float ha, int cbu_amount);//构造函数,给属性赋值
~CBullet();//析构函数
void printInfo();//打印输出
};
main文件如下:
#include<iostream>
using namespace std;
#include "youxi.h"
#include<string.h>
CRole::CRole(const char* na, const char* ty, float m)
{
name = new char[strlen(na)+1];
type = new char[strlen(ty)+1];
strcpy(name, na);
strcpy(type, ty);
money = m;
}
CRole::~CRole()
{
delete []name;
delete[]type;
cout << "CRole 析构函数" << endl;
}
void CRole::printInfo()
{
cout << "类名:CRole" << " 对象名:" << name << " 类型:" << type << " 金钱:" << money << endl;
}
CPlane::CPlane(const char* na, const char* ty, float m, float f_s, float f_h) :CRole(na, ty, m)
{
fight_speed = f_s;
fight_height = f_h;
}
CPlane::~CPlane()
{
cout << "CPlane 类的析构函数" << endl;
}
void CPlane::printInfo()
{
cout << "类名:CPlane" << " 对象名:" << name << " 类型:" << type << " 金钱:"<< money
<< " 飞行速度" << fight_speed << " KM/s" << " 飞行高度:" << fight_height << " KM" << endl;
}
CTank::CTank(const char* na, const char* ty, float m, float oil_m, int arm) :CRole(na, ty, m)
{
oil_mass = oil_m;
armoured = arm;
}
CTank::~CTank()
{
cout << "CTank 类的析构函数" << endl;
}
void CTank::printInfo()
{
cout << "类名:CTank " << " 对象名:" << name << " 类型:" << type << " 金钱:"
<< money << " 载油量:" << oil_mass << " L" << " 装甲数量:" << armoured << endl;
}
CBullet::CBullet(const char* na, const char* ty, float m, float ha, int cbu_amount) :CRole(na, ty, m)
{
harm = ha;
cbullet_amount = cbu_amount;
}
CBullet::~CBullet()
{
cout << "CBullet类的析构函数" << endl;
}
void CBullet::printInfo()
{
cout << "类名:CBullet " << " 对象名:" << name << " 类型:" << type << " 金钱:"
<< money << " 伤害:" << harm << " 子弹数量:" << cbullet_amount << " 发" << endl;
}
void main()
{
CRole* C[15] = {
C[0] = new CPlane("1号飞机","战斗飞机",9000000.0,1000.0,6000),
C[1] = new CPlane("2号飞机","战斗飞机",100000000.0,1500.0,5000),
C[2] = new CPlane("3号飞机","运输飞机",500000.0,800.0,60000),
C[3] = new CTank("1号坦克","重型坦克",8000000.0,2000.0,10000000),
C[4] = new CTank("2号坦克","重型坦克",8000000.0,2000.0,10000000),
C[5] = new CBullet("1号子弹","普通弹",1000.0,200.0,1000),
C[6] = new CBullet("2号子弹","加强普通弹",1500.0,500.0,1000),
C[7] = new CBullet("3号子弹","散弹",3000.0,1000.0,1000),
C[8] = new CBullet("4号子弹","加强散弹",5000.0,1500.0,1000),
C[9] = new CBullet("5号子弹","穿甲弹",5000.0,3000.0,500),
C[10] = new CBullet("6号子弹","加强穿甲弹",7000.0,4000.0,400),
C[11] = new CBullet("7号子弹","普通燃烧弹",8000.0,3000.0,500),
C[12] = new CBullet("8号子弹","加强燃烧弹",10000.0,5000.0,500),
C[13] = new CBullet("9号子弹","追踪弹",10000.0,4000.0,500),
C[14] = new CBullet("10号子弹","加强追踪弹",15000.0,5000.0,500),
};
//打印输出
for (int i = 0; i < 15; i++)
{
C[i]->printInfo();
}
}
代码仅供参考