C++中基类的公有成员与派生类的公有成员和保护成员可以互相访问

即基类中的成员函数是否可以调用派生类中的成员函数 继承方式是公有继承。
如果可以用什么语句?
非常感谢!

使用虚函数.
然后基类指针就可以访问派生类的方法了.

#include <iostream>
using namespace std;

class B
{
protected:
virtual fun1(){
cout << "class B virtual func1"<<endl;
};
int a,b,c,d;
public:
B(){a=1,b=2,c=3,d=4;}
void test(){fun1();};
};

class N :public B
{
public:
N(){};
protected:
virtual fun1(){
cout << "class N virtual func1"<<endl;
};

};

int _tmain(int argc, _TCHAR* argv[])
{
B obj;
obj.test();
N obj1;
B * p = &obj1;
p->test();

return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-04-12
renkeyijichengzhangbei
相似回答