Saturday, September 17, 2011

Derived class cannot access private area of base class

example:



#include <iostream>

using namespace std;

class BASE{
public:
void doit();
int a;
protected:
int b;
private:
int c;
};

class DERIVED:public BASE{
public:
void doit();
};

void BASE::doit(){
cout << "BASE::doit()" << endl;
cout << a << endl;
cout << b << endl;
cout << c << endl;
};

void DERIVED::doit(){
cout << "DERIVED::doit()" << endl;
BASE::doit();
cout << "---" << endl;
cout << a << endl;
cout << b << endl;
cout << c << endl; //ERROR: cannot access private of base class

}

int main()
{

BASE base;
base.doit();
DERIVED derived;
derived.doit();
return 0;
}





No comments:

Post a Comment