C++ 高手进

[复制链接]
查看11 | 回复2 | 2009-3-19 21:20:05 | 显示全部楼层 |阅读模式
#include
class A
{protected:
int a,b;
public:
A(){a=0;b=0;};
A(int x,int y):a(x),b(y){};
void seta();
};
class B: public A
{private:
int c;
public:
B(){a=0;b=0;c=0;};
B(int x,int y,int z):a(x),b(y),c(z){};
void setb();
};
void A::seta()
{int x,y;
cin>>x>>y;
a=x;
b=y;
}
void B::setb()
{int x,y,z;
cin>>x>>y>>z;
a=x;
b=y;
c=z;
}
D:\我的文档\桌面\新建文件夹\Cpp2.cpp(15) : error C2614: 'B' : illegal member initialization: 'b' is not a base or member
D:\我的文档\桌面\新建文件夹\Cpp2.cpp(15) : error C2614: 'B' : illegal member initialization: 'a' is not a base or member
不能继承 a,b 数据成员吗?

回复

使用道具 举报

千问 | 2009-3-19 21:20:05 | 显示全部楼层
你用这个class B: public A{private: int c;public: B(){a=0;b=0;c=0;}; B(int x, int y, int z):A(x,y),c(z){}; void setb();};...
回复

使用道具 举报

千问 | 2009-3-19 21:20:05 | 显示全部楼层
再去看一下派生的规定,b和a是类A的private成员,B类当然不能用,除非通过A对象的public方法。...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行