实训7:虚函数与多态性

实训7:虚函数与多态性

参考代码 by 小喾苦
答案仅供参考!



第1关:坐标计算

实验目的

  1. 掌握运算符重载的方法;

实验任务

参考实验指导书第八章的实验任务 1。

实验步骤

参考实验指导书第八章的实验步骤 1。

测试说明

平台会对你编写的代码进行测试:

输入描述:空格隔开的两个整数x和y,表示一个坐标

输出描述:输出结果为5行,分别执行并输出p,p++,++p,p–,–p的结果

测试输入: 1 2

预期输出:

(1, 2)
(1, 2)
(3, 4)
(3, 4)
(1, 2)

C++参考代码

#include <iostream>
using namespace std;

class Point
{
	int _x, _y;
public:
	Point(int x=0, int y=0) : _x(x), _y(y) {}
	Point& operator++();
	Point operator++(int);
	Point& operator--();
	Point operator--(int);
	friend ostream& operator << (ostream& o, const Point& p);
};

/********** Begin **********/

Point& Point::operator++() {
    this->_x++;
    this->_y++;
    return *this;
}

Point Point::operator++(int) {
    Point temp(*this);
    this->_x++;
    this->_y++;
    return temp;
}

Point& Point::operator--() {
    this->_x--;
    this->_y--;
    return *this;
}

Point Point::operator--(int) {
    Point temp(*this);
    this->_x--;
    this->_y--;
    return temp;
}

/**********  End  **********/

ostream& operator << (ostream& o, const Point& p) {
	o << '(' << p._x << ", " << p._y << ')';
	return o;
}

int main()
{
	int x,y;
	cin>>x>>y;
	Point p(x, y);
	cout << p << endl;
	cout << p++ << endl;
	cout << ++p << endl;
	cout << p-- << endl;
	cout << --p << endl;
	return 0;
}

第2关:摩托车类

实验目的

  1. 学习使用虚函数实现动态多态性。

实验任务

参考实验指导书第八章的实验任务2。

实验步骤

参考实验指导书第八章的实验步骤2。

测试说明

输出把基类中 Run、Stop 声明为虚函数时主程序的输出结果

平台会对你编写的代码进行测试:

测试输入:无
预期输出:

vehicle run!
vehicle stop!
bicycle run!
bicycle stop!
motocar run!
motocar stop!
motocycle run!
motocycle stop!
vehicle run!
vehicle stop!
bicycle run!
bicycle stop!
motocar run!
motocar stop!
motocycle run!
motocycle stop!

C++参考代码

#include <iostream>
using namespace std;


/********** Begin **********/

class Vehicle {
public:
    virtual void Run() {cout << "vehicle run!" << endl;}
	virtual void Stop() {cout << "vehicle stop!" << endl;}
};

class Bicycle : virtual public Vehicle {
public:
    virtual void Run() {cout << "bicycle run!" << endl;}
	virtual void Stop() {cout << "bicycle stop!" << endl;}
};

class Motorcar : virtual public Vehicle {
public:
    virtual void Run() {cout << "motocar run!" << endl;}
	virtual void Stop() {cout << "motocar stop!" << endl;}
};

/**********  End  **********/

class Motorcycle : public Bicycle, public Motorcar
{
public:
	void Run() {cout << "motocycle run!" << endl;}
	void Stop() {cout << "motocycle stop!" << endl;}
};

int main()
{
	Vehicle v;
	v.Run();
	v.Stop();
	Bicycle b;
	b.Run();
	b.Stop();
	Motorcar m;
	m.Run();
	m.Stop();
	Motorcycle mc;
	mc.Run();
	mc.Stop();
	Vehicle* vp = &v;
	vp->Run();
	vp->Stop();
	vp = &b;
	vp->Run();
	vp->Stop();
	vp = &m;
	vp->Run();
	vp->Stop();
	vp = &mc;
	vp->Run();
	vp->Stop();
	return 0;
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值