打印本文 打印本文  关闭窗口 关闭窗口  
C++习题与解析(继承和派生-02)
作者:佚名  文章来源:不详  点击数  更新时间:2008/4/18 14:40:53  文章录入:杜斌  责任编辑:杜斌

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]; 
strcpy(name,na); 

void setage(int a) 

age=a; 

char* getname() { return name; } 
int getage() { return age; } 
}; 
class leader:virtual public base // 领导类 

char *job;//职务 
char *dep;//部门 
public: 
leader() { } 
void setjob(char jb[]) 

job=new char[strlen(jb)+1]; 
strcpy (job, jb); 

void setdep(char dp[]) 

dep=new char [strlen (dp) +1] ; 
strcpy (dep, dp); 

char *getjob() { return job; } 
char *getdep() { return dep; } 
}; 
class engineer:virtual public base // 工程师类 

char *major; // 专业 
char *prof; // 职称 
public: 
engineer () { } 
void setmajor(char maj []) 

major=new char[strlen(maj)+1]; 
strcpy (major,maj); 

void setprof(char pf[]) 

prof=new char[strlen(pf)+1]; 
strcpy (prof, pf); 

char*getmajor() {return major; } 
char*getprof() {return prof; } 
} ; 
class chairman:public leader,public engineer // 主任工程师类 
{ } ; 
void main() 

chairman c; 
c.setname("李明"); 
c.setage(42); 
c.setjob("处长"); 
c.setdep("设计处"); 
c.setmajor("电站锅炉设计"); 
c.setprof("高级工程师"); 
cout<< "输出结果:"<<endl; 
cout << " " << c.getname() << ",年龄" << c.getage()<<"岁,担任" <<c.getdep() <<c.getjob() <<","<<endl; 
cout << " " << c.getprof() << ",从事" << c.getmajor()<< "专业" << "。 " << endl; 


本程序的执行结果如下: 
输出结果: 
李明,年龄42岁,担任设计处处长, 
高级工程师,从事电站锅炉设计专业。 
打印本文 打印本文  关闭窗口 关闭窗口