| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 425 人关注过本帖
标题:急!!!用继承写了个小程序,请高手们看看错在哪里
只看楼主 加入收藏
softzhonghua
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2009-4-21
结帖率:80%
收藏
已结贴  问题点数:10 回复次数:2 
急!!!用继承写了个小程序,请高手们看看错在哪里
头文件:
#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 编辑 ]
搜索更多相关主题的帖子: 继承 
2009-11-12 19:08
qlc00
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:2
帖 子:157
专家分:540
注 册:2007-11-26
收藏
得分:5 
#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 编辑 ]

Anything is possible!
2009-11-12 21:09
yyx511
Rank: 1
等 级:新手上路
帖 子:4
专家分:5
注 册:2009-11-17
收藏
得分:5 
赞同楼上的
2009-11-17 11:09
快速回复:急!!!用继承写了个小程序,请高手们看看错在哪里
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035030 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved