Keep the private on your own : Private « Class « C++






Keep the private on your own

   
#include <iostream>
#include <string.h>
using namespace std;
class Base {
 public:
   Base(char *message) { strcpy(Base::message, message); }; 
   void show_base(void) { cout << message << endl; };
 private:
   char message[256];
};

class Derived: public Base {
 public:
   Derived(char *dmsg, char *bmsg) : Base(bmsg) {
     strcpy(message, dmsg); 
   };
   void show_derived(void) 
   { 
   cout << message << endl; 
     show_base(); 
   };
 private:
   char message[256];
};

int main(void)
{
   Base some_base("This is a base");
   Derived some_derived("Derived message", "Base message");

   cout << "The size of the base class is " << sizeof(some_base) << " bytes" << endl;
   cout << "The size of the derived class is " << sizeof(some_derived) << " bytes" << endl;
}
  
    
    
  








Related examples in the same category

1.Private and protected member variablesPrivate and protected member variables
2.Private and public variables and methodsPrivate and public variables and methods
3.Class combination with private fields
4.Private and public sections
5.Setting member access levels
6.Use public methods to access private fields