您的位置:首页 > 其它

C 习题和解析(继承和派生-02)

2015-10-11 22:41 246 查看
6.6 编写一个程式设计一个汽车类vehicle,包含的数据成员有车轮个数wheels和车重weight。小车类car是他的私有派生类其中包含载人数passenger_load。卡车类truck是vehicle的私有派生类其中包含载人数passenger_load和载重量payload,每个类都有相关数据的输出方法。 
解: 
vehicle类是基类由他派生出car类和truck类将公共的属性和方法放在vehicle类中。 
本题程式如下: 
本程式的执行结果如下: 
#include<iostream.h> 
class vehicle // 定义汽车类 

protected: 
int wheels; // 车轮数 
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; // 载人数 
public: 
car(int wheels,float weight,int passengers=4); 
int get_passengers(); 
void show(); 
}; 
class truck:public vehicle // 定义卡车类 

int passenger_load; // 载人数 
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 

-------------------------------------------------------- 

6.7 设计一个圆类circle和一个桌子类table,另设计一个圆桌类roundtable,他是从前两个类派生的,需要输出一个圆桌的高度、面积和颜色等数据。 
解: 
circle类包含私有数据成员radius和求圆面积的成员函数getarea();table类包含私有数据成员height和返回高度的成员函数getheight()。roundtable类继承任何上述类的数据成员和成员函数,添加了私有数据成员color和相应的成员函数。本题程式如下: 
#include<iostream.h> 
#include<string.h> 
class circle 

double radius; 
public: 
circle(double r) { radius=r; } 
double getarea() { return radius*radius*3.14; } 
}; 
class table 

double height; 
public: 
table(double h) { height=h; } 
double getheight() { return height; } 
}; 
class roundtable : public table,public circle 

char *color; 
public: 
roundtable(double h, double r, char c[]) : circle (r) , table (h) 

color=new char[strlen(c) 1]; 
strcpy (color, c); 

char *getcolor() { return color; } 
}; 
void main() 

roundtable rt(0.8,1.2,"黑色"); 
cout << "圆桌属性数据:" << endl; 
cout << "高度:" <<rt.getheight() << "米" << endl; 
cout << "面积:" <<rt.getarea() << "平方米" << endl; 
cout << "颜色:" <<rt.getcolor() << endl; 

本程式的执行结果如下: 
圆桌属性数据: 
高度:0.8米 
面积:4.5216平方米 
颜色:黑色 

------------------------------------------------------- 

6.8 设计一个虚基类base,包含姓名和年龄私有数据成员连同相关的成员函数,由他派生出领导类leader,包含职务和部门私有数据成员连同相关的成员函数。再由base派 生出工程师类engineer,包含职称和专业私有数据成员连同相关的成员函数。然后由1eda和engineer类派生出主任工程师类chairman。采用一些数据进行测试。 
解: 
由于chairman类从leader类和engineer类派生,而leader类和engineer类都是从base类派生的,所以为了使base只存一个副本,必须采用虚拟派生的方法。 
本题程式如下: 
#include<iostream.h> 
#include<string.h> 
class base // 基类 

char* name;// 姓名 
int age; // 年龄 
public: 
base(){} 
void setname(char na[]) 

name=new char[strlen(na) 1];

版权申明:本站文章均来自网络,如有侵权,请联系028-86262244-215 ,我们收到后立即删除,谢谢! 

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: