#include<iostream> |
|
10分 |
#include <iostream> #include <cstdio> using namespace std; class A { public: void get(){x=3,y=4,z=5;} int x; int fun2(){return z;} protected: int y; private: int z; }; class B:public A { public: int fun1(){return y;} }; int main(int argc, char *argv[]) { B b; b.get(); cout<<b.x<<endl; b.fun1(); b.fun2(); printf("%p\n", &B::fun1); printf("%p\n", &B::fun2); return 0; } |
5分 |
cout<<b.fun1<<endl;
fun1是函数不是成员变量啊 |
5分 |
函数调用得使用()
cout << b.fun1() << endl; cout << b.fun2() << endl; |
非常感谢 恍然大悟
|
|
如果换成私有继承呢如何访问基类?
#include<iostream> using namespace std; class A { public: void get(){x=3, y=4,z=5;} int x; protected: int y; private: int z; }; class B:private A { public: int fun1(){return x;} int fun2(){return y;} }; void main() { B b; b.fun1(); cout<<b.fun1()<<endl; b.fun2(); cout<<b.fun2()<<endl; } |