| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 828 人关注过本帖
标题:继承与派生
取消只看楼主 加入收藏
亦尘
Rank: 1
等 级:新手上路
帖 子:21
专家分:3
注 册:2011-11-16
结帖率:55.56%
收藏
已结贴  问题点数:20 回复次数:2 
继承与派生
求教高手啊,继承不是不能继承基类的构造函数吗???可是我的程序竟然继承啦???求解啊
#include<iostream.h>
#include<string.h>

//基类person
class person
{
protected:
    char name[10];
    char sex[4];
    int age;
public:
    person(){}
    ~person(){}
    void show(){}
};





//派生student类
class student:virtual public person
{
protected:
    char speciality[10];  //学生的专业
    int num;             //学生的学号
public:
    student();
    ~student();
    void show();
};

//student类构造函数的实现
student::student()
{
    cout<<"请输入学生的信息:"<<endl;
    cout<<"学号:";
    cin>>num;
    cout<<"姓名:";
    cin>>name;
    cout<<"性别:";
    cin>>sex;
    cout<<"年龄:";
    cin>>age;
    cout<<"专业:";
    cin>>speciality;
}

//析构函数
student::~student()
{
}

//显示函数的实现
void student::show()
{
    cout<<"学生的信息如下:"<<endl;
    cout<<"学号"<<"   "<<"姓名"<<"   "<<"性别"<<"   "<<"年龄"<<"   "<<"专业"<<endl;
    cout<<num<<"   "<<name<<"   "<<sex<<"   "<<age<<"   "<<speciality<<endl;
}








//teacher类
class teacher:virtual public person
{
protected :
     char department[20];
public:
    teacher();
    ~teacher();
    void show();
};

//teacher类构造函数的实现
teacher::teacher()
{
    cout<<"请输入老师的信息:"<<endl;
    cout<<"姓名:";
    cin>>name;
    cout<<"性别:";
    cin>>sex;
    cout<<"年龄:";
    cin>>age;
    cout<<"院系:";
    cin>>department;
}


//析构函数的实现
teacher::~teacher()
{
}

//显示函数的实现
void teacher::show()
{
    cout<<"老师的信息如下:"<<endl;
    cout<<"姓名"<<"   "<<"性别"<<"   "<<"年龄"<<"   "<<"院系"<<endl;
    cout<<name<<"   "<<sex<<"   "<<age<<"   "<<department<<endl;
}







//派生类stuteacher
class  stuteacher: public student, public teacher
{
public:
    stuteacher();
    ~stuteacher();
    void show();
};

//stuteacher类构造函数的实现
stuteacher::stuteacher()
{
    cout<<"请输入在读老师的信息:"<<endl;
    cout<<"姓名:";
    cin>>name;
    cout<<"性别:";
    cin>>sex;
    cout<<"年龄:";
    cin>>age;
    cout<<"院系:";
    cin>>department;
    cout<<"专业";
    cin>>speciality;
}


//析构函数的实现
stuteacher::~stuteacher()
{
}


//stuteacher类显示函数的实现
void stuteacher::show()
{
    cout<<"老师的信息如下:"<<endl;
    cout<<"姓名"<<"   "<<"性别"<<"   "<<"年龄"<<"   "<<"院系"<<"   "<<"专业"<<endl;
    cout<<name<<"   "<<sex<<"   "<<age<<"   "<<department<<"   "<<speciality<<endl;
}



void main()
{
    student objs;
    objs.show();
    teacher objt;
    objt.show();
    stuteacher objst;
    objst.show();
}
搜索更多相关主题的帖子: 继承 class include person public 
2011-11-30 13:17
亦尘
Rank: 1
等 级:新手上路
帖 子:21
专家分:3
注 册:2011-11-16
收藏
得分:0 
你用vc6.0运行一下看看结果啊,我最后只想输如stuteacher那个对象的成员啊,可是却每一个都要输入一遍
2011-12-01 22:14
亦尘
Rank: 1
等 级:新手上路
帖 子:21
专家分:3
注 册:2011-11-16
收藏
得分:0 
谢谢给位啦
2011-12-09 20:41
快速回复:继承与派生
数据加载中...
 
   



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

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