大二(下)C++ 类的继承与派生实验

实验一:
定义一个车基类,派生出自行车类和汽车类,又以自行车类和汽车类为基类共同派生出摩托车类,每个类都要定义带有参数的构造函数。
对自行车类继承车基类的方式分别用private、protected、public,观察基类成员在派生类中的访问属性;观察自行车类、汽车类和摩托车类对象定义时构造、析构函数的调用顺序。最后将车基类定义为虚基类再观察程序运行结果。
第一版:

#include
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 << "It's Height is:" << Height << endl;      //输出本类高度   
	}
};
			   
int main()
{
     
	Bicycle b(120,2,4);          //定义派生类对象  
	b.Run();                     //观察构造、析构函数调用顺序  
	b.Stop();
    b.Show();
	getchar();
	return 0;			  
}

第二版:

#include
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 << "It's Height is:" << 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 << "It's SeatNum is:" << SeatNum << endl;     //输出本类座位数        
	}
};

class MotorCycle: public Bicycle, public Car  //第3层类,从第2层两个类公有继承 
{
     
public:
	MotorCycle(int m, int w, int h, int s): Bicycle(m, w, h), Car(m, w, s)   //调用两基类构造函数    
	{
     
		cout << "Constructing MotorCycle...\n";
	}
	~MotorCycle()
	{
     
		cout << "Destructing MotorCycle...\n";
	}
	void Show()          //输出4个成员变量的信息,需消除二义性   
	{
     
		cout << "It's maxspeed is:" << Bicycle::MaxSpeed << endl;  //错误    
		cout << "It's weight is:" << Car::Weight << endl;      //错误     
		cout << "It's height is:" << Height << endl;
		cout << "It's seatnum is:" << SeatNum << endl;
	}
};
int main()
{
     
	MotorCycle mc(100,1000,3,3); //定义摩托车类对象   mc.Run ( );
	mc.Bicycle::Stop();//错误   
	mc.Bicycle::Show();//错误
	getchar();
	return 0;
}

实验二:

#include
using namespace std;
class Base
{
      
public:
	int i;
	Base(int x) : i(x) 
	{
     
	}
	void show() 
	{
      
		cout << "i in Base is: " << i << endl;
	} 
};
class Derived: public Base
{
      
public:
	Derived(int x) : Base(x) 
	{
     
	}
	void show() 
	{
      
		cout  << "i in Derived is: " << i << endl; 
	} 
};
int main() 
{
     
	Base b1(1); //定义基类对象b1   
	cout << "基类对象 b1.show():\n";
	b1.show();
	Derived d1(2);    //定义派生类对象d1   
	b1=d1;            //用派生类对象给基类对象赋值   
	cout << "基类b1=d1,b1.show():\n";
	b1.show();
	cout << "派生类对象 d1.show():\n";
	d1.show();
	Base & b2=d1;   //用派生类对象来初始化基类引用   
	cout << "引用b2=d1, b2.show():\n";
	b2.show();
	Base * b3=&d1;  //派生类对象的地址赋给指向基类的指针   
	cout << "基类指针b3=&d1, b3->show():\n";
	b3->show();
	Derived *d4=new Derived(3); //定义派生类指针并生成新对象   
	Base* b4 = d4;              //派生类指针赋给指向基类的指针   
	cout << "基类指针b4 = d4, b4->show():\n";
	b4->show();
	cout << "派生类指针d4,d4->show():\n";
	d4->show();
	delete d4;
	getchar();
	return 0;
}

你可能感兴趣的:(C++)