多态性(基础实验探究报告)(实验二)

接着奏乐接着舞,直接内容展示。

实验二内容展示

图一 实验二内容

目录

实验二内容展示

实验任务1.(运算符重载,自增自减)

实验任务2.(虚函数的使用背景)

实验任务3.(赋值与相等运算符重载)


实验任务1.(运算符重载,自增自减)

1. 声明Point类,有坐标_x,_y两个成员变量。对Point类重载”++”(自增)、”--”(自减)运算符,实现对坐标值的改变。

上代码:

class Point{
		int _x;
		int _y;
	public:
		Point(int x=0,int y=0) :_x(x),_y(y){

		}
		~Point() {

		}
		Point& operator ++(){
			cout<<"调用前置++\n";
			this->_x++;
			return *this;
		}
		Point operator ++(int){
			cout<<"调用后置++\n";
			Point tmp=++*this;
			return tmp;
		}
		Point& operator --(){
			cout<<"调用前置--\n";
			this->_x++;
			return *this;
		}
		Point operator --(int){
			cout<<"调用后置--\n";
			Point tmp=--*this;
			return tmp;
		}		
		void SexPointposition(int x,int y) {
			this->_x=x;
			this->_y=y;
		}
		
		
};

总的来说,前置自增不用传入参数(从结构上看),返回值应该是一个引用,才能能进行赋值,后置自增一般实现上依赖于前自增,末尾省略了一个‘0’,所以要传入一个int参数。后面有对这段代码进行测试。

实验任务2.(虚函数的使用背景)

2. 声明一个车(vehicle)基类,具有Run、Stop等成员函数。由此派生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。在main()函数中声明vehicle、bicycle、motorcar、motorcycle的对象,调用其Run()、Stop()函数,观察其执行情况。再分别用vehicle类型的指针来调用这几个对象的成员函数,看看能否成功;把Run、Stop声明为虚函数,再试试看。

这里就比较偷懒,run我直接自增一下坐标hhhhh

(不虚函数直接调用肯定是不行的,已验证)

#include <iostream>
#include <typeinfo>
using namespace std;
/*
3. 声明一个车(vehicle)基类,具有MaxSpeed、Weight等成员变量,Run、Stop等成员函数,
由此派生出自行车(bicycle)类、汽车(motorcar)类。
自行车(bicycle)类有高度(Height)等属性,汽车(motorcar)类有座位数(SeatNum)等属性。
从bicycle和motorcar派生出摩托车(motorcycle)类,
在继承过程中,注意把vehicle设置为虚基类。
如果不把vehicle设置为虚基类,会出现什么问题。通过编程说明。
*/

class Point{
		int _x;
		int _y;
	public:
		Point(int x=0,int y=0) :_x(x),_y(y){

		}
		~Point() {

		}
		Point& operator ++(){
			cout<<"调用前置++\n";
			this->_x++;
			return *this;
		}
		Point operator ++(int){
			cout<<"调用后置++\n";
			Point tmp=++*this;
			return tmp;
		}
		Point& operator --(){
			cout<<"调用前置--\n";
			this->_x++;
			return *this;
		}
		Point operator --(int){
			cout<<"调用后置--\n";
			Point tmp=--*this;
			return tmp;
		}		
		void SexPointposition(int x,int y) {
			this->_x=x;
			this->_y=y;
		}
		
		
};

class vehicle {
	public:
		int MaxSpeed;
		int Weight;
		Point p;
		
		vehicle() {		
		}
		~vehicle() {
		}
		virtual void Run() {
			cout<<"Basic typeid:"<<typeid(*this).name()<<"Run"<<endl;
			p++;
		}
		virtual void Stop() {
			p--;		
		}		
};

class bicycle
:public virtual vehicle 
{
	public:
		float Height;
		bicycle() {
		}
		~bicycle() {
		}
		void Run() {
			cout<<typeid(*this).name()<<"Run"<<endl;
			p++;
		}		
};

class motorcar
:public  virtual vehicle 
{
	public:
		int SeatNum;
		motorcar() {
		}
		~motorcar() {
		}
	    void Run() {
	        cout<<typeid(*this).name()<<"Run"<<endl;	
			p++;
		}		
};

class motorcycle
:public bicycle,
public motorcar 
{
	public:
		motorcycle() {
		}
		~motorcycle() {
		}
		void Run() {
			cout<<typeid(*this).name()<<"Run"<<endl;
			p++;
		}
};

typedef vehicle* vp;

int main()
{
	vehicle* vr = nullptr;
	vehicle* v1=new vehicle();
	bicycle* bi1=new bicycle();
	motorcar* mot1=new motorcar();
	motorcycle* mc1=new motorcycle();
	vp arr[4]={v1,bi1,mot1,mc1};
	for(int i=0;i<4;i++){
		vr=arr[i];
		vr->Run();
	}
	
}

下面是运行结果:

图二 运行结果

这里可以看出前面自增运算写的正确,这里还浅浅用一下基类指针,给他整个数组(都虚函数了怎么还不用虚的玩玩)(叉腰.jpg)每种调用run都能正确的给出相应的交通工具,如果直接写同名的话是作用域的问题了,子类中新写的内容将原来基类继承来的东西覆盖掉,而采用虚函数的写法才能动态绑定,才能用指针形式去访问到相应的函数进行调用(这里测试函数应该再写些其他内容的,便于更好理解)

实验任务3.(赋值与相等运算符重载)

3. 对实验一任务4中的people类重载”==”运算符和”=”运算符,其中”==”运算符判断两个people类对象的id属性大小,”=”运算符实现people类对象的赋值操作。

class civil {
public:
	string name;
	int num;
	string identifiednumber;
	Sextype sex;
	Date birth;

public:
	civil(string name="", int num=0, string identifiednumber="", Sextype sex=Ambigous) :name(name), num(num), identifiednumber(identifiednumber), sex(sex) {

	}
	~civil() {

	}
	void printinfo() {
		cout << "name" << name << endl<<
			"num" << num << endl<<
			"identifiednumbed" << identifiednumber <<endl<<
			"sex" << sex<<endl;
		birth.formatPrint();
		
	}

	bool operator ==(const civil& other) {
		return this->identifiednumber == other.identifiednumber;
	}
	civil& operator=(const civil& other) {
		if (this != &other) { // 自赋值检查

			this->identifiednumber = other.identifiednumber;
			this->name = other.name;
			this->num = other.num;
			this->birth = other.birth;
			this->sex = other.sex;
		}
		return *this;
	}

};

自己给自己赋值会形成类似于死循环的东西,所以要进行检查,如果不是才准许修改。

这个就真没啥心得体会了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值