北京化工大学面向对象实验6

实验 6 虚函数与多态性
6.3 实验内容
(1) 下面的 shape 类是一个表示形状的抽象类, area() 为求图形面积的函数。从
shape 类派生三角形类和圆类,并实现求面积函数:
class shape{
public:
virtual double area()=0;
};
#define pi acos(-1)
class Shape {
public:
	virtual double area() = 0;
};
class Round:virtual public Shape
{
public:
	Round(int rr = 0)
		:r(rr)
	{}
	virtual double area()//重写虚函数
	{
		return pi * pow(r, 2);
	}
private:
	int r;
};
class Triangle :virtual public Shape
{
public:
	Triangle(int aa=0,int bb=0,int cc=0)
		:a(aa),b(bb),c(cc)
	{}
	virtual double area()
	{
		double p = (a + b + c) / 2.0;
		return sqrt(p * (p - a) * (p - b) * (p - c));
	}
private:
	double a, b, c;
};

(2) 利用虚函数实现的多态性来求四种几何图形的面积之和。这四种几何图形是:
三角形 (Triangle) 、矩形 (Rectangle) 、正方形 (Square) 和圆 (Circle) 。几何图
形的类型可以通过构造函数或通过成员函数来设置。
#define pi acos(-1)
class Shape {
public:
	virtual double area() = 0;
};
class Circle :virtual public Shape
{
public:
	Circle(int rr = 0)
		:r(rr)
	{}
	virtual double area()//重写虚函数
	{
		return pi * pow(r, 2);
	}
private:
	int r;
};
class Triangle :virtual public Shape
{
public:
	Triangle(int aa = 0, int bb = 0, int cc = 0)
		:a(aa), b(bb), c(cc)
	{}
	virtual double area()
	{
		double p = (a + b + c) / 2.0;
		return sqrt(p * (p - a) * (p - b) * (p - c));
	}
private:
	double a, b, c;
};
class Rectangle :public Shape
{
public:
	Rectangle(double xx,double yy)
		:x(xx),y(yy)
	{}
	double area()//不规范但是只要基类的函数前加virtual就可
	{
		return x * y;
	}
private:
	double x, y;
};
class Square :public Shape
{
public:
	Square(double aa)
		:a(aa)
	{}
	double area()
	{
		return a * a;
	}
private:
	double a;
};
int main()
{
	Circle c(1);
	Triangle tr(3, 4, 5);
	Rectangle rec(1, 2);
	Square sq(5);
	Shape* p = &c;//基类的指针调用
	cout<<p->area()<<endl;
	p = &tr;
	cout<<p->area()<<endl;
	Shape& it1 = rec;//基类的引用调用
	cout<<it1.area()<<endl;
	Shape& it2 = sq;
	cout<<it2.area()<<endl;
	return 0;
}

(3) 定义一个车 (Vehicle) 基类,有 run stop 等成员函数,由此派生出自行车 (Bicy
cle) 类、汽车 (Motorcar) 类,从 Bicycle Motorcar 派生出摩托车 (Motorcycle)
类,它们都有 run stop 等成员函数。观察虚函数的作用。
class Vehicle
{
public:
	virtual void run()
	{
		//cout << "The Vehicle is running" << endl;//这里感觉不应该有输出,因为这个类是抽象出来的
	}
	virtual void stop()
	{
		//cout << "The Vehicle has Stopped" << endl;
	}
};
class Bicycle :virtual public Vehicle
{
public :
	virtual void run()
	{
		cout << "The Bicycle is running" << endl;
	}
	virtual void stop()
	{
		cout << "The Bicycle has Stopped" << endl;
	}
};
class  Motorcar :virtual public Vehicle//虽然没有成员变量但这里一定要虚继承,否则最后基类调Motorcycle是会不明确
{
public:
	virtual void run()
	{
		cout << "The Motorcar is running" << endl;
	}
	virtual void stop()
	{
		cout << "The Motorcar has Stopped" << endl;
	}
};
class Motorcycle :virtual public Bicycle, virtual public Motorcar
{
public:
	virtual void run()
	{
		cout << "The Motorcycle is running" << endl;
	}
	virtual void stop()
	{
		cout << "The Motorcycle has Stopped" << endl;
	}
};
int main()
{
	Bicycle b;
	Motorcar car;
	Motorcycle mc;
	Vehicle* p = &b;
	p->run();
	p->stop();
	p = &car;
	p->run();
	p->stop();
	p = &mc;
	p->run();
	p->stop();
	return 0;
}

(3)继承时一定要虚继承,因为最后一个Motorcycle类继承了Motorcar和Bicycle类

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值