资源描述
(完整版)试验6 多态和虚函数、运算符重载
试验内容
(1)程序Ex_Shape:定义一个抽象类Cshape,包含纯虚数函数Area(用于计算面积)和SetData(用于重设形状大小),然后派生出三角形Ctriangle类,矩形Crect类、园Ccircle类,分别求其面积.最后定义一个Carea类,计算这几个形状的面积之和,各形状的数据通过Carea类构造函数或成员函数来设置,编写一个完整的程序。
(2)程序Ex_Complex;定义一个复数类Ccomplex,通过重载运算符“*”和“/”,直接实现两个复数之间的乘除运算。运算符“*”用成员函数实现重载,而运算符“/”用友元函数实现重载。编写一个完整的程序(包括测试运算符的程序部分)。
提示:两复数相乘的计算公式为:(a+bi)*(c+di)=(ac-bd)+(ad+bc)i ,而两复数相除的计算公式为: (a+bi)/(c+di)=(ac+bd)/(c*c+d*d)+(bc—ad)/ (c*c+d*d)i。
实验准备和说明
(1)在学习完第2章的“运算符重载”内容之后进行本次实验。
(2)编写本次上机所需要的程序.
实验步骤
1。创建工作文件夹
打开计算机,在“D:\Visual C++程序\ LiMing”文件夹中创建一个新的子文件夹”实验6“。
2.输入并运算程序Ex_Shape.cpp
输入并运算程序Ex_Shape。cpp的具体步骤如下。
(1)启动VisualC++6。0.
(2)单击标准工具栏上的“”按钮,在新打开的文档窗口中输入下列程序代码:
#include〈iostream.h>
Class Cshape
{
public:
virtual float Area()=0; //将Area定义成纯虚函数
virtual void SetData(float f1, float f2)=0; //将SetData定义成纯虚函数
};
class Ctriangle:public Cshape
{
public:
Ctriangle(float h=0, float w=0)
{H=h; W=w; }
float Area() //在派生类定义纯虚函数的具体实现代码
{ return(float)(H*W*0.5); }
void SetData(float f1, float f2)
{ H=f1; W=f2; }
private:
floatH,W;
};
Class Crect :publicCShape
{
public:
Crect(float h=0, float w=0)
{ H=h; W=w; }
float Area() //在派生类定义纯虚函数的具体实现代码
{ return(float)(H*W); }
void SetData(float f1, float f2)
{ H=f1; W=f2; }
private:
float H,W;
};
Class Ccircle: public Cshape
{
public:
Ccircle(float r=0)
{ R=r;}
float Area() //在派生类定义纯虚函数的具体实现代码
{ return(float)(3。14159265*R*R); }
void SetData(float r, float) //保持与纯虚函数一致
{ R=r;}
private:
float R;
};
class CArea
{
public:
CArea(float triWidth,float triHeight,float rcHeidht,float r)
{
ppShape=new Cshape*[3];
ppShape[0]= new CTriangle(triWidth,triHeight);
ppShape[1]= new CRect(Crect(rcWidth,rcHeight);
ppShape[2]= new CCircle(r);
}
~CArea()
{
for(inti=0;i<3;i++)
delete ppShape[i];
delete []ppShape;
}
void SetShapeData(int n,float f1,floatf2=0)
∥n为0表示操作的是三角形,1表示矩形,2表示圆形
{
if((n>2)‖(n<0)return;
ppShape[n]-〉SetData(f1,f2);
}
void.CalAndPrint(void) ∥计算并输出
{
float fSnm=0。0;
char*str[3]= {“三角”,“矩",“圆” };
for(int i=0;i<3;i++)
{
float area= ppShape[i]—>Area();∥通过基类指针,求不同形状的面积
cout<〈str[i]<<“形面积是:“〈〈area〈<endl;
fSum+=aera;
}
count〈<“总面积是”〈〈fSum〈〈endl;
}
Private:
Cshape**ppShape; ∥指向基类的指针数组
}
void main()
{
Carea a(10,20,6,8,6。5);
a.CalAndprint();
a.A。setshapedata(0,20,30);
a。CalAndprint();
a.SetShapeData(2,11);
a.CalAndprint();
a。setShapeData(1,2,5):
a。CalAndprint();
}
(3) 单击标准工具栏的Save按钮,弹出“保存为”文件对话框,将文件定位到“D\VisualC++程序\LiMing\实验6”,文件名为Ex_shape.cpp。
(4)编译运行,分析结果。
试一试在上述程序的基础上,若还有一个正方形Csquare,则这样的类应如何实现?整个程序应如何修改?
3.输入并运行程序Ex_Complex。cpp
输入并运行程序Ex_Complex.cpp的具体步骤如下.
(1)选择“文件”→“关闭工作区”,关闭原来项目。
(2)单击标准工具栏上的“New Text File”按钮,在新打开的文档窗口中述如下列程序代码:
#include<iostream。h〉
Class Ccomplex
{
Public:
Ccomplex(double r=0,double i=0)
{ realpart=r; imagepart=i; }
Void print()
{ cout〈〈"该复数实数="<<realpart<〈",虚部="〈〈imagepart〈〈endl; }
Ccomplex operator*(Ccomplex &b); // 成员函数重载运算符*
Friend CComplex operator/(CComplex &a,CComplex &b); // 友元函数重载运算符/private:
double realpart; //复数的实部
double imagepart; //复数的虚部
};
CComplex CComplex::operator *(CComplex &b)
{
CComplex temp;
temp。realPart=real.Part*b。realPart—imagePart*b.imagePart;
temp.imagePart=realPart*b。imagePart+imagePart*b。realPart;
return temp;
}
CComplex operator/(CComplex &a,CComplex &b)
{
CComplex temp;
double d=b。realPart*b.realPart+b.imagePart*b.imagePart;
temp。realPart=(a。realPart*b.realPart+a.imagePart*b。imagePart)/d;
temp.imagePart=(a。imagePart*b.realPart—a。realPart*b。imagePart)/d;
return temp;
}
void main()
{
CComplex c1(12,20),c2(50,70),c;
c=c1。c2;
c.print();
c=c1/c2;
c。print();
}
(3)单击标准工具栏的save按钮,弹出“保存为”文件对话框,将文件保存为Ex_Complex。cpp。
(4)编译运行,分析结果.
试一试 在上述程序的基础上,若还有复数和实数的乘除运算,则如何进行重载?
4。退出Visual C++ 6.0
5。写出实验报告
结合上述分析和试一试内容,写出实验报告。
思考与练习
(1)在程序Ex_Shape中,若基类CShape中没有纯虚函数SetData(),则编译肯定会有错误,这是为什么?
(2)用友元函数和成员函数进行运算符重载的区别是什么?
展开阅读全文