实验四
(2010-4-23)
多态程序设计
实验目的:
(1)掌握运算符重载的方法; 运算符其实相当于一个函数 只不过我们平时使用的是它的隐式形式
重载是函数名不变,带入函数的参数个数不变,但参数类型必须改变,返回值类型改变,功能也随之发生相应变化 其实质是将函数功能拓展
函数重载的方法与定义一个函数的方法几乎完全相同
格式为: 返回值类型 作用域::重载函数名 (参数表){函数定义}
注意:类其实也可以看做是一种数据类型 所以return 之后可以返回一个对象 (2)掌握使用虚函数实现动态多态性; 也叫动态联编 是通过指针实现的
练习项目
1.定义Point类,有坐标x,y两个成员变量,利用友元函数对Point类重载“++”运算符,实现对坐标值的改变。具体要求如下:
(3) 编写程序定义Point类,在类中定义整型的私有成员变量x,y; (4) 在类中定义两个友元函数,分别重载前置++和后置++; (5) 编写主函数测试。
注意函数有无返回值的区别,以及返回值是否带有&应用符号。
int a,b; 1程序代码:
#include
26
}
cout<<\cout<<\ob1.show();
cout<<\cout<<\ob1.show(); return 0;
运行结果:
x,y 5 6
Point++-> x=5,y=6
Now Point -> x=6,y=7 ++Point-> x=7,y=8 Now Point-> x=7,y=8 Press any key to continue
2.定义Point类,有坐标x,y两个成员变量,利用运算符重载对Point类重载“++”运算符,实现对坐标值的改变。具体要求如下:
1) 编写程序定义Point类,在类中定义整型的私有成员变量x,y;
2) 定义成员函数Point& operator++(); Point operator++(int);以实现对Point类重载“++”运算符,分别重载前置++
和后置++;
3) 编写主函数测试。
{ cout<<\Point ->
2 程序代码: x=\#include
运行结果: }
Point operator++(){ Input x y int a,b; 5 6 a=++x; b=++y; Point++ -> x=5,y=6 cout<<\Now Point -> x=6,y=7 return *this; ++Point -> x=7,y=8 } Now Point -> x=7,y=8 void show() Press any key to continue
3.定义一个分数类,通过重载运算符实现分数的四则运算、求负运算和赋值运算。其中,要求加法“+” 和减法“-”用友元函数实现重载,其他运算符用成员函数实现重载。
3程序代码:
27
#include
void simple(int &a,int &b)//将分子分母化简 { int i,c; c=abs(a)0&&b<0){a=-a;b=-b;}// }
class number{ int x,y; public: number(int a,int b) {x=a; y=b; simple(x,y);} friend number operator+(number &a,number &b){ number temp(0,0); temp.y=a.y*b.y; //先通分 temp.x=a.x*b.y+b.x*a.y; //通分后分子相加 simple(temp.x,temp.y); //最后化简 return temp; } friend number operator-(number &a,number &b){ number temp(0,0); temp.y=b.y*a.y; //与加法相同 temp.x=a.x*b.y-b.x*a.y; simple(temp.x,temp.y); return temp; } number operator*(number &a) { x*=a.x; y*=a.y; simple(x,y); return *this; } number operator/(number &a)
28
{
x*=a.y; y*=a.x; simple(x,y); return *this; } number operator=(number& a) { x=a.x; y=a.y; simple(x,y); return *this; } number operator-()//单目运算符“-”重载 { x=-x; y=y; return *this; } void print() {cout<<\ };
int main(void) { int x,y; cout<<\Class a/b a=? b=?\ number a(x,y); a.print(); cout<<\ a=-a; a.print(); cout<<\a/b=x/y Now Input x=? y=?\ number *p=new number(x,y);//来个动态的玩玩 a=*p; a.print(); delete p; cout<<\a/b+x/y Now Input x=? y=?\ number *q=new number(x,y); a=a+*q; a.print(); delete q;
cout<<\a/b-x/y Now y=?\ number *r=new number(x,y); a=a-*r; a.print(); delete r; cout<<\(a/b)*(x/y) Now y=?\ number *s=new number(x,y); a=a*(*s); a.print(); delete s; cout<<\(a/b)/(x/y) Now y=?\ number *t=new number(x,y); a=a/(*t); a.print(); delete t; return 0; }
Input x=?
运行结果:
Building Class a/b a=? b=? 5 6
Now a/b=5/6 Run a/b=-a/b Now a/b=-5/6
Run a/b=x/y Now Input x=? y=? 7 8
Now a/b=7/8
Run a/b+x/y Now Input x=? y=? 9 8
Now a/b=2/1
Run a/b-x/y Now Input x=? y=? 14 15
Now a/b=16/15
Run (a/b)*(x/y) Now Input x=? y=? 3 4
Now a/b=4/5
Run (a/b)/(x/y) Now Input x=? y=? 4 5
Now a/b=1/1
Press any key to continue
Input x=?
Input x=?
4.定义一个车(vehicle)基类,有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。具体要求如下: (1) 编写程序定义一个车(vehicle)基类,有Run、Stop等成员函数; (2) 由车(vehicle)基类派生出自行车(bicycle)类、汽车类(motorcar),从bicycle类和motorcar类派生出摩托
车类(motorcycle),它们都有Run、Stop等成员函数。
(3) 在main()函数中定义vehicle、bicycle、motorcar、motorcycle的对象,调用其Run()、Stop()函数,观察
其执行情况。
(4) 分别用vehicle类型的指针来调用几个对象的成员函数,看看能否成功(提示:把Run、Stop定义为虚
函数)。
class bicycle:virtual public vehicle{
4程序代码: public: #include
29
{cout<<\ ptr=&m_c; }; ptr->run(); class motorcycle:public bicycle,public motorcar{ ptr->stop(); public: ptr=&m_cy; void run() ptr->run(); {cout<<\ ptr->stop(); void stop() return 0; {cout<<\} };
运行结果: int main(void)
{ Bicycle run vehicle *ptr; Bicycle stop motorcar m_c; Motorcar run bicycle b; Motorcar stop motorcycle m_cy; Motorcycle run ptr=&b; Motorcycle stop ptr->run(); Press any key to continue ptr->stop();
5.编写程序,定义抽象基类Container,由此派生出2个派生类球体类Sphere,圆柱体类Cylinder,分别用虚函数分别计算表面积和体积。
(1) 球体的表面积为:4?r2,球体的体积为
4?r3; 圆柱表面积为: 2πR(h+R) 圆柱3体的体积πR2h。
(2) 定义相应的对象,编写主函数测试。
5程序代码:
#include
class Sphere:public Containner{ public:
30
Sphere(double a):Containner(a) {} void area() {cout<<\ void volume() {cout<<\olume=\};
class Cylinder:public Containner{ public: Cylinder(double a,double b):Containner(a) {h=b; } void area() {cout<<\ void volume() {cout<<\private: double h;