继承与派生实验
因为要按照教材多次更改代码,就把每次的版本都贴上来了。
#include <iostream>
using namespace std;
class Vehicle //定义基类
{
protected:
int MaxSpeed; //最大速度
int Weight; //重量
public:
Vehicle(int m, int w) //初始化成员变量的值
{
MaxSpeed = m;
Weight = w;
cout << "Constructing Vehicle... \n";
}
~Vehicle()
{
cout << "Destructing Vehicle... \n";
}
void Run()
{
cout << "The vehicle is running! \n";
}
void Stop()
{
cout << "Please stop running! \n";
}
void Show()
{
cout << "It \\'s maxspeed is:" << MaxSpeed << endl;
cout << "It \\'s weight is:" << Weight << endl;
}
};
class Bicycle : public Vehicle //定义派生类,公有继承
{
protected:
int Height; //高度,单位:厘米
public:
Bicycle(int m, int w, int h) : Vehicle(m, w) //调用基类构造函数
{
Height = h; //为本类中新增成员提供初始值
cout << "Constructing Bicycle...\n";
}
~Bicycle()
{
cout << "Destructing Bycycle...\n";
}
void Show() //改造基类的Show 函数
{
Vehicle::Show(); //调用基类Show 输出MaxSpeed 和Weight 值
cout << Height << endl; //输出本类高度
}
};
int main()
{
Bicycle b(1, 1, 1); //定义派生类对象
b.Run(); //观察构造、析构函数调用顺序
b.Stop();
b.Show();
return 0;
}
在Bicycle类下面增加Car类的定义
#include <iostream>
using namespace std;
class Vehicle //定义基类
{
protected:
int MaxSpeed; //最大速度
int Weight; //重量
public:
Vehicle(int m, int w) //初始化成员变量的值
{
MaxSpeed = m;
Weight = w;
cout << "Constructing Vehicle... \n";
}
~Vehicle()
{
cout << "Destructing Vehicle... \n";
}
void Run()
{
cout << "The vehicle is running! \n";
}
void Stop()
{
cout << "Please stop running! \n";
}
void Show()
{
cout << "It \\'s maxspeed is:" << MaxSpeed << endl;
cout << "It \\'s weight is:" << Weight << endl;
}
};
class Bicycle : public Vehicle //定义派生类,公有继承
{
protected:
int Height; //高度,单位:厘米
public:
Bicycle(int m, int w, int h) : Vehicle(m, w) //调用基类构造函数
{
Height = h; //为本类中新增成员提供初始值
cout << "Constructing Bicycle...\n";
}
~Bicycle()
{
cout << "Destructing Bycycle...\n";
}
void Show() //改造基类的Show 函数
{
Vehicle::Show(); //调用基类Show 输出MaxSpeed 和Weight 值
cout << Height << endl; //输出本类高度
}
};
class Car : public Vehicle //定义派生类Car,公有继承
{
protected:
int SeatNum; //座位数
public:
Car(int m, int w, int s) : Vehicle(m, w) //调用基类构造函数
{
SeatNum = s; //为本类中新增成员提供初始值
cout << "Constructing Car...\n";
}
~Car()
{
cout << "Destructing Car...\n";
}
void Show() //改造基类的Show 函数
{
Vehicle::Show(); //调用基类Show 输出MaxSpeed 和Weight 值
cout << SeatNum << endl; //输出本类座位数
}
};
int main()
{
Bicycle b(1, 1, 1); //定义自行车类对象
b.Run();
b.Stop();
b.Show();
Car c(2,2,2); //定义汽车类对象
c.Run();
c.Stop();
c.Show();
return 0;
}
增加的第3层类MotorCycle及修改以后的main( )函数
#include <iostream>
using namespace std;
class Vehicle //定义基类
{
protected:
int MaxSpeed; //最大速度
int Weight; //重量
public:
Vehicle(int m, int w) //初始化成员变量的值
{
MaxSpeed = m;
Weight = w;
cout << "Constructing Vehicle... \n";
}
~Vehicle()
{
cout << "Destructing Vehicle... \n";
}
void Run()
{
cout << "The vehicle is running! \n";
}
void Stop()
{
cout << "Please stop running! \n";
}
void Show()
{
cout << "It \\'s maxspeed is:" << MaxSpeed << endl;
cout << "It \\'s weight is:" << Weight << endl;
}
};
class Bicycle : virtual public Vehicle //定义派生类,公有继承
{
protected:
int Height; //高度,单位:厘米
public:
Bicycle(int m, int w, int h) : Vehicle(m, w) //调用基类构造函数
{
Height = h; //为本类中新增成员提供初始值
cout << "Constructing Bicycle...\n";
}
~Bicycle()
{
cout << "Destructing Bycycle...\n";
}
void Show() //改造基类的Show 函数
{
Vehicle::Show(); //调用基类Show 输出MaxSpeed 和Weight 值
cout << Height << endl; //输出本类高度
}
};
class Car : virtual public Vehicle //定义派生类Car,公有继承
{
protected:
int SeatNum; //座位数
public:
Car(int m, int w, int s) : Vehicle(m, w) //调用基类构造函数
{
SeatNum = s; //为本类中新增成员提供初始值
cout << "Constructing Car...\n";
}
~Car()
{
cout << "Destructing Car...\n";
}
void Show() //改造基类的Show 函数
{
Vehicle::Show(); //调用基类Show 输出MaxSpeed 和Weight 值
cout << SeatNum << endl; //输出本类座位数
}
};
class MotorCycle : public Bicycle, public Car //第3 层类
{
public:
MotorCycle(int m, int w, int h, int s) : Bicycle(m, w, h), Car(m, w, s), Vehicle(m, w)
{
cout << "Constructing MotorCycle...\n";
}
~MotorCycle()
{
cout << "Destructing MotorCycle...\n";
}
void Show() //输出4 个成员变量的信息,需消除二义性
{
cout << "It\'s maxspeed is:" << MaxSpeed << endl; //错误
cout << "It\'s weight is:" << Weight << endl; //错误
cout << "It\'s height is:" << Height << endl;
cout << "It\'s seatnum is:" << SeatNum << endl;
}
};
int main()
{
MotorCycle mc(1, 2, 3, 4); //定义摩托车类对象
mc.Run(); //错误
mc.Stop(); //错误
mc.Show();
return 0;
}
http://am473ur.com/