【面向对象程序设计】运算符重载与多态——由基类CRole派生出3个子类: CPlane(飞机类)、CTank(坦克类)、子弹类(CBullet),分别把三者的信息输出到屏幕上

一、实验内容

 (1)声明Point类,有坐标_x,_y两个成员变量;对Point类重载“++(自增)”、“——(自减)”运算符,实现对坐标值的改变。
 (2)定义基类CRole(角色),包含一个protected类型的数据成员char *name,有一个带参构造传递name,析构函数删除name。由CRole类派生出3个子类: CPlane(飞机类)、CTank(坦克类)、子弹类(CBullet),用函数printInfo分别把三者的信息输出到屏幕上(用cout分别输出其类名+对象名即可,对象名在各自构造函数中设置)。然后创建一个链表,该链表中要包括3架飞机、2部坦克,10颗子弹。在main函数最后用printInfo输出所有的对象信息。其中printInfo在基类为虚函数,可以自行增加其他功能。

二、代码

#include<iostream>
using namespace std;
class point
{
public:
	point(int a, int b) { x = a; y = b; }  //构造函数
	int getx()const { return x; }  //用const约束参数
	int gety()const { return y; }
	point& operator ++(int);    //后置自增
	point& operator --(int);    //后置自减
	point& operator ++();      //前置自增
	point& operator --();      //前置自减
	void print();    //输出坐标
	protected:
	int x;
	int y;
};
point& point::operator ++(int)
{
	x++;
	y++;
	return *this;     //返回调用函数对象
}
point& point::operator --(int)
{
	x--;
	y--;
	return *this;
}
point& point::operator ++()
{
	++x;
	++y;
	return *this;
}
point& point::operator --()
{
	--x;
	--y;
	return *this;
}
void point::print()
{
	cout << "("<<x <<","<< y <<")"<< endl;
}
int main()
{
	point obj(6,8);
	cout << "原坐标:" << "(" << obj.getx() << "," << obj.gety() << ")" << endl;
	cout << "后置自增:";
	obj++;
	obj.print();
	cout << "后置自减:";
	obj--;
	obj.print();
	cout << "前置自增:";
	++obj;
	obj.print();
	cout << "前置自减:";
	--obj;
	obj.print();
	return 0;
}
#include<iostream>
#include<string>
using namespace std;
class crole
{
protected:
	char* name;  //名字
	char* type;    //型号
public:
	crole(const char* na,const char*ty);   //构造函数
	virtual ~crole();    //虚析构函数
	virtual void printinfo();
};
class cplane :public crole  //飞机类
{
public:
	cplane(const char* na, const char* ty);
	~cplane();
	void printinfo();//打印输出
};
class ctank :public crole  //坦克类
{
public:
	ctank(const char* na, const char* ty);
	~ctank();
	void printinfo();//打印输出
};
class cbullet :public crole   //子弹类
{
protected:
	float distance;   //子弹射程
public:
	cbullet(const char* na, const char* ty, float di);
	~cbullet();
	void printinfo();//打印输出
};

crole::crole(const char* na, const char* ty)
{
	name = new char[strlen(na) + 1];
	type = new char[strlen(ty) + 1];
}
crole::~crole()
{
	delete[]name;
	delete[]type;
	cout << "crole 析构函数" << endl;
}
void crole::printinfo()
{
	cout << "类名:crole" << "对象名:" << name << "型号:" << type << endl;
}


cplane::cplane(const char* na, const char* ty) :crole(na, ty){}
cplane::~cplane()
{
	cout << "cplane 类的析构函数" << endl;
}
void cplane::printinfo()
{
	cout << "类名:cplane" << "	对象名:" << name << "	型号:" << type << endl;
}


ctank::ctank(const char* na, const char* ty) :crole(na, ty) {}
ctank::~ctank()
{
	cout << "ctank 类的析构函数" << endl;
}
void ctank::printinfo()
{
	cout << "类名:ctank  " << "	对象名:" << name << "	型号:" << type << endl;
}


cbullet::cbullet(const char* na, const char* ty,float di) :crole(na, ty) 
{
	float diatance = di;
}
cbullet::~cbullet()
{
	cout << "cbullet类的析构函数" << endl;
}
void cbullet::printinfo()
{
	cout << "类名:cbullet  " << "	对象名:" << name << "	型号:" << type << "    射程: "<<distance<<endl;
}
int main()
{
	crole* arr[15] = {
	arr[0] = new cplane("1号飞机","直升机"),
	arr[1] = new cplane("2号飞机","战斗飞机"),
	arr[2] = new cplane("3号飞机","空中加油机"),
	arr[3] = new ctank("1号坦克","t-55坦克"),
	arr[4] = new ctank("2号坦克","t-72a"),
	arr[5] = new cbullet("1号子弹","普通弹",300.0),
	arr[6] = new cbullet("2号子弹","达姆弹",2000.0),
	arr[7] = new cbullet("3号子弹","超大口径子弹",1000.0),
	arr[8] = new cbullet("4号子弹","加强散弹",500.0),
	arr[9] = new cbullet("5号子弹","空尖弹击",350.0),
	arr[10] = new cbullet("6号子弹","加强穿甲弹",1200.0),
	arr[11] = new cbullet("7号子弹","霰弹",400.0),
	arr[12] = new cbullet("8号子弹","加强燃烧弹",1800.0),
	arr[13] = new cbullet("9号子弹","追踪弹",1400.0),
	arr[14] = new cbullet("10号子弹","双弹头弹",800.0),
	};
	for (int i = 0; i < 15; i++)
	{
		arr[i]->printinfo();
	}
	return 0;
	
}

三、运行结果

在这里插入图片描述
在这里插入图片描述

四、总结

  首先是了解最常见的自增和自减运算符的特点和应用。声明一个point类,通过此类进行自增和自减运算符的重载来实现对坐标值的改变。其中尤其需要注意自增和自减运算符有前置和后置两种形式,前置形式重载为一元运算符函数,后置形式重载为二元运算符函数,每个重载运算符的函数都必须有明确的特征,使编译器确定要使用的版本。
  在第二小问中要求由一个基类派生出三个子类—飞机类、坦克类、子弹类,并且存储和输出各类的信息,我创建的代码包括飞机和坦克的名字和型号以及子弹的型号和射程。根据题意要在基类中拥有虚函数,这是支持多态性的前提,那么它的析构函数也为虚析构函数,虚析构函数使得用delete运算符删除对象时,系统可以正确的调用析构函数。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会举重的薯片

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值