类的运行时多态(一次实验作业)

T1.Purpose of the experiment

Master the writing grammar of pure virtual function; Be able to construct an abstract class skillfully; Skillfully use virtual
function to realize polymorphism. Remember the assignment
compatibility rule. Learn to transfer different types of formal
parameters under different topic requirements.

T2.Experiment content

[Experiment topic 1]
定义一个包含两个纯虚函数的抽象类shape:
class shape
{
public:
virtual float Area()=0;
virtual void SetData(float,float=0)=0;
};
成员函数SetData用于设置几何图形的属性值。成员函数Area()用于求出几何图形的面积。由shape派生出四种几何图形,
分别是:三角形、矩形、正方形和圆形。
例如三角形类的定义如下:
class triangle:public shape
{
float w,h;
public:
triangle(float ww=0,float hh=0){w=ww;h=hh;}
float Area(){return w*h/2;}
void SetData(float ww,float hh=0){w=ww;h=hh;}
};
其它图形的派生类请自行编写。派生类的类名和属性如下:
1)类名 triangle 属性:三角形的一边长w和此边上的高h
2)类名 rectangle 属性:矩形的长w和宽h
3)类名 square 属性:正方形边长s
4)类名 circle 属性:圆的半径r
要求利用虚函数实现多态,编写一个求几何图形面积的普通函数CaclArea(shape *),其参数为抽象类shape的指针
在主函数中定义四种几何图形的对象,调用CaclArea()求四种几何图形对象的面积之和。

 Experimental procedure

#include<iostream>
#define PI 3.1415
using namespace std;
//Define an abstract class shape containint two pure virtual funcionts
class shape {
public:
	virtual float Area() = 0;//pure virtual function
	virtual void SetData(float, float = 0) = 0;//pure virtual function
	static float sum_area;
};
float shape::sum_area = 0;
//Define a derived class--triangle
class triangle :public shape {
	float bottom, high;
public:
	triangle(float b = 0, float h = 0) :bottom(b), high(h) {}
	float Area() { return bottom * high / 2; }
	void SetData(float b, float h = 0) { bottom = b; high = h; }
};
//Define a derived class--rectangle
class rectangle :public shape {
	float length, width;
public:
	rectangle(float l = 0, float w = 0) :length(l), width(w) {}
	float Area() { return length* width; }
	void SetData(float l, float w) { length = l; width = w; }
};
//Define a derived class--square
class square :public shape {
	float side;
public:
	square(float s = 0) :side(s) {}
	float Area() { return side * side; }
	void SetData(float s,float t=0) { side = s; }
};
//Define a derived class--circle
class circle :public shape {
	float radium;
public:
	circle(float r = 0) :radium(r) {}
	float Area() { return PI * radium*radium; }
	void SetData(float r,float t=0) { radium = r; }
};
//Define a function to get area
float CaclArea(shape* p) {
	(p->sum_area) += p->Area();
	return p->Area();
}
int main() {
	shape* p[4];
	p[0] = new triangle(2, 1);//bottom:2 high:1
	p[1] = new rectangle(3, 2);//length:3 width:2
	p[2] = new square(4);//side:4
	p[3] = new circle(5);//radium:5
	cout << "The area of triangle is " << CaclArea(p[0]) << endl;
	cout << "The area of rectangle is " << CaclArea(p[1]) << endl;
	cout << "The area of square is " << CaclArea(p[2]) << endl;
	cout << "The area of circle is " << CaclArea(p[3]) << endl;
	cout << "The total area of them is " << p[0]->sum_area << endl;
	for (int i = 0; i < 4; i++)delete p[i];
}

 Experimental results and analysis

在这里插入图片描述
The experimental results are as expected.
According to the requirements of the topic, the virtual function is used to realize polymorphism(多态), and the purpose of different interface(接口) selection is achieved.

 Experimental process analysis

在这里插入图片描述

During the experiment, I found that the compiler reported an error: the square and circle defined by the display are abstract class types. It makes me wonder. Finally, I went to check the setting of pure virtual function in the parent class shape again:

virtual void SetData(float, float = 0) = 0;

At that time, the definition of SetData function in the square class was as follows:
void SetData(float s);
Later, I learned that the formal parameters (形式参数)in the subclass(子类) did not match those in the base class.
Then I revised it as follows: void SetData(float s,float t=0);
At last, the program runs normally.**

 Empirical conclusioin

If we can make good use of virtual functions to achieve polymorphism, we will make the code more efficient.

T3.Experiment summary

Keep in mind that abstract classes cannot define objects.

Secondly, when there are pure virtual functions int the base class, if you don’t want the subclass to be abstract, you must redefine the pure virtual function and correspond its parameter list one by one to ensure the compilation success.

It should be noted that virtual is only used in class bodies. When a member function with the same name is defined in a derived class, as long as the number of parameters, parameter type and return type of the function are exactly the same as the virtual function with the same in the base class, the member function of the derived class will become a virtual function whether it uses virtual or not.

When the virtual function is accessed by a base class pointer of reference, the system will determine the virtual function version of the class where the calling object is based on the actual object pointed or referenced by the runtime pointer of reference.

By Suki
Good night!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值