{
public: DerivedClass(int a,int b):BaseClass1(a),number1(b) {} ~DerivedClass(){cout<<\析构函数调用!!!\private: BaseClass number; BaseClass1 number1; };
int main() { DerivedClass d(1,2); return 0; }
19) 定义一个车(vehicle)基类,具有MaxSpeed、Weight等成员变量,Run、Stop等成员函数,由此派生出自行车(bicycle)类,汽车(motorcar)类。自行车(bicycle)类有高度(Height)等属性,汽车(motorcycle)类有座位数(SeatNum)等属性。从bicycle和motorcycle派生出摩托车(Motorcar)类,在继承过程中,注意把vehicle设置为虚基类。如果不把vehicle 设置为虚基类,会有什么问?编程实验及分析原因。
#include
public: vehicle(int m,int w):MaxSpeed(m),Weight(w){cout<<\构造函数调用!!!\ void Run(){cout<<\车 开始以最大速度开始行驶\
注:实验成绩分为(90——100分)优,(80——89分)良,(70——79)中,(60——69分)及格,(59分)不及格。
void Stop(){cout<<\车 停止 \ ~vehicle(){cout<<\析构函数调用!!!\ int MaxSpeed; int Weight; };
class bicycle:virtual public vehicle {
public: bicycle(int m,int w,int h):vehicle(m,w),Hight(h){cout<<\构造函数调用!!!\ void Run(){cout<<\自行车高度是\ void Stop(){cout<<\自行车停止 \ ~bicycle(){cout<<\析构函数调用!!!\ int Hight; };
class motorcar:virtual public vehicle {
public: motorcar(int m,int w,int s):vehicle(m,w),SeatNum(s){cout<<\构造函数调用!!!\ void Run(){cout<<\汽车座位数是\ void Stop(){cout<<\汽车停止 \ ~motorcar(){cout<<\析构函数调用!!!\ int SeatNum; };
class motorcycle:public bicycle,public motorcar {
public: motorcycle(int m,int w,int h,int s):vehicle(m,w),bicycle(m,w,h),motorcar(m,w,s){} void Run(){cout<<\摩托车的速度:\ 重量:\ 高度:\ 座位数为:\ void Stop(){cout<<\摩托车停止 \ ~motorcycle(){cout<<\析构函数调用!!!\};
int main() { vehicle v(2,4); v.Run(); v.Stop(); bicycle b(2,5,7); b.Run(); b.Stop(); motorcar mc(7,9,4); mc.Run();
注:实验成绩分为(90——100分)优,(80——89分)良,(70——79)中,(60——69分)及格,(59分)不及格。
}
mc.Stop();
motorcycle m(20,300,4,1); m.Run(); m.Stop(); return 0;
20) 思考并回答:继承,派生,子类对基类成员的访问权限,继承方式,继承时的构造函数和析构函数的调用顺序,虚基类
继承:从基类继承所有的属性(构造函数和析构函数除外) 派生:产生新类的过程
子类对基类成员的访问权限:不能访问私有成员。共有继承:不变;私有继承:共有和保护作为私有出现;保护继承:共有和保护作为保护出现 继承方式:共有、私有、保护继承
继承时的构造函数和析构函数的调用顺序:完全相反
注:实验成绩分为(90——100分)优,(80——89分)良,(70——79)中,(60——69分)及格,(59分)不及格。
构造:被继承时的声明顺序 -> 内嵌对象的定义顺序 析构:新增的类型的成员 -> 内嵌对象 -> 继承的
虚基类:解决二义性(菱形结构)问题的(同一基类继承多次而产生的二义性问题)
实验五 多态和运算符重载 ( 设计性实验 2学时)
1. 目的要求:
掌握运算符重载的方法;学习使用虚函数实现动态多态性。
2. 实验内容:
21) 定义Point类,有坐标x,y两个私有成员变量;对Point类重载“+”(相加)、“-”(相减)和“==”(相等)运算符,实现对坐标的改变,要求用友元函数和成员函数两种方法实现。对Point类重载<<运算符,以使得代码 Point p; cout<
#include
private: int x,y; public:
Point(int x,int y):x(x),y(y){
cout<<\构造函数调用---\endl; }
~Point(){cout<<\析构函数调用---\endl;} int getX() const {return x;} int getY() const {return y;}
//Point operator + (Point &point2); //Point operator - (Point &point2); //Point operator == (Point &point2); inline void Display(){
cout<<\x<<\y<<\endl;
注:实验成绩分为(90——100分)优,(80——89分)良,(70——79)中,(60——69分)及格,(59分)不及格。
}
friend Point operator + (Point &point1, Point &point2); friend Point operator - (Point &point1, Point &point2); friend Point operator == (Point &point1, Point &point2); friend ostream & operator << (ostream &out,Point &point); }; /*
Point Point:: operator + (Point &point2) {
Point temp(0,0);
temp.x = this->x + point2.x; temp.y = this->y + point2.y; return temp; }
Point Point:: operator - (Point &point2) {
Point temp(0,0);
temp.x = this->x - point2.x; temp.y = this->y - point2.y; return temp; }
Point Point:: operator == (Point &point2) {
if ((this->x == point2.x) && (this->y == point2.y)) {
return point2; }
else cout<<\ }*/
Point operator + (Point &point1, Point &point2){ Point temp(0,0);
temp.x = point1.x + point2.x; temp.y = point1.y + point2.y; return temp; }
Point operator - (Point &point1, Point &point2){ Point temp(0,0);
temp.x = point1.x - point2.x; temp.y = point1.y - point2.y; return temp; }
Point operator == (Point &point1, Point &point2){
if ((point1.x == point2.x) && (point1.y == point2.y)) {
注:实验成绩分为(90——100分)优,(80——89分)良,(70——79)中,(60——69分)及格,(59分)不及格。