《面向对象程序设计》实验指导书(1-6个实验,含参考代码) 下载本文

面向对象程序设计

实验指导书

(适用:电子信息11级)

彭召意 陶立新 编写

计算机与通信学院

2014.9

目录

实验一 C++基础的应用 ................................................................................................................... 1 实验二 类和对象的应用 ................................................................................................................. 3 实验三 类的构造函数、析构函数的应用 ..................................................................................... 4 实验四 友员和运算符重载 ............................................................................................................. 5 实验五 类的继承与派生 ................................................................................................................. 6 实验六 类的多态性与虚函数 ......................................................................................................... 7 附录:各实验的程序代码 ............................................................................................................... 8

I

实验一 C++基础的应用

(实验课时:2 实验性质:设计)

实验名称: C++基础的应用

实验目的: (1)进一步学习VC++6.0开发环境及程序调试方法。

(2)练习C++函数的定义及使用; (3)练习C++数组的定义及使用; (4)练习C++指针的定义及使用; (5)练习C++结构体的定义及使用;

(6)练习多文件的程序的编译和运行方法;

实验设备: (1)硬件:个人微机(配置不低于:CPU为P4,主频1.6G,内存256MB,硬盘40GB);

(2)软件:操作系统为WindowsXP(或2000、server2003等),工具软件为Visual

C++6.0。

实验内容: (1)熟悉Visual C++6.0编译系统的常用功能,特别是debug调试功能;

(2)编程1:编写一个程序c1.cpp,用来求2个或3个整数的最大数。要求:

用重载函数的方法来求最大数;函数原型:int max( int a, int b) 和 int max( int a, int b,int c)。

(3)编程2:编写一个程序c2.cpp,求: a!+ b! + c!的值。要求:使用递归函

数。主程序和函数分开到两个源程序文件中,分别进行编译后,再运行;

(4)编程3:有一个3*4的矩阵,要求编程求出其中值最大的那个元素的值,

以及其所在的行号和列号;

(5)编程4:建立一个动态链表并进行输出和删除管理。链表的每个节点为学

生信息,包括:学号,姓名,性别,下一学生信息的指针。程序的工作:(a)建立三个学生信息的节点,然后顺序输出该三个学生信息;(b)删除中间的节点,再顺序输出学生信息。

(6)使用debug调试功能:追踪第2个程序中函数的运行。

(7)思考:

在第2个程序中,如果a,b,c的值较大,则显示的结果会怎样?这时如何

解决此问题?

(提示:显示的结果会不对,可能还会是负数,定义一个数组来存放最后结果。)

实验要求: (1)掌握Visual C++6.0开发环境的常用功能及使用,能进一步使用Visual C++6.0的debug调试功能:单步调试、设置断点等;

(2)掌握C++函数的定义及使用,掌握函数的嵌套调用和递归调用的方法; (3)掌握C++数组、指针、结构体的定义及使用; (4)掌握多文件的程序的编译和运行方法; (5)程序格式规范,程序运行正确;

(6)认真书写实验报告,如实填写各项实验内容。

1

实验步骤: (1)启动Visual C++6.0开发环境;

(2)熟悉Visual C++6.0编译系统的常用功能; (3)在file菜单中,新建C++源程序c1.cpp; (4)编辑C++源程序文件c1.cpp的内容。

(5) 编译c1.cpp,系统会提示是否创建工程(由于前面没有创建工程),选择

“是”。则创建一个工程;

(6)程序编译正确时,则可建立可执行程序; (注意观察每步的结果) (7)运行可执行文件; (8)关闭工作空间。

(9)对编程2,在file菜单中,新建C++源程序cpp2_1; (10)编辑C++源程序文件cpp2_1的内容;

(11)编译cpp2_1,系统会提示是否创建工程(由于前面没有创建工程),选择

“是”,则创建工程;

(12)在file菜单中,新建C++源程序cpp2_2; (13)编辑C++源程序文件cpp2_2的内容;

(14)编译文件cpp2_2时,系统提示是否加入到此工程,选择“是”,则把文件

cpp2_2加入到该工程中。

(15)编译和运行此程序,并注意观察每步的结果。

(16)使用调试功能:运行到光标处、单步跟踪、查看变量值。 (17)使用关闭工作空间。

(18)参照上述方法,完成编程3和编程4。

注意:若编译时出错:“fatal error c1010:unexpexted end of file while looking for

precompiled header directive.”;

该文件需要包含头文件:stdafx.h,即在开头有语句:#include “stdafx.h”

思考题:假如已建立了两个C++源程序c1.cpp和c2.cpp。先用open打开c1.cpp,并完成了编译、连接和运行,再打开c2.cpp,对它进行编译和连接。此时会出现什么问题?原因何在?怎样解决?

2

实验二 类和对象的应用

(实验课时:2 实验性质:设计)

实验名称: 类和对象的应用

实验目的: (1)熟悉C++面向对象的程序设计的基本概念;

(2)练习类的定义和对象的创建;

(3)练习对象的成员函数的调用方法以及对象的初始化;

实验设备: (1)硬件:个人微机(配置不低于:CPU为P4,主频1.6G,内存256MB,硬盘40GB);

(2)软件:操作系统为WindowsXP(或2000、server2003等),工具软件为Visual

C++6.0。

实验内容: 事先编好程序,上机调试和运行程序,分析结果。

(1)编程1:定义并实现一个简单的学生类:公有属性为:学号、姓名、班级,

成员函数为输入和显示一个学生的上述信息;

(2)将程序(1)中的数据成员改为私有的,考虑对程序有何影响;

(3)编程2:设计一个长方形类 Rectangle。成员变量包括:长度和宽度,成员

函数有:Area计算面积,Set设置长方形的长和宽,Get获取长方形的长和宽。最后,编写一个主程序来测试该类及成员函数。

(4)编程3:设计一个汽车类Car。成员变量有:品牌,颜色,车门,车灯,行

驶速度。成员函数有:打开车门、打开车灯、加速、减速等。最后,编写一个主程序来测试该类及功能。(提示:在成员函数中用输出成员函数名来表示该成员函数被调用)

实验要求: (1)掌握C++类和对象的基本概念;

(2)掌握C++类和对象的定义及使用; (3)掌握对象的成员函数的调用方法; (4)程序格式规范,程序运行正确;

(5)认真书写实验报告,如实填写各项实验内容。

实验步骤: (1)启动Visual C++6.0开发环境;

(2)创建一个项目;

(3)建立C++源程序文件; (4)编辑C++源程序文件内容; (5)建立并运行可执行程序; (6)关闭工作空间。

3

实验三 类的构造函数、析构函数的应用

(实验课时:2 实验性质:设计)

实验名称: 类的构造函数、析构函数的应用

实验目的: (1)练习类的构造函数、析构函数的定义和使用方法;

(2)练习对象数组、对象指针的使用; (3)练习类的静态成员的使用。

实验设备: (1)硬件:个人微机(配置不低于:CPU为P4,主频1.6G,内存256MB,硬盘40GB);

(2)软件:操作系统为WindowsXP(或2000、server2003等),工具软件为Visual

C++6.0。

实验内容: (1)编程:设计一个学生类。要求: 学生的属性有:学号,姓名,性别; 学生的行为有:输入学生的上述属性和输出学生的上述属性。 编写构造函数对学生的所有属性初始化,无参数的构造函数设置学号为1001,姓名为“张三”,性别为‘M’(‘M’代表男,‘F’代表女)。

(2)修改上述程序:修改定义析构函数,在析构函数中输出学号。再运行程序,

观察运行结果。

(3)修改上述main程序:使用对象指针,用new生成一个学生对象,然后,

用delete清除该对象,使用单步运行观察运行结果。

(4)修改上述程序:增加一个私有数据成员成绩score;增加一个成员函数

readdata:输入一学生的学号和成绩;增加一个成员函数getscore:取得学生的成绩。再修改main函数;定义一个班的同学(假如一个班有10位同学,采用对象数组),输出该班的平均成绩。

(5)不用对象数组,而使用静态的数据成员来实现(4)中求一个班的学生的平

均成绩,学生的成绩在构造函数中输入。

(提示:定义2个静态数据成员,1个是存储总成绩,1个是存储学生人数)。 (6)分别运行上述程序,并分析结果。

注意:在增加一个新的功能时,可以把暂时不需要的功能代码给注释掉。

实验要求: (1)掌握C++类的构造函数、析构函数的定义和使用方法;

(2)掌握C++对象数组、对象指针的使用; (3)掌握类的静态数据成员的定义和使用; (4)程序格式规范,程序运行正确;

(5)认真书写实验报告,如实填写各项实验内容。

实验步骤: (1)启动Visual C++6.0开发环境;

(2)创建一个项目;

(3)建立C++源程序文件; (4)编辑C++源程序文件内容; (5)建立并运行可执行程序; (6)关闭工作空间。

4

实验四 友员和运算符重载

(实验课时:2 实验性质:设计)

实验名称: 友元和运算符重载

实验目的: (1)进一步了解运算符重载的概念和使用方法;

(2)掌握几种常用的运算符重载的方法; (3)练习友员函数的定义和使用; (4)了解转换构造函数的使用方法。

实验设备: (1)硬件:个人微机(配置不低于:CPU为P4,主频1.6G,内存256MB,硬盘40GB);

(2)软件:操作系统为WindowsXP(或2000、server2003等),工具软件为Visual

C++6.0。

实验内容: 事先编好程序,上机调试和运行程序,分析结果。

(1)有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使

之能用于矩阵相加。如: c=a+b。

(2)编写一个友元函数来读取矩阵指定所在行列的值。

(3)定义一个Teacher(教师)类和一个Student(学生)类,二者有一部分数据成员是

相同的,例如num(号码),name(姓名),sex(性别)。编写程序,将一个Student对象(学生)转换为Teacher(教师)类,只将以上3个相同的数据成员移植过去。可以设想为: 一位学生大学毕业了,留校担任教师,他原有的部分数据对现在的教师身份来说仍然是有用的,应当保留并成为其教师的数据的一部分。

实验要求: (1)掌握C++运算符重载的概念和使用方法;

(2)熟悉转换构造函数的使用方法; (3)程序格式规范,程序运行正确;

(4)认真书写实验报告,如实填写各项实验内容。

实验步骤: (1)启动Visual C++6.0开发环境;

(2)创建一个项目;

(3)建立C++源程序文件; (4)编辑C++源程序文件内容;

(5)建立并运行可执行程序,并分析结果; (6)关闭工作空间。

5

实验五 类的继承与派生

(实验课时:2 实验性质:设计)

实验名称:类的继承与派生

实验目的: (1)进一步理解继承与派生的概念;

(2)掌握通过继承派生出一个新的类的方法;

实验设备: (1)硬件:个人微机(配置不低于:CPU为P4,主频1.6G,内存256MB,硬盘40GB);

(2)软件:操作系统为WindowsXP(或2000、server2003等),工具软件为Visual

C++6.0。

实验内容: 事先编好程序,上机调试和运行程序,分析结果。

(1)编程:编写一个学生类Student。要求: ① 学生的保护属性有:学号(Num),姓名(Name),性别(Sex);

学生的公用成员函数有:ReadData( )用于输入Num、Name、Sex。OutputData( )

用于输出Num、Name、Sex。

② 然后,在学生类的基础上派生出研究生类Graduate,该类新增私有属性研究

方向(ResearchD),新增公用成员函数ResearchWork( ),该函数功能为输出学号和研究方向。

③ 编写main函数测试基类和派生类的各属性很函数的访问。修改派生类的派生

方式以及基类成员的访问方式,再测试看看基类和派生类的属性访问方式有何不同。

(2)编程:在上述工程中增加一个Teacher(教师)类,采用多重继承方式由

Student和Teacher类公有派生出新类Student_Teacher。要求:

① Teacher类的保护成员有:Name、Sex、Title(职称)、Wages(工资)。两

个基类中的Name、Sex同名,在引用这些数据成员时,指定作用域。

② 在派生类Student_Teacher中,增加两个成员函数:ReadF1()用于读入:

Num、Name、Sex、Title、Wages。Show()用于输出数据,在Show()中,首先调用Student类的display()函数用于输出Num、Name、Sex,然后再用cout语句输出Title、Wages。

③ 在类体中声明成员函数,在类外定义成员函数。

思考题:类的三种继承方式的主要区别是什么?

实验要求: (1)掌握继承与派生的概念;

(2)掌握通过继承派生出一个新的类的方法; (3)程序格式规范,程序运行正确;

(4)认真书写实验报告,如实填写各项实验内容。

实验步骤: (1)启动Visual C++6.0开发环境;

(2)创建一个项目;

(3)建立C++源程序文件; (4)编辑C++源程序文件内容; (5)建立并运行可执行程序; (6)关闭工作空间。

6

实验六 类的多态性与虚函数

(实验课时:2 实验性质:设计)

实验名称: 类的多态性与虚函数 实验目的: (1)了解多态性的概念;

(2)了解虚函数的作用和使用方法;

(3)了解纯虚函数和抽象类的概念和用法。

实验设备: (1)硬件:个人微机(配置不低于:CPU为P4,主频1.6G,内存256MB,硬盘40GB);

(2)软件:操作系统为WindowsXP(或2000、server2003等),工具软件为Visual

C++6.0。

实验内容: 事先编好程序,上机调试和运行程序,分析结果。

(1)编程:定义抽象基类Shape(图形)。

Shape类有一个公共的纯虚函数Area,用于派生类求图形的面积。 (2)由Shape派生出3个派生类:Circle(圆形)、Rectangle(矩形)、Triangle(三

角形),用一个函数PrintArea分别输出以上三者的面积,3个图形的数据在定义对象时给定。

在主程序中测试这些类的PrintArea函数。

(3)在类Circle基础上再派生一个圆柱体Cylinder,重载Area函数求圆柱体的

体积。然后,编写主程序采用静态多态性和动态多态性分别测试Area函数。

(4)在类Circle中定义一个析构函数,让它输出一行信息“This is Cicle

Destruction。”,在类Cylinder中定义一个析构函数,让它输出一行信息“This is Cylinder Destruction。”。

在主程序中用指向基类Circle类的指针动态生成Cylinder类对象,然后用delete

语句释放该对象,测试看看运行结果如何。再修改Circle的析构函数为虚函数,测试看看运行结果如何。

(5)运行上述程序,并分析结果。

实验要求: (1)掌握多态性的概念;

(2)掌握虚函数的作用和使用方法;

(3)掌握纯虚函数和抽象类的概念和用法; (4)程序格式规范,程序运行正确;

(5)认真书写实验报告,如实填写各项实验内容。

实验步骤: (1)启动Visual C++6.0开发环境;

(2)创建一个项目;

(3)建立C++源程序文件; (4)编辑C++源程序文件内容;

(5)建立并运行可执行程序,并分析结果; (6)关闭工作空间。

思考题:虚函数的作用是什么?

7

附录:各实验的程序代码

实验1:程序代码

程序1:

#include using namespace std; int main()

{int max(int a,int b,int c); int max(int a,int b); int a=8,b=-12,c=7;

cout<<\ cout<<\ return 0; }

int max(int a,int b,int c) {if(b>a) a=b; if(c>a) a=c; return a; }

int max(int a,int b) {if(a>b) return a; else return b; }

程序2: //cpp2_1.cpp

#include using namespace std; int main()

{int fac(int); int a,b,c,sum=0; cout<<\ cin>>a>>b>>c;

sum=sum+fac(a)+fac(b)+fac(c);

cout<

//cpp2_2.cpp,递归方式 int fac(int n)

8

{

if ( n<=1) return 1; else

return ( n*fac(n-1)); }

//cpp2_2.cpp,非递归方式 int fac(int n) {int f=1;

for (int i=1;i<=n;i++) f=f*i; return f; }

程序3:

#include using namespace std; int main()

{ int i,j,row=0,colum=0,max;

int a[3][4]={{5,12,23,56},{19,28,37,46},{-12,-34,6,8}}; max=a[0][0]; for (i=0;i<=2;i++) for (j=0;j<=3;j++) if (a[i][j]>max) {max=a[i][j]; row=i; colum=j; }

cout<<\ return 0; }

程序4:

#include //#include using namespace std; const int n=10; struct student { char num[6]; char name[8]; char sex;

student *next;

9

} ;

int main() { int i;

student *head,*p1,*p2;

head=p1=NULL; for (i=0;i<3;i++) { p2 = new student; if (p2==NULL) break; cout<<\ cout<<\ cin>>p2->num; cout<<\ cin>>p2->name; cout<<\ cin>>p2->sex; if (i==0) { head=p1=p2; } else { p1->next = p2; p1 = p2; } }

if (p1!=NULL) p1->next = NULL;

cout<<\ p1=head; i=0;

while (p1!=NULL) { i++; cout<<\ cout << \ name:\ p1 = p1->next; }

10

p1= head->next;

head->next = p1->next; delete p1;

cout<<\ p1=head; i=0;

while (p1!=NULL) { i++; cout<<\ cout << \ name:\ p1 = p1->next; }

while (head!=NULL) { p1 = head->next; delete head; head = p1; }

return 0; }

思考题:

打开c2.cpp,对它进行编译时,出现提示:

选择“否”,出现提示:

11

选择“是”,则编译完成。连接时,出现错误提示:

--------------------Configuration: Cpp1 - Win32 Debug-------------------- Linking...

Cpp2.obj : error LNK2005: _main already defined in Cpp1.obj

Debug/Cpp1.exe : fatal error LNK1169: one or more multiply defined symbols found Error executing link.exe.

Cpp1.exe - 2 error(s), 0 warning(s)

出错原因:c1工程中有两个main()函数。

解决方法:关闭c1的工作空间后,再打开c2.cpp。

12

实验2:程序代码 (1)定义并实现类 student

#include class student //定义类 { public: //类的公有成员 int Num; char Name[10]; char Sclass[20];

void inputs( )

{ cin>>Num>>Name>>Sclass;} void display( )

{ cout << \学号: \<<” 姓名:”<

void main( ) //主函数 { student ss; //声明对象 ss.inputs(); //调用成员函数 ss.display( );

}

(2)程序2

include using namespace std; class Time {public:

void set_time(void) {cin>>hour; cin>>minute; cin>>sec; }

void show_time(void)

{cout<

private: int hour;

13

班级:”<

int minute; int sec; };

Time t; int main() {

t.set_time(); t.show_time(); return 0; }

程序3

#include using namespace std; class Time {public:

void set_time(void); void show_time(void); private: int hour; int minute; int sec; };

void Time::set_time(void) {cin>>hour; cin>>minute; cin>>sec; }

void Time::show_time(void)

{cout<

Time t; int main() { t.set_time(); t.show_time(); return 0; }

14

实验3:程序代码 (1)3.5

#include #include using namespace std; class Student {public:

Student(int n,string nam,char s) {num=n; name=nam; sex=s;

cout<<\ }

~Student()

{cout<<\ void display()

{cout<<\ cout<<\ cout<<\ }

private: int num; string name; char sex; };

int main()

{Student stud1(10010,\ stud1.display();

Student stud2(10011,\ stud2.display(); return 0; }

(2)修改析构函数: ~Student()

{cout<<\

(3)增加部分:

Student *p=new Student(10012,\Li_si\m'); delete p;

(4)增加部分: 数据成员:

15

private:

float score;

成员函数: public:

Student() {};

void readdata() { cin>>num>>score;} float getscore() {return score;}

修改main:

增加:Student s[10]; int i;

float total=0; 增加:

for (i=0;i<10;i++) { s[i].readdata();

total+=s[i].getscore(); }

cout<

(5)增加:

在类Student中:friend void fdisplay(Student *s); 在类外:

void fdisplay(Student *s)

{cout<<\};

在main函数中: Student s1; s1.readdata(); fdisplay(&s1);

16

实验4:

程序1: #include using namespace std;

class Matrix //定义Matrix类 {public:

Matrix(); //默认构造函数 friend Matrix operator+(Matrix &,Matrix &); //重载运算符“+” void input(); //输入数据函数 void display(); // private:

int mat[2][3]; };

Matrix::Matrix() //{for(int i=0;i<2;i++) for(int j=0;j<3;j++) mat[i][j]=0; }

Matrix operator+(Matrix &a,Matrix &b) //{Matrix c;

for(int i=0;i<2;i++) for(int j=0;j<3;j++)

{c.mat[i][j]=a.mat[i][j]+b.mat[i][j];} return c; }

void Matrix::input() //{cout<<\ for(int i=0;i<2;i++) for(int j=0;j<3;j++) cin>>mat[i][j]; }

void Matrix::display() //{for (int i=0;i<2;i++) {for(int j=0;j<3;j++) {cout<

int main() {Matrix a,b,c; a.input();

17

输出数据函数 定义构造函数 定义重载运算符“+”函数 定义输入数据函数 定义输出数据函数

b.input();

cout<

cout<

c=a+b; //用重载运算符“+”实现两个矩阵相加 cout<

另外的参考矩阵类: 定义一个矩阵类Matrix

//首先定义了矩阵类的最大行数和列数 #include using namespace std; #define MaxRow 100 #define MaxColumn 100

class Matrix //定义Matrix类 {public:

Matrix() { Row = 2;Column = 2;}; //默认构造函数

Matrix(int r,int c) //带参数的构造函数 { int i,j;

if (r>MaxRow) Row = MaxRow; else

Row = r;

if (c>MaxColumn)

Column = MaxColumn; else

Column = c; for(i=0;i

for (j=0;j

int MatVal[MaxRow][MaxColumn]; //矩阵值 int Row;//行数

int Column; //列数 };

18

//主程序 int main() {

Matrix a,b,c(2,3); return 0; }

程序2:

#include using namespace std; class Student {public:

Student(int,char[],char,float); int get_num(){return num;}

char * get_name(){return name;} char get_sex(){return sex;} void display()

{cout<<\}

private: int num;

char name[20]; char sex; float score; };

Student::Student(int n,char nam[],char s,float so) {num=n;

strcpy(name,nam); sex=s; score=so; }

class Teacher {public:

Teacher(){}

Teacher(Student&);

Teacher(int n,char nam[],char sex,float pay); void display(); private: int num;

char name[20];

19

char sex; float pay; };

Teacher::Teacher(int n,char nam[],char s,float p) {num=n;

strcpy(name,nam); sex=s; pay=p; }

Teacher::Teacher(Student& stud) {num=stud.get_num();

strcpy(name,stud.get_name()); sex=stud.get_sex(); pay=1500;}

void Teacher::display()

{cout<<\\\n\

int main()

{Teacher teacher1(10001,\ Student student1(20010,\ cout<<\ student1.display();

teacher2=Teacher(student1); cout<<\ teacher2.display(); return 0; }

20

实验5:程序代码 (1)例5.3

#include #include using namespace std;

class Student //声明基类

{public: //基类公用成员 void display( );

protected : //基类保护成员 int num; string name; char sex; };

void Student::display( )

{cout<<\ cout<<\ cout<<\ }

class Student1: protected Student //用protected继承方式声明一个派生类 {public:

void display1( ); private:

int age; string addr; };

void Student1::display1( )

{cout<<\引用基类的保护成员,合法 cout<<\引用基类的保护成员,合法 cout<<\引用基类的保护成员,合法 cout<<\引用派生类的私有成员,合法 cout<<\引用派生类的私有成员,合法 };

int main( )

{Student1 stud1; //stud2是派生类student2的对象 stud1.display1( ); //display是派生类中的公用成员函数 stud1.num = 10023; // 该语句错误,应该删除。 return 0; }

21

增加的成员函数ReadData(): (1)声明语句为:

void ReadData(); (2)定义该函数的语句为:

void Student::ReadData() {

cout<<”Please input : num,name,sex:”; cin >>num>>name>>sex; cout<

(2)增加的Teacher类:

class Teacher //声明基类 {

protected : //基类保护成员 string name;

char sex;

string title; double wages; };

(3)派生类Student_Teacher:

class Student_Teacher: public Student,public Teacher {

public:

void ReadF1(); void Show(); };

void Student_Teacher ::ReadF1() {

cout<<\:num,name,sex,title,wages:\ cin>>num>>Teacher::name>>Teacher::sex>>title>>wages; Student::name = Teacher::name; Student::sex = Teacher::sex; cout<

void Student_Teacher::Show() {

display();

cout<<\ cout<<\}

22

(4) 在main函数中增加测试语句: Student_Teacher ST1; ST1. ReadF1(); ST1. Show();

23

实验6:(2014正确代码)

#include using namespace std;

#define PI 3.14 //(1) class Shape { public: }; //(2)

class Circle:public Shape { public: /* */ };

virtual //(4) { }

double R;

cout<<\~Circle() void PrintArea() { }

cout<<\面积=\virtual double Area() { }

return (PI*R*R); Circle(double r):R(r){ } virtual double Area() = 0;

void PrintArea() { }

cout<<\面积=\

public:

24

class Rectangle:public Shape { public: Rectangle(double L,double W) { Length = L; Width = W;

}

virtual double Area() { return (Length*Width); }

public: double Length,Width; };

class Triangle:public Shape { public: Triangle(double B,double H) { Bottom = B; Higth = H; }

virtual double Area() { return (Bottom*Higth/2); }

public: double Bottom,Higth; }; //(3)

class Cylinder:public Circle { public: Cylinder(double r,double h):Circle(r) { Higth = h;

}

25

};

virtual double Area() { }

void PrintArea() { } //(4) virtual //(4) ~Cylinder() { }

double Higth;

cout<<\。\cout<<\体积=\return (PI*R*R*Higth);

public:

void main() { /*(4)

//动态的多态性

cout<

Cylinder cy1(1.5,6); cout<<\圆柱体的\cy1.PrintArea(); Triangle t1(4,5); cout<<\三角形的\t1.PrintArea(); Rectangle r1(2,4); cout<<\矩形的\r1.PrintArea(); Circle c1(3); cout<<\圆的\c1.PrintArea();

26

Shape *p;

cout<<\圆的面积:\ p = &c1; p->PrintArea();

cout<<\圆柱体的体积:\ p = &cy1; p->PrintArea();

*/

//(4) //(4)

Circle *p2 = new Cylinder(4,6); delete p2;

}

27

28