![]() |
#2
qlc002009-11-12 21:09
#include<iostream>
using namespace std; class vehile { public: vehile(){}; vehile(unsigned int a); void run(); void stop(); void showVehile(); private: unsigned int weight; }; class bicycle:virtual public vehile { public: bicycle(); bicycle(unsigned int a,unsigned int b); void showBicycle(); private: unsigned int height; }; class motorcar:virtual public vehile { public: motorcar(); motorcar(unsigned int a,unsigned int c); void showMotorcar(); private: unsigned int seat; }; class motorcycle:public bicycle,public motorcar { public: motorcycle(); motorcycle (unsigned int a,unsigned int b,unsigned int c,unsigned int d); void showMtorcycle(); private: unsigned int length; }; vehile::vehile(unsigned int a) { weight=a; } void vehile::showVehile() { cout<<"重量:"<<weight<<endl; } void vehile::run() { cout<<"出发!"<<endl; } void vehile::stop() { cout<<"停止!"<<endl; } bicycle::bicycle(unsigned int a,unsigned int b):vehile(a) { height=b; } void bicycle::showBicycle() { cout<<"高度:"<<height<<endl; } motorcar::motorcar(unsigned int a,unsigned int c):vehile(a) { seat=c; } void motorcar::showMotorcar() { cout<<"座位数:"<<seat<<endl; } motorcycle::motorcycle(unsigned int a,unsigned int b,unsigned int c,unsigned int d):bicycle(a,b),motorcar(a,c),vehile(a) { length=d; } void motorcycle::showMtorcycle() { cout<<"长度"<<length<<endl; } void main() { motorcycle a(2,12,90,35); cout<<"摩托车的属性如下:"<<endl; a.showVehile(); a.showBicycle(); a.showMotorcar(); a.showMtorcycle(); cout<<"启动试车"<<endl; a.run(); a.stop(); } 这个是我帮你改好的,但是头文件什么的我都写在一起了。错误的地方就是类的成员函数在类外实现的时候没有加类的作用域!还有就是在类中声明的时候不能它它继承基类,只有在类外实现的时候才能那样初始化! [ 本帖最后由 qlc00 于 2009-11-12 23:41 编辑 ] |
头文件:
#ifndef LAB7_3_H
#define LAB7_3_H
#include<iostream>
using namespace std;
class vehile
{
public:
vehile();
vehile(unsigned int a);
void run();
void stop();
void showVehile();
private:
unsigned int weight;
};
class bicycle:virtual public vehile
{
public:
bicycle();
bicycle(unsigned int a,unsigned int b):vehile(a);
void showBicycle();
private:
unsigned int height;
};
class motorcar:virtual public vehile
{
public:
motorcar();
motorcar(unsigned int a,unsigned int c):vehile(a);
void showMotorcar();
private:
unsigned int seat;
};
class motorcycle:public bicycle,public motorcar
{
public:
motorcycle();
motorcycle (unsigned int a,unsigned int b,unsigned int c,unsigned int d):bicycle(a,b),motorcar(a,c),vehile(d);
void showMtorcycle();
private:
unsigned int length;
};
#endif;
方法实现文件
#include"lab7_3car.h"
vehile::vehile(unsigned int a) //车构造函数
{
weight=a;
}
void vehile::showVehile()
{
cout<<"重量:"<<weight<<endl;
}
void vehile::run()
{
cout<<"出发!!!"<<endl;
}
void vehile::stop()
{
cout<<"停止!!!"<<endl;
}
bicycle(unsigned int a,unsigned int b):vehile(a)//自行车构造函数
{
height=b;
}
void bicycle::showBicycle()
{
cout<<"高度:"<<height<<endl;
}
motorcar(unsigned int a,unsigned int c):vehile(a) //摩托车构造函数
{
seat=c;
}
void motorcar::showMotorcar()
{
cout<<"座位数:"<<seat<<endl;
}
motorcycle::motorcycle(unsigned int a,unsigned int b,unsigned int c,unsigned int d):bicycle(a,b),motorcar(a,c),vehile(d)
{
length=d;
}
void motorcycle::showMtorcycle()
{
cout<<"长度:"<<length<<endl;
}
主函数:
#include<iostream>
#include"lab7_3car.h"
using namespace std
int main()
{
motorcycle a(2,12,90,35);
cout<<"摩托车的属性如下:"<<endl;
a.showVehile();
a.showBicycle();
a.showMotorcar();
a.showMtorcycle();
cout<<"启动试车:"<<endl;
a.run();
a.stop();
}
[ 本帖最后由 softzhonghua 于 2009-11-12 19:09 编辑 ]