标题:求一个多层派生时的构造函数程序问题?
只看楼主
gchs2012
Rank: 1
等 级:新手上路
帖 子:5
专家分:4
注 册:2011-10-26
结帖率:0
 问题点数:0 回复次数:2 
求一个多层派生时的构造函数程序问题?
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
    Student(int n,string nam):num(n),name(nam){}
    void display()
    {
        cout<<"num:"<<num<<endl;
        cout<<"name:"<<name<<endl;
    }
protected:
    int num;
    string name;
};

class Student1:public Student
{
public:
    Student1(int n,string nam,int a):Student(n,nam),age(a){}
    void show()
    {
        display();
        cout<<"age:"<<age<<endl;
    }
protected:
    int age;
};

class Student2:public Student1
{
    Student2(int n,string nam,int a,int s):Student1(n,nam,a),score(s){}
    void show_all()
    {
        show();
        cout<<"score:"<<score<<endl;
    }
protected:
    int score;
};

void main()
{
    Student2 stud(10010,"Zhangcan",17,89);
    stud.show_all();
}
帮忙各位高手给看看这个程序!谢谢!
搜索更多相关主题的帖子: public class display include 
2011-12-09 21:34
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:0 
你得告诉别人,出现了什么问题需要解决
比如说:编译不通过,倒数第三行报错为:error C2248: 'Student2::Student2' : cannot access private member declared in class 'Student2'
2011-12-10 08:10
lonely_21
Rank: 5Rank: 5
等 级:职业侠客
威 望:3
帖 子:108
专家分:395
注 册:2011-11-13
得分:0 
吧student2那个构造函数设为public的,默认情况下是私有的,不能直接调用
2011-12-10 23:52



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-357411-1-1.html




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

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