5540、題目內容:
P178 例6.2
定义基类base,含有私有数据成员price和quantity。定义构造函数和虚函数以显示数据成员的值。
定义派生类derived,公有继承base。含有数据成员life。定义构造函数,以及虚函数以显示数据成员的值。。
主函数中定义派生类对象mc,初值为2000、2、18。指向基类的指针pc,取派生类对象的地址赋值给pc。实现派生类数据成员的输出。
输入输出说明:
输出: price is 2000 quantity is 2 life is 18#includeusing namespace std; class base{ private: int price; int quantity; public: base(int price,int quantity){ this->price=price; this->quantity=quantity; } virtual void output(){ cout<<"price is "< life=life; } virtual void output(){ base::output(); cout<<"life is "< output(); }
5541、題目內容:
P184 例6.8
定义基类a1和a2一个含有虚函数,输出一个串“--a1--”,另一个含有非虚函数,输出串“--a2--”。
定义派生类b,含有函数输出串“--b--”。
定义主函数分别定义两个基类的对象及指针。定义派生类的对象。取基类对象的地址赋值给基类的指针,并调用类中的同名函数。取派生类类对象的地址赋值给基类的指针,并调用类中的同名函数。以实现要求的输出。
输入输出说明:
输出: --a1-- --a2-- --b-- --a2--#includeusing namespace std; class a1{ public: virtual void output(){ cout<<"--a1--"< output(); a2 *e2=&obj2; e2->output(); e1=&obj3; e1->output(); e2=&obj3; e2->output(); }
5542、題目內容:
P199 例7.4
定义含有两个私有数据成员的类,其有友元的++运算符重载函数,分别为前自加和后自加。以实现要求的输出。
输入输出说明:
输出: x: 11,y: 22 x: 12,y: 23 x: 13,y: 24 x: 14,y: 25 x: 15,y: 26#includeusing namespace std; class A{ public: int x,y; public: A(int x,int y){ this->x=x; this->y=y; } friend A operator++(A &obj); }; A operator++(A &obj){ obj.x++; obj.y++; return obj; } int main(){ A obj(11,22); for(int i=1;i<=5;i++){ cout<<"x: "<
5543、題目內容:
P204 例子7.6
以成员函数重载运算符++,包括前自加和后自加以实现要求的输出。
输入输出说明:
输出: x: 11,y: 22 x: 12,y: 23 x: 13,y: 24 x: 14,y: 25 x: 15,y: 26#includeusing namespace std; class A{ public: int x,y; public: A(int x,int y){ this->x=x; this->y=y; } A operator++(){ this->x++; this->y++; return *this; } }; int main(){ A obj(11,22); for(int i=1;i<=5;i++){ cout<<"x: "<
5533、題目內容:
P216 例7.13
将运算符函数重载为友元函数,并用重载的运算符“<<”输出日期。
即对象出现在日期类的输出流中。
输入输出说明:
输出: 2016.4.28#includeusing namespace std; class Date{ protected: int day,month,year; public: Date(int day,int month,int year){ this->day=day; this->month=month; this->year=year; } friend ostream& operator<<(ostream &os,const Date &d); }; ostream& operator<<(ostream &os,const Date &d){ cout<
5534、題目內容:
编写程序,计算汽车运行的时间,首先建立基类car,其中含有数据成员distance存储两点间的距离。假定距离以英里计算,速度为每小时80英里,使用虚函数travel_time()计算并显示通过这段距离的时间。在派生类kilometre中,假定距离以千米计算,速度为每小时120千米,使用函数travel_time()计算并显示通过这段距离的时间。距离从键盘输入(80英里~128公里)
输入输出说明:
输入: 10000 输出: base time is 78.125 derived time is 83.3333#includeusing namespace std; class car { public: car (double distance) { this->distance=distance; } virtual void travel_time() { //虚函数,以英里为单位且时速80计算行车时间 cout << "base time is " << distance / (80*1.6) << endl; }; protected: //派生类的distance成员构造时会调用基类的构造函数 double distance; }; class kilometer :public car { public: virtual void travel_time() { //虚函数,以千米为单位且时速120计算行车时间 cout << "derived time is " << distance / 120 < >x; car c1(x); //建立基类对象c1 kilometer c2(x); //派生类对象c2 car *index = &c1; //定义指向基类对象的指针index,指向c1 index->travel_time(); //调用了基类对象c1的travel_time函数 index = &c2; //指向派生类的对象c2 index->travel_time(); //调用了派生类对象c2的travel_time函数 return 0; }
5535、題目內容:
编一个程序,用成员函数重载运算符“+”和“-”,将两个二维数组相加和相减,要求第一个二维数组的值由构造函数设置,另一个二维数组的值由键盘输入。
输入输出说明:
第一个3*3二维数组通过构造函数设置,值为:1 2 3 4 5 6 7 8 9 另一个3*3二维数组的值由键盘输入,从键盘输入的值为1 2 3 4 5 6 7 8 9 基于二维数组的类定义二个对象,进行加减运算,所得结果赋值给另外一个对象。显示输出。 输入:1 2 3 4 5 6 7 8 9 输出: 2 4 6 8 10 12 14 16 18 0 0 0 0 0 0 0 0 0#includeusing namespace std; const int n=3; class Array{ public: Array(){ int k=1; for(int i=0;i p[i][j]+=b.p[i][j]; } } return *this; } Array operator-(Array b){ for(int i=0;i p[i][j]-=b.p[i][j]; } } return *this; } void show(){ for(int i=0;i >b[i][j]; } } Array a2(b); Array a3=a1; a1=a1+a2.p; a1.show(); a1=a3; a1=a1-a2.p; a1.show(); }
題目內容:
编一个程序,用友元函数重载运算符“+”和“-”,将两个二维数组相加和相减,要求第一个二维数组的值由构造函数设置,另一个二维数组的值由键盘输入。
输入输出说明:
输入:1 2 3 4 5 6 7 8 9 输出: 2 4 6 8 10 12 14 16 18 0 0 0 0 0 0 0 0 0#includeusing namespace std; const int n=3; class Array{ public: Array(){ int k=1; for(int i=0;i >b[i][j]; } } Array a2(b); Array a3; a3=operator+(a1,a2); a3.show(); a3=operator-(a1,a2); a3.show(); }



