Q:

C++ Program to show access to Private Public and Protected using Inheritance

belongs to collection: C++ Inheritance Solved Programs

0

Write a C++ Program to show access to Private Public and Protected using Inheritance. Here’s a Simple C++ Program to show access to Private Public and Protected using Inheritance in C++ Programming Language.

What are Inheritance in C++ ?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

  • Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.
  • When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.
  • The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.

Types of Inheritance : :

There are different types of inheritance : :

 
 
  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid (Virtual) Inheritance

Below is the source code for C++ Program to show access to Private Public and Protected using Inheritance which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

/*  C++ Program to show access to Private Public and Protected using Inheritance */

#include<iostream>
using namespace std;
class Base
{
        private:
             int basedata1 ;
        protected:
             int basedata2 ;
        public:
             int basedata3 ;
/*The member function of a class can directly access all other members (data + methods)  of the same class,  no matter whether the data is public, protected or private */
             void get_base_data()
             {
                cout << "\n Enter basedata1: ";
                cin >> basedata1 ;  // accessible 
                cout << "\n Enter basedata2: ";
                cin >> basedata2 ;  // accessible
                cout << "\n Enter basedata3: ";
                cin >> basedata3 ;  // accessible
             }
void display_base_data()
        {
                cout << "\n basedata1 = " << basedata1;
                cout << "\n basedata2 = " << basedata2;
                cout << "\n basedata2 = " << basedata3;
        }
} ; // End of the class Definition
class Derive : public Base
{
        private:
                int derivedata1 ;
        protected:
                int derivedata2 ;
        public:
                int derivedata3 ;
void get_derive_data()
        {
                cout << "\n Enter basedata1: ";
                cin >> basedata1 ;  //not accessible; private data of base class
                cout << "\n Enter basedata2: "; 
                cin >> basedata2 ;  // accessible; protected data of base class
                cout << "\n Enter basedata3: ";
                cin >> basedata3 ;  // accessible; public data of the base class
        
                cout << "\n Enter derivedata1: "; 
                cin >> derivedata1 ;  // accessible
                cout << "\n Enter derivedata2: ";
                cin >> derivedata2 ;  // accessible
                cout << "\n Enter  derivedata3: ";
                cin >> derivedata3 ;  // accessible
        }
                
void display_derive_data()
        {
                cout << "\n basedata1 = " << basedata1;
                cout << "\n basedata2 = " << basedata2;
                cout << "\n basedata2 = " << basedata3;

                cout << "\n derivedata1 = " << derivedata1;
                        out << "\n derivedata2 = " << derivedata2;
                cout << "\n derivedata2 = " << derivedata3;
        }
} ; // End of the sub class definition

int main()
{
        Base bobj ;

        bobj.basedata1 = 10 ; // Not accessible
        bobj.basedata2 = 20 ; // Not accessible
        bobj.basedata3 = 30 ; // Accessible

        bobj.get_base_data();  // accessible
        bobj.display_base_data(); // accessible


        // Cont. on the next slide

Derive  dobj ;
        dobj.basedata1 = 5 ; // Not accessible
        dobj.basedata2 = 6 ; // Not accessible
        dobj.basedata3 = 7 ; // Accessible

        dobj.derivedata1 = 8 ; // Not accessible
        dobj.derivedata2 = 9 ; // Not accessible
        dobj.derivedata3 = 10 ; // Accessible

        dobj.get_base_data() ; // Accessible
        dobj.display_base_data() ;  // Accessible
        dobj.get_derive_data();   // Accessible
        dobj.display_derive_data(); // Accessible
return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Program to Overriding member functions using I... >>
<< C++ Program to find Area of Rectangle using inheri...