Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
// C++ program to demonstrate the working of public inheritance

#include <iostream>
using namespace std;

class Base {
private:
int pvt = 1;

protected:
int prot = 2;

public:
int pub = 3;

// function to access private member
int getPVT() {
return pvt;
}
};

class PublicDerived : public Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
};

int main() {
PublicDerived object1;
cout << "Private = " << object1.getPVT() << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.pub << endl;
return 0;
}

C++
// 2nd program

#include <iostream>
using namespace std;

class Base {
private:
int pvt = 1;

protected:
int prot = 2;

public:
int pub = 3;

// function to access private member
int getPVT() {
return pvt;
}
};

class ProtectedDerived : protected Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}

// function to access public member from Base
int getPub() {
return pub;
}
};

int main() {
PublicDerived object1;
cout << "Private = " << object1.getPVT() << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.pub << endl;
return 0;
}


What I have tried:

(Nothing, apparently. The two code blocks above is the only content provided by the OP.)
Posted
Updated 18-Dec-22 23:20pm
v3

In the call:
C++
cout << "Private = " << object1.getPVT() << endl;

You are calling the public method getPVT which returns the value of the private variable. In the second program (I assume) you are trying to access the private variable pvt directly, which is not allowed.
 
Share this answer
 
Comments
Shivam Siddharth 19-Dec-22 5:22am    
in both programs same thing is being done, private member is accessed with getPvt; in first it can be accessed while in second it cannot be.
Richard MacCutchan 19-Dec-22 8:35am    
You are using PublicDerived in both programs, so your comment is not valid. I suggest you edit your question (use the Improve question link above) and post the actual code you are testing with, and show us the actual results.
Shivam Siddharth 19-Dec-22 9:14am    
program 1 output:
Private = 1
Protected = 2
Public = 3

program 2 output:
g++ /tmp/YAWIi8V4tM.cpp
/tmp/YAWIi8V4tM.cpp: In function 'int main()':
/tmp/YAWIi8V4tM.cpp:34:1: error: 'PublicDerived' was not declared in this scope
34 | PublicDerived object1;
| ^~~~~~~~~~~~~
/tmp/YAWIi8V4tM.cpp:35:25: error: 'object1' was not declared in this scope
35 | cout << "Private = " << object1.getPVT() << endl;
|
Richard MacCutchan 19-Dec-22 9:27am    
You do not have a class named PublicDerived in your second program. Try actually reading your code before you post here.
Shivam Siddharth 19-Dec-22 9:36am    
typing mistake sorry, changed PublicDerived to ProtectedDDerived in 2nd program it is still showing error
"private" means that the variable or method is not accessible outside the class: it is only available to the outside world if it's content is exposed by a public method.

The first example doesn't expose the variable itself: the outside world can do what it likes with it but it won't affect the integer value stored in pvt in the class. You can call your method, change the value it returns, then call your method again and get the value you started with again.

The second example does expose the variable: the outside world can directly alter the variabel content in the class.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900