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

派生的类为什么不能访问自己的私有成员啊

yang0401 发布于 2012-05-08 09:38, 373 次点击
程序代码:
#include <iostream.h>
enum br {aa,bb,cc,dd,ee,ff,gg};
class mammal
{
public:
    mammal();
    mammal(int i);
    ~mammal();
    int getage(){return age;}
    void setage(int j){age = j;}
    int getwidth(){return width;}
    void setwidth(int w){width=w;}
protected:
    int age;
    int width;
};
class dog:public mammal
{
    dog();
    dog(int itsage);
    dog(int itsage,int itswidth);
    dog(int itsage,br hh);
    dog(int itsage,int itswidth ,br hh);
    ~dog();
    br getbr(){return mm;}
    void setbr(br mmm){mm=mmm;}
private:
    br mm;
};
mammal::mammal():
age(1),
width(50)
{
    cout << "初始化\n";
}
mammal::mammal(int i):
age(i),
width(5)
{
    cout << "初始化年龄\n";
}
mammal::~mammal()
{
}
dog::dog():
mammal(),
mm(bb)
{
}
dog::dog(int itsage):
mammal(itsage),
mm(bb)
{
}
dog::dog(int itsage,int itswidth):
mammal(itsage),
mm(bb)
{
    width=itswidth;
}
dog::dog(int itsage,int itswidth ,br hh):
mammal(itsage),
mm(hh)
{
    width=itswidth;
}
dog::dog(int itsage,br hh):
mammal(itsage),
mm(hh)
{
}
dog::~dog()
{
}
int main()
{
    mammal p(50);
    cout << p.getage() << "\n" << p.getwidth()<< endl;
    p.setage(03);
    p.setwidth(06);
    cout << p.getage() << "\n" << p.getwidth()<< endl;
    dog p1;
    cout << p1.getbr() << endl;


    return 0;
}






一直提示dog不能访问私有成员
4 回复
#2
yang04012012-05-08 09:43
--------------------Configuration: Cpp1 - Win32 Debug--------------------
Compiling...
Cpp1.cpp
H:\文档\Cpp1.cpp(82) : error C2248: 'dog::dog' : cannot access private member declared in class 'dog'
        H:\文档\Cpp1.cpp(19) : see declaration of 'dog::dog'
H:\文档\Cpp1.cpp(82) : error C2248: 'dog::~dog' : cannot access private member declared in class 'dog'
        H:\文档\Cpp1.cpp(24) : see declaration of 'dog::~dog'
H:\文档\Cpp1.cpp(83) : error C2248: 'getbr' : cannot access private member declared in class 'dog'
        H:\文档\Cpp1.cpp(25) : see declaration of 'getbr'
执行 cl.exe 时出错.

Cpp1.obj - 1 error(s), 0 warning(s)
#3
唯我独魔2012-05-08 10:37
程序代码:
class dog:public mammal
{
public:
    dog();
    dog(int itsage);
    dog(int itsage,int itswidth);
    dog(int itsage,br hh);
    dog(int itsage,int itswidth ,br hh);
    ~dog();
RT,缺了个public

[ 本帖最后由 唯我独魔 于 2012-5-8 10:40 编辑 ]
#4
yang04012012-05-08 11:00
谢谢,我好粗心啊
#5
passionkk2012-05-09 10:37
。。。
1