Home | | Computer Science 11th std | C++: Access control in publicly derived class

Example Program in C++ - C++: Access control in publicly derived class | 11th Computer Science : Chapter 16 : Inheritance

Chapter: 11th Computer Science : Chapter 16 : Inheritance

C++: Access control in publicly derived class

From a publicly derived class, public and protected members of the base class remain public and protected members of the derived class.

Access control in publicly derived class

 

From a publicly derived class, public and protected members of the base class remain public and protected members of the derived class. The public members can accessed by the object of the derived class similar to its own members in public.

 

Illustration 16.9 the publicly inherited derived class

#include<iostream>

#include<string>

using namespace std;

class Employee

{

private:

      char name[50];

      int code;

public:

      void getinfo();

      void dispinfo();

};

class staff: public Employee

{

      private:

      int ex;

      public:

      void getdata();

      void display();

};

void Employee::getinfo()

{

      cout<<"Name:";

      gets(name);

      cout<<"Code:";

      cin>>code;

}

void Employee::dispinfo()

{

      cout<<"Name:"<<name<<endl;

      cout<<"Code:"<<code<<endl;

}

void staff::getdata()

{

      cout<<"Experience:";

      cin>>ex;

}

void staff::display()

{

      cout<<"Experience:"<<ex<<" Years"<<endl;

}

int main()

{

      staff s;

      cout<<"Enter data"<<endl;

      s.getinfo();                           //derived member function

      s.getdata();                           //defined member function

      cout<<endl<<endl<<"\tDisplay Data"<<endl;

      s.dispinfo();                         //derived member function

      s.display();//defined member function

      return 0;

}

Output:

Enter data

Name:USHA

Code:1201

Experience: 30

Display Data

Name:USHA

Code:1201

Experience:30 Years

 

In the above program since “staff” is derived publicly even the derived function can be accessed by the object of the class.

We cannot deny access to certain members of a base class when inhering publicly.

 


Tags : Example Program in C++ , 11th Computer Science : Chapter 16 : Inheritance
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 16 : Inheritance : C++: Access control in publicly derived class | Example Program in C++


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.