天津理工大学C++期末考试(6)

2019-04-09 17:56

实验四

(2010-4-23)

多态程序设计

实验目的:

(1)掌握运算符重载的方法; 运算符其实相当于一个函数 只不过我们平时使用的是它的隐式形式

重载是函数名不变,带入函数的参数个数不变,但参数类型必须改变,返回值类型改变,功能也随之发生相应变化 其实质是将函数功能拓展

函数重载的方法与定义一个函数的方法几乎完全相同

格式为: 返回值类型 作用域::重载函数名 (参数表){函数定义}

注意:类其实也可以看做是一种数据类型 所以return 之后可以返回一个对象 (2)掌握使用虚函数实现动态多态性; 也叫动态联编 是通过指针实现的

练习项目

1.定义Point类,有坐标x,y两个成员变量,利用友元函数对Point类重载“++”运算符,实现对坐标值的改变。具体要求如下:

(3) 编写程序定义Point类,在类中定义整型的私有成员变量x,y; (4) 在类中定义两个友元函数,分别重载前置++和后置++; (5) 编写主函数测试。

注意函数有无返回值的区别,以及返回值是否带有&应用符号。

int a,b; 1程序代码:

#include a=c.x++; b=c.y++; //using namespace std; cout<<\//Powered by X.Duke return c; class Point{ } int x,y; void show() public: { Point(){} cout<<\ x=\ Point(int &a,int &b) } { x=a,y=b; } }; friend Point operator++(Point &c){ int a,b; int main(void) a=++c.x; b=++c.y; { cout<<\ cout<<\ return c; int a,b; } cin>>a;cin>>b; friend Point operator++(Point &c,int){ Point ob1(a,b);

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 }; using namespace std; int main(void) //Powered by X.Duke { class Point{ int a,b; int x,y; cout<<\public: cin>>a>>b; Point(){} Point obj(a,b); Point(int &a,int &b) obj++; { x=a; y=b; } obj.show(); Point operator++(int){ ++obj; int a,b; obj.show(); a=x++; b=y++; return 0; cout<<\} return *this;

运行结果: }

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 #include //using namespace std; //Powered by X.Duke

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 void run() using namespace std; {cout<<\//Powered by X.Duke void stop() class vehicle{ {cout<<\public: }; virtual void run() class motorcar:virtual public vehicle{ {cout<<\public: virtual void stop() void run() {cout<<\ {cout<<\}; void stop()

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 using namespace std; //Powered by X.Duke const double PI=3.14; class Containner{ public: Containner(double a) {r=a; } virtual void area()=0; virtual void volume()=0; protected: double r; };

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;


天津理工大学C++期末考试(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:复习题十答案及详细分析

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: