In: Computer Science
Class object in C++ programming language description about lesson base class and derived class example.
#include
#include
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
void setPVT(int pv) {
pvt=pv;
}
int getPVT() {//get value by private
return pvt;
}
};
class ProtectedDerived : protected Base {
protected:
// int prot=2;
// int pub=3;
// void setPVT(int);
// int getPVT();
public:
// function to access protected member from Base
int getProt() {
return prot;
}
void set(int pv,int pr, int pu)
{ // set values to pvt, prot, and pub
setPVT(pv);
prot=pr;
pub=pu;
}
void input()
{ // input values to pvt, prot and pub
int pvt; // temporary local variable
cout <<"enter pvt ?"; cin>>pvt;
setPVT(pvt);
cout <<"enter prot?"; cin>>prot;
cout <<"enter pub ?"; cin>>pub;
}
void print()
{
cout <<"private pvt=" << getPVT() < cout <<"protected prot=" << prot < cout <<"public pub =" << pub < } // function to access public member from Base int getPub() { return pub; } int getPvt() { return getPVT(); } }; int main() { ProtectedDerived object1; //cout << "Private cannot be accessed." << endl; cout <<"Private = " << object1.getPvt() < cout <<"Protected = " << object1.getProt() << endl; cout <<"Public = " << object1.getPub() << endl; object1.set(10,20,30); object1.print(); object1.input(); object1.print(); //.. return 0; }