C++ 实验14 虚函数与多态

编写一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();
	}
}

代码仅供参考 

 

 

  • 5
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您介绍一下关于C++中多函数实验。 首先,多是指不同对象以不同的方式响应相同的消息的能力。在C++中,通过函数来实现多函数是指在基类中被声明为函数函数,在派生类中被重写后,在程序中使用基类指针或引用调用该函数时,会调用派生类中的函数。 下面是一个简单的多函数实验示例: ```c++ #include<iostream> using namespace std; class Shape { public: virtual void draw() { cout << "Drawing Shape" << endl; } }; class Circle: public Shape { public: void draw() { cout << "Drawing Circle" << endl; } }; class Square: public Shape { public: void draw() { cout << "Drawing Square" << endl; } }; int main() { Shape *s; Circle c; Square sq; s = &c; s->draw(); s = &sq; s->draw(); return 0; } ``` 运行结果为: ``` Drawing Circle Drawing Square ``` 在上述示例中,我们定义了一个基类Shape和两个派生类Circle和Square,分别重写了基类的函数draw()。在程序中,我们先定义了一个基类指针s,然后将其指向第一个派生类对象c,再调用s的函数draw(),此时会调用派生类Circle的draw()函数,因为指针实际上指向的是Circle类型的对象。接着,我们将指针s重新指向第二个派生类对象sq,再次调用s的函数draw(),此时会调用派生类Square的draw()函数。 通过这个实验,我们可以看到多函数的特性,即通过基类指针或引用调用函数时,会根据实际对象的类型来调用相应的派生类函数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值