6.6 編寫一個程序設計一個汽車類vehicle,包含的數(shù)據(jù)成員有車輪個數(shù)wheels和車重weight。小車類car是它的私有派生類其中包含載人數(shù)passenger_load??ㄜ囶恡ruck是vehicle的私有派生類其中包含載人數(shù)passenger_load和載重量payload,每個類都有相關數(shù)據(jù)的輸出方法。
解:
vehicle類是基類由它派生出car類和truck類將公共的屬性和方法放在vehicle類中。
本題程序如下:
本程序的執(zhí)行結果如下:
#include
class vehicle // 定義汽車類
{
protected:
int wheels; // 車輪數(shù)
float weight; // 重量
public:
vehicle(int wheels,float weight);
int get_wheels();
float get_weight();
float wheel_load();
void show();
};
class car:public vehicle // 定義小車類
{
int passenger_load; // 載人數(shù)
public:
car(int wheels,float weight,int passengers=4);
int get_passengers();
void show();
};
class truck:public vehicle // 定義卡車類
{
int passenger_load; // 載人數(shù)
float payload; // 載重量
public:
truck(int wheels,float weight,int passengers=2,float max_load=24000.00);
int get_passengers();
float efficiency();
void show();
};
vehicle::vehicle(int wheels,float weight)
{
vehicle::wheels=wheels;
vehicle::weight=weight;
}
int vehicle::get_wheels()
{
return wheels;
}
float vehicle::get_weight()
{
return weight/wheels;
}
void vehicle::show()
{
cout << "車輪:" << wheels << "個" << endl;
cout << "重量:" << weight << "公斤" << endl;
}
car::car(int wheels, float weight,
int passengers) :vehicle (wheels, weight)
{
passenger_load=passengers;
}
int car::get_passengers ()
{
return passenger_load;
}
void car::show()
{
cout <<" 車型:小車" << endl;
vehicle::show();
cout << "載人:" << passenger_load << "人" << endl;
cout << endl;
}
truck:: truck(int wheels, float weight,int passengers, float max_load):vehicle(wheels,weight)
{
passenger_load=passengers;
payload=max_load;
}
int truck::get_passengers()
{
return passenger_load;
}
float truck::efficiency()
{
return payload/(payload+weight);
}
void truck::show()
{
cout <<"車型:卡車" << endl;
vehicle:: show ();
cout << "載人:" << passenger_load << "人" << endl;
cout << "效率:" << efficiency() << endl;
cout << endl;
}
void main ()
{
car car1(4,2000,5);
truck tru1(10,8000,3,340000);
cout << "輸出結果" << endl;
car1. show ();
tru1. show ();
}
輸出結果
車型:小車
車輪:4個
重量:2000公斤
載人:5人
車型:卡車
車輪:10個
重量:8000公斤
載人:3人
效率:0.977012
解:
vehicle類是基類由它派生出car類和truck類將公共的屬性和方法放在vehicle類中。
本題程序如下:
本程序的執(zhí)行結果如下:
#include
class vehicle // 定義汽車類
{
protected:
int wheels; // 車輪數(shù)
float weight; // 重量
public:
vehicle(int wheels,float weight);
int get_wheels();
float get_weight();
float wheel_load();
void show();
};
class car:public vehicle // 定義小車類
{
int passenger_load; // 載人數(shù)
public:
car(int wheels,float weight,int passengers=4);
int get_passengers();
void show();
};
class truck:public vehicle // 定義卡車類
{
int passenger_load; // 載人數(shù)
float payload; // 載重量
public:
truck(int wheels,float weight,int passengers=2,float max_load=24000.00);
int get_passengers();
float efficiency();
void show();
};
vehicle::vehicle(int wheels,float weight)
{
vehicle::wheels=wheels;
vehicle::weight=weight;
}
int vehicle::get_wheels()
{
return wheels;
}
float vehicle::get_weight()
{
return weight/wheels;
}
void vehicle::show()
{
cout << "車輪:" << wheels << "個" << endl;
cout << "重量:" << weight << "公斤" << endl;
}
car::car(int wheels, float weight,
int passengers) :vehicle (wheels, weight)
{
passenger_load=passengers;
}
int car::get_passengers ()
{
return passenger_load;
}
void car::show()
{
cout <<" 車型:小車" << endl;
vehicle::show();
cout << "載人:" << passenger_load << "人" << endl;
cout << endl;
}
truck:: truck(int wheels, float weight,int passengers, float max_load):vehicle(wheels,weight)
{
passenger_load=passengers;
payload=max_load;
}
int truck::get_passengers()
{
return passenger_load;
}
float truck::efficiency()
{
return payload/(payload+weight);
}
void truck::show()
{
cout <<"車型:卡車" << endl;
vehicle:: show ();
cout << "載人:" << passenger_load << "人" << endl;
cout << "效率:" << efficiency() << endl;
cout << endl;
}
void main ()
{
car car1(4,2000,5);
truck tru1(10,8000,3,340000);
cout << "輸出結果" << endl;
car1. show ();
tru1. show ();
}
輸出結果
車型:小車
車輪:4個
重量:2000公斤
載人:5人
車型:卡車
車輪:10個
重量:8000公斤
載人:3人
效率:0.977012