⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pex12_1.cpp

📁 数据结构C++代码,经典代码,受益多多,希望大家多多支持
💻 CPP
字号:
#include <iostream.h>
#include <string.h>
#pragma hdrstop

// base class for entire hierarchy
class Vehicle
{
	protected:
    	char vehicleName[20];
    public:
		// constructor. assign vehicleName
		Vehicle(char nmv[])
		{
			strcpy(vehicleName,nmv);
		}

		// print vehicleName      
		void Identify(void)
		{
			cout << vehicleName << " vehicle" << endl;
		}
};

class Automobile: public Vehicle
{
	protected:
    	char automobileName[20];
    public:
		// constructor. initialize base class
		// and assign automobileName
		Automobile(char nmv[], char nma[]): Vehicle(nmv)
		{
			strcpy(automobileName,nma);
		}
      
		// execute base class Identify and print automobileName      
		void Identify(void)
		{
			Vehicle::Identify();
			cout << automobileName << " automobile" << endl;
		}
};

class Airplane: public Vehicle
{
	protected:
    	char airplaneName[20];
    public:
		// constructor. initialize base class
		// and assign airplaneName
		Airplane(char nmv[], char nma[]): Vehicle(nmv)
		{
			strcpy(airplaneName,nma);
		}
      
		// execute base class Identify and print airplaneName      
		void Identify(void)
		{
			Vehicle::Identify();
			cout << airplaneName << " airplane" << endl;
		}
};

class Gasoline: public Automobile
{
	protected:
    	char gasolineVehicleName[20];
    public:
		// constructor. initialize base class
		// and assign gasolineVehicleName
		Gasoline(char nmv[], char nma[], char nmg[]): Automobile(nmv,nma)
		{
			strcpy(gasolineVehicleName,nmg);
		}
      
		// execute base class Identify and print gasolineVehicleName      
		void Identify(void)
		{
			Automobile::Identify();
			cout << gasolineVehicleName << " gasoline powered" << endl;
		}
};

// Jet is a type of Airplane
class Jet: public Airplane
{
	protected:
    	char jetName[20];
    public:
		// constructor. initialize base class
		// and assign jetName
		Jet(char nmv[], char nma[], char nmj[]): Airplane(nmv,nma)
		{
			strcpy(jetName,nmj);
		}
      
		// execute base class Identify and print jetName      
		void Identify(void)
		{
			Airplane::Identify();
			cout << jetName << " jet" << endl;
		}
};

void main(void)
{
	// declare four instances of objects in the hierarchy
	Vehicle A("hefty");
	Automobile B("big", "4 wheel drive");
	Gasoline C("large", "pickup truck", "Ford");
	Jet D("jumbo", "passenger", "Boeing");
	
	// execute Identify for each object
	A.Identify();
	B.Identify();
	C.Identify();
	D.Identify();
}

/*
<Run>

hefty vehicle
big vehicle
4 wheel drive automobile
large vehicle
pickup truck automobile
Ford gasoline powered
jumbo vehicle
passenger airplane
Boeing jet
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -