C++ Class Inheritance with private members

Introduction

The derived class cannot access private members of a base class:

class MyBaseClass 
{ 
private: /*w  w w . ja  v  a2  s .co  m*/
    char c; 
    int x; 
}; 

class MyDerivedClass : public MyBaseClass 
{ 
    // c and x NOT accessible here 
}; 

int main() 
{ 
    MyDerivedClass o; 
    o.c = 'a';    // Error, not accessible to object 
    o.x = 123;    // error, not accessible to object 
} 



PreviousNext

Related