Q:

C++ program to demonstrate use of protected data members in inheritance.

belongs to collection: C++ programs on various topics

0

C++ program to demonstrate use of protected data members in inheritance.

 

All Answers

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

C++ program (Code Snippet) – Demonstrate use/access of Protected Data Member inside Derived class’s Public Member Function using C++ Inheritance

Let’s consider the following example:

/*C++ program to demonstrate use of 
protected data members in inheritance*/

#include <iostream>

using namespace std;

//class definition
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(){
	//creating object of B (derieved class)
	B objB;
	//get values of a,b and p
	objB.get_a(10);
	objB.get_b(20);
	objB.get_p(30);
	//print values of a,b and p
	objB.put_a();
	objB.put_b();
	objB.put_p();
	
	return 0;
}

Output

            a=10
            b=20
            p=30

 

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

total answers (1)

C++ programs on various topics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ program to access public data member inside ma... >>
<< C++ program to demonstrate calling of private memb...