注册 登录
编程论坛 C++教室

求一个多层派生时的构造函数程序问题?

gchs2012 发布于 2011-12-09 21:34, 542 次点击
#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();
}
帮忙各位高手给看看这个程序!谢谢!
2 回复
#2
rjsp2011-12-10 08:10
你得告诉别人,出现了什么问题需要解决
比如说:编译不通过,倒数第三行报错为:error C2248: 'Student2::Student2' : cannot access private member declared in class 'Student2'
#3
lonely_212011-12-10 23:52
吧student2那个构造函数设为public的,默认情况下是私有的,不能直接调用
1