| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 373 人关注过本帖
标题:派生的类为什么不能访问自己的私有成员啊
只看楼主 加入收藏
yang0401
Rank: 2
等 级:论坛游民
帖 子:84
专家分:57
注 册:2011-5-23
结帖率:69.23%
收藏
已结贴  问题点数:20 回复次数:4 
派生的类为什么不能访问自己的私有成员啊
程序代码:
#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不能访问私有成员
搜索更多相关主题的帖子: age return 
2012-05-08 09:38
yang0401
Rank: 2
等 级:论坛游民
帖 子:84
专家分:57
注 册:2011-5-23
收藏
得分:0 
--------------------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)
2012-05-08 09:43
唯我独魔
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:176
专家分:782
注 册:2011-4-13
收藏
得分:20 
程序代码:
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 编辑 ]
2012-05-08 10:37
yang0401
Rank: 2
等 级:论坛游民
帖 子:84
专家分:57
注 册:2011-5-23
收藏
得分:0 
谢谢,我好粗心啊
2012-05-08 11:00
passionkk
Rank: 1
等 级:新手上路
帖 子:3
专家分:5
注 册:2010-6-29
收藏
得分:0 
。。。
2012-05-09 10:37
快速回复:派生的类为什么不能访问自己的私有成员啊
数据加载中...
 
   



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

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