Q:

C++ Program to Access Protected Data Member using Inheritance

belongs to collection: C++ Inheritance Solved Programs

0

Write a C++ Program to Access Protected Data Member using Inheritance. Here’s a Simple C++ Program to Access Protected Data Member using Inheritance in C++ Programming Language.

All Answers

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

What are Inheritance in C++ ?


  • 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 Access Protected Data Member using Inheritance which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

/*  C++ Program to Access Protected Data Member using Inheritance  */

#include <iostream>

using namespace std;

class A{
        private:
                int a;
        protected:
                int p;
        public:
                void get_a(int a){
                        this->a=a;
                }
                void put_a(){
                        cout<<"a="<<a<<endl;
                }
};
class B: public A{
        private:
                int b;
        public:
                void get_b(int b){
                        this->b=b;
                }
                void get_p(int p){
                        this->p=p;
                }
                void put_b(){
                        cout<<"b="<<b<<endl;
                }               
                void put_p(){
                        cout<<"p="<<p<<endl;
                }               
};
int main(){
        
        B objB;
        
        objB.get_a(10);
        objB.get_b(20);
        objB.get_p(30);
        
        objB.put_a();
        objB.put_b();
        objB.put_p();
        
        return 0;
}

OUTPUT : :


/*  C++ Program to Access Protected Data Member using Inheritance  */

a=10
b=20
p=30

Process returned 0

Above is the source code and output for C++ Program to Access Protected Data Member using Inheritance which is successfully compiled and run on Windows System to produce desired output.

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

total answers (1)

C++ Program for Inheritance Beyond Single Level... >>