| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 440 人关注过本帖
标题:请教 继承的问题
只看楼主 加入收藏
飞扬冲天
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:86
专家分:122
注 册:2011-10-21
结帖率:66.67%
收藏
 问题点数:0 回复次数:2 
请教 继承的问题
想问一下,为什么多态继承必须要求有默认构造函数啊?

#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

class person
{
public:
    person()
    {
        cout<<endl<< "person construct!" <<endl;
    }
    virtual void display() = 0;
protected:
    int id;
    string name;
};

class student: virtual public person
{
public:
    student(){}    //为什么一定要有个默认构造函数啊?否这就报错...?
    student(int pid, string pname, int pnum, float pscore)
    {
        cout<<endl<< "student constructor";
        id = pid;
        name = pname;
        num = pnum;
        score = pscore;
    }
    void display()
    {
        cout<<endl<<"ID: "<<id;
        cout<<endl<<"Name: "<<name;
        cout<<endl<<"Num: "<<num;
        cout<<endl<<"score: "<<score;
    }
protected:
    int num;
    float score;
};

class teacher: virtual public person
{
public:
    teacher(){}   //还有这里?
    teacher(int pid, string pname, string pjob, string pdep)
    {
        cout<<endl<< "teach constructor";
        id = pid;
        name = pname;
        job = pjob;
        dep = pdep;
    }
    void display()
    {
        cout<<endl<<"ID: "<<id;
        cout<<endl<<"Name: "<<name;
        cout<<endl<<"Job: "<<job;
        cout<<endl<<"Department: "<<dep;
    }
protected:
    string job;
    string dep;

};


class graduate_student: virtual public student,virtual public teacher
{
public:
    graduate_student(int pid, string pname, int pnum, string pjob)
    {
        cout<<endl<<"graduate construct";
        id = pid;
        name = pname;
        num = pnum;
        job = pjob;
    }
    void display()
    {
        cout<<endl<<"ID: "<<id;
        cout<<endl<<"Name: "<<name;
        cout<<endl<<"Num: "<<num;
        cout<<endl<<"Job: "<<job;
    }
};

int main()
{
    student s1(2011201,"jack",1,100);   
    s1.display();
    teacher t1(1022,"john","teacher","school");
    t1.display();
    graduate_student gs1(13561,"xue",23,"techer student");
    gs1.display();
    getch();
    return 0;
}

[ 本帖最后由 飞扬冲天 于 2012-4-25 21:36 编辑 ]
搜索更多相关主题的帖子: 继承 void display include person 
2012-04-25 21:28
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:0 
为什么多态继承必须要求有默认构造函数啊?
------ 不需要呀

student(){}    //为什么一定要有个默认构造函数啊?否这就报错...?
------ 因为graduate_student的构造函数调用了student的默认构造函数
你可以不让它调用默认构造函数,比如
graduate_student(int pid, string pname, int pnum, string pjob) : student(pid,pname,pnum,0) // 逻辑对不对我不管,我说的只是语法
{


2012-04-26 08:22
飞扬冲天
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:86
专家分:122
注 册:2011-10-21
收藏
得分:0 
回复 2楼 rjsp
嗯,明白了...第一次看C++,我只是过了一次..现在回过头来发现好多东西理解的不透彻
谢谢了
2012-04-26 16:49
快速回复:请教 继承的问题
数据加载中...
 
   



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

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