Thursday, November 08, 2012

C++ 範例程式 : 類別, 物件, 繼承, 類別成員, 類別函式, 多載, 覆載

類別 : Car, RacingCar
物件 : car1, mcar1, ...
類別成員 : sum
類別函式 : showSum()
多載 : Car(), RacingCar()
覆載 : show()

程式碼 include iostream

using namespace std;

#define ASIZE 3

class Car {
	protected:
		int num;
		double gas;

	public:
		static int sum;
		Car();
		Car(int);
		Car(int,double);
		int setNum(int);
		int setGas(double);
		void setCar(int,double);
		int getNum(){ return num; }	//inline function
		double getGas(){ return gas; }	//inline function
		virtual void show();
		static void showSum();
};

class RacingCar : public Car {
	private:
		int course;

	public:
		static int sum;
		RacingCar();
		RacingCar(int);
		RacingCar(int,double,int);
		void setCourse(int);
		int getCourse(){ return course; }	//inline function
		void show();
		static void showSum();
};

// For class Car
int Car::sum=0;

Car::Car()
{
	sum++;
	cout << "製造一輛車" << endl;
	num = 0;
	gas = 0.0;
}

Car::Car(int n)
{
	sum++;
	cout << "製造一輛車" << endl;
	setNum(n);
	gas = 0.0;
}

Car::Car(int n, double g)
{
	sum++;
	cout << "製造一輛車" << endl;
	setNum(n);
	setGas(g);
}

int Car::setNum(int n)
{
	if(n > 0)
	{
		num = n;
		cout << "設定車輛編號 " << num << endl;
		return 0;
	}
	else
	{
		cout << "無法設定車輛編號, 數值必須大於 0." << endl;
		return -1;
	}
}

int Car::setGas(double g)
{
	if(g > 0)
	{
		gas = g;
		cout << "設定油量 " << gas << endl;
		return 0;
	}
	else
	{
		cout << "無法設定油量, 數值必須大於 0." << endl;
		return -1;
	}
}

void Car::setCar(int n, double g)
{
	setNum(n);
	setGas(g);
}

void Car::show()
{
	cout << "車輛編號 " << num << " 油量 " << gas << endl;
}

void Car::showSum()
{
	cout << "一共生產 " << sum << " 輛車" << endl;
}

// For class RacingCar
int RacingCar::sum = 0;

RacingCar::RacingCar()
{
	sum++;
	cout << "改造成一輛賽車" << endl;
	course = 0;
}

RacingCar::RacingCar(int c)
{
	sum++;
	cout << "改造成一輛賽車" << endl;
	setCourse(c);
}

RacingCar::RacingCar(int n, double g, int c) : Car(n, g)
{
	sum++;
	cout << "改造成一輛賽車" << endl;
	setCourse(c);
}

void RacingCar::setCourse(int c)
{
	if(c > 0)
	{
		course = c;
		cout << "設定賽車跑道 " << course << endl;
	}
	else
	{
		cout << "設定賽車跑道號碼必須大於零 " << endl;
	}
}

void RacingCar::show()
{
	cout << "車輛編號 " << num << " 油量 " << gas << endl;
	cout << "賽車跑道 " << course << endl;
}

void RacingCar::showSum()
{
	cout << "一共生產 " << sum << " 輛賽車" << endl;
}

// main function
void buy(Car&);

int main()
{
	Car car1;

	cout << "設定 car1 資訊" << endl;
	car1.setNum(1);
	car1.setGas(5168.88);
	cout << "已設定車輛編號 " << car1.getNum() << " 油量 " << car1.getGas() << endl;

	cout << "查詢 car1 資訊" << endl;
	car1.show();

	cout << endl;
	Car cara[ASIZE];

	cout << "設定 cara 資訊" << endl;
	for(int i = 0; i < ASIZE; i++)
	{
		cara[i].setNum(i+2);
		cara[i].setGas(1000*(i+1)+688);
		cout << "已設定車輛編號 " << cara[i].getNum() << " 油量 " << cara[i].getGas() << endl;
	}

	cout << "查詢 cara 資訊" << endl;
	for(int i = 0; i < ASIZE; i++)
	{
		cara[i].show();
	}

	cout << endl;
	Car *pCar;
	pCar = new Car;

	cout << "設定 pCar 資訊" << endl;
	pCar->setNum(5);
	pCar->setGas(3600);
	cout << "已設定車輛編號 " << pCar->getNum() << " 油量 " << pCar->getGas() << endl;

	cout << "查詢 pCar 資訊" << endl;
	pCar->show();

	cout << endl;
	cout << "肌肉車 mcar 生產資訊" << endl;
	Car mcar1(6);
	mcar1.setGas(6000);
	Car mcar2 = Car(7, 6800);

	cout << "查詢肌肉車 mcar 資訊" << endl;
	mcar1.show();
	mcar2.show();

	Car::showSum();

	cout << "\n購買汽車" << endl;
	buy(car1);
	buy(cara[0]);
	buy(*pCar);

	//
	// RacingCar
	//
	cout << "\n\n生產賽車 racar1" << endl;
	RacingCar racar1;
	racar1.setCar(8, 3600.0);
	racar1.setCourse(5);

	cout << "查詢賽車 racar1 資訊" << endl;
	racar1.show();

	RacingCar::showSum();
	Car::showSum();

	cout << "\n\n生產賽車 racar2" << endl;
	RacingCar racar2 = RacingCar(9, 3000.0, 6);

	cout << "查詢賽車 racar2 資訊" << endl;
	racar2.show();

	RacingCar::showSum();
	Car::showSum();

	cout << "\n\n賽車指標 pRacar1" << endl;
	Car* pRacar1;
	pRacar1 = &racar1;

	pRacar1->show();

	RacingCar::showSum();
	Car::showSum();

	cout << "\n\n車輛指標陣列 pCarArr" << endl;
	Car* pCarArr[3];
	cout << "車輛指標陣列 pCarArr[0] 指向 racar1" << endl;
	pCarArr[0] = &racar1;
	cout << "車輛指標陣列 pCarArr[1] 指向 racar2" << endl;
	pCarArr[1] = &racar2;
	cout << "車輛指標陣列 pCarArr[2] 生產一輛賽車" << endl;
	pCarArr[2] = new RacingCar(10, 3000.0, 7);

	RacingCar::showSum();
	Car::showSum();

	cout << "\n\n查詢車輛指標陣列 pCarArr" << endl;
	for(int i = 0; i < 3; i++)
	{
		pCarArr[i]->show();
	}

	RacingCar::showSum();
	Car::showSum();

	cout << "\n\n賽車指標陣列 pRaCarArr" << endl;
	RacingCar* pRaCarArr[3];
	cout << "車輛指標陣列 pRaCarArr[0] 指向 racar1" << endl;
	pRaCarArr[0] = &racar1;
	cout << "車輛指標陣列 pRaCarArr[1] 指向 racar2" << endl;
	pRaCarArr[1] = &racar2;
	cout << "車輛指標陣列 pRaCarArr[2] 生產一輛賽車" << endl;
	//pRaCarArr[2] = pCarArr[2];
	//car_class_inheritence_protected-baseptr.cpp:282:26: error: invalid conversion from ‘Car*’ to ‘RacingCar*’
	pRaCarArr[2] = new RacingCar(11, 5000.0, 8);

	RacingCar::showSum();
	Car::showSum();

	cout << "\n\n查詢賽車指標陣列 pRaCarArr" << endl;
	for(int i = 0; i < 3; i++)
	{
		pRaCarArr[i]->show();
	}

	RacingCar::showSum();
	Car::showSum();

	return 0;
}

void buy(Car& c)
{
	cout << "購買了車輛編號 " << c.getNum() << " 油量 " << c.getGas() << endl;
}

No comments: