实验三 虚函数与多态 纯虚函数(完整版)

实验三 虚函数与多态 纯虚函数(完整版)
实验三 虚函数与多态 纯虚函数(完整版)

实验三虚函数与多态、纯虚函数

一.实验目的

1. 在掌握继承与派生关系的基础上,进一步理解虚函数与多态性的关系,实现运行时的多态。

2. 学会定义和使用纯虚函数

二、实验容

1.例:了解"单接口,多方法"的概念。现有称为figure的基类,存放了各二维对象(三角形、矩形和圆形三个类)的各维数据,set_dim()设置数据,是标准成员函数。

show_area()为虚函数,因为计算各对象的面积的方法是不同的。

【程序】

#include < iostream >

using namespace std;

class figure{

protected:

double x,y;

public:

void set_dim(double i,double j=0)

{ x=i; y=j; }

virtual void show_area()

{ cout<<"No area computation defined for this class.\n";

}

};

class triangle:public figure{

void show_area()

{ cout<<"Triangle with height "<< x<<" and base "<< y<<" has an area of "<< x*0.5*y<< endl;

}

};

class square:public figure{

public:

void show_area()

{ cout<<"Square with dimensions "<< x<<" and "<< y<<" has an area of "<< x*y<< endl;

}

};

class circle:public figure{

public:

void show_area()

{ cout<<"Circle with radius "<< x<<" has an area of "<<3.14159*x*x<< endl;

}

};

int main(){

figure *p;

triangle t;

square s;

circle c;

p->set_dim(10.0,5.0);

p->show_area();

p=&s;

p->set_dim(10.0,5.0);

p->show_area();

p=&c; p->set_dim(10.0);

p->show_area();

return 0;

}

【要求】

(1)建立工程,录入上述程序,调试运行并记录运行结果。

(2)修改上述程序,将virtual void show_area()中的virtual去掉,重新调试运行观察结果有何变化?为什么?

在不使用关键字virtual后,基类指针p对show-area的访问p-

>show_area()没有针对所指对象的类型调用不同的函数,而是直接根据p的类型调用了基类的成员函数show-area。

(3)修改上述程序入口函数,使其动态建立三角形、矩形和圆形3个对象,通过基类指针访问这3个对象,然后释放这3个对象。

#include < iostream >

using namespace std;

class figure{

protected:

double x,y;

public:

void set_dim(double i,double j=0)

{ x=i; y=j; }

virtual void show_area()

{ cout<<"No area computation defined for this class.\n";

}

};

class triangle:public figure{

public:

void show_area()

{ cout<<"Triangle with height "<< x<<" and base "<< y<<" has an area of "<< x*0.5*y<< endl;

}

};

class square:public figure{

public:

void show_area()

{ cout<<"Square with dimensions "<< x<<" and "<< y<<" has an area of "<< x*y<< endl;

}

};

class circle:public figure{

public:

void show_area()

{ cout<<"Circle with radius "<< x<<" has an area of

"<<3.14159*x*x<< endl;

}

};

int main()

{

figure *p;

triangle *p1;

square *p2;

circle *p3;

p1=new triangle;

p2=new square;

p3=new circle;

p=p1;

p->set_dim(10.0,5.0); p->show_area();

p=p2;

p->set_dim(10.0,5.0); p->show_area();

p=p3;

p->set_dim(10.0);

p->show_area(); delete p1;

delete p2;

delete p3;

}

(4)修改类定义中的析构函数,使之适应用户动态定义对 2、使用纯虚函数和抽象类对实验二中的题1进行改进。

#include < iostream >

using namespace std;

class figure{

protected:

double x,y;

public:

void set_dim(double i,double j=0)

{ x=i; y=j; }

virtual void show_area()

{

cout<<"No area computation defined for this class.\n";

}

///////////////////////////////////

///////在此处将基类的析构函数声明为虚析构就OK了

virtual ~figure(){}

////////////////////////////////////

};

class triangle:public figure{

public:

void show_area()

{

cout<<"Triangle with height "<< x<<" and base "<< y<<" has an area of "<< x*0.5*y<< endl;

}

};

class square:public figure{

public:

void show_area()

{

cout<<"Square with dimensions "<< x<<" and "<< y<<" has an area of "<< x*y<< endl;

}

};

class circle:public figure{

public:

void show_area()

{

cout<<"Circle with radius "<< x<<" has an area of

"<<3.14159*x*x<< endl;

}

};

int main(){

figure *p;

triangle t;

square s;

circle c;

p=&t;

p->set_dim(10.0,5.0);

p->show_area();

p=&s;

p->set_dim(10.0,5.0);

p->show_area();

p=&c; p->set_dim(10.0);

p->show_area();

return 0;

}

2、编程:自定义一个抽象类Element,提供显示、求面积等公共接口(虚函数),派生出Point、Line、Circle等图形元素类,并重新定义(override)这些虚函数,完成各自的任务。

在这里,Element是抽象基类,它不能提供具体的显示操作,应将其成员函数定义为纯虚函数。只有采用指向基类的指针或对基类的引用进行调用,实现的才是动态绑定,

完成运行时的多态性。

#include < iostream >

using namespace std;

const double PI=3.14159;

class Element{

public:

virtual const char* Name() const =0;

virtual double Area() const =0;

};

class Point:public Element{

protected:

double x,y;

public:

Point(double xv=0,double yv=0):x(xv),y(yv)

{}

virtual const char* Name() const{return "Point";}

virtual double Area() const{return 0;}

};

class Line:public Element{

protected:

double length;

public:

Line(double l=0):length(l){}

virtual const char* Name() const{return "Line";} virtual double Area() const{return 0;}

};

class Circle:public Element{

protected:

double radius;

public:

Circle(double r):radius(r){}

virtual const char* Name() const{return "Circle";} virtual double Area() const{return PI*radius*radius;} };

void print(Element *p){

cout<Name()<<":\t"<Area()<

}

int main()

{

Point A(1,1);

print(&A);

Line B(10);

print(&B);

Circle C(10);

print(&C);

return 0;

}

相关主题
相关文档
最新文档