Question

In: Computer Science

Class object in C++ programming language description about lesson inheritance example.

Class object in C++ programming language description about lesson inheritance example.

Solutions

Expert Solution

#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() {

        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;

}


Related Solutions

Class object in C++ programming language description about lesson inheritance example.
Class object in C++ programming language description about lesson inheritance example.
Class object in C++ programming language description about lesson inheritance multi level example.
Class object in C++ programming language description about lesson inheritance multi level example.
Class object in C++ programming language description about lesson Overloading function example.
Class object in C++ programming language description about lesson Overloading function example.
Class object in C++ programming language description about lesson copy constructor example.
Class object in C++ programming language description about lesson copy constructor example.
Class object in C++ programming language description about lesson static variable example.
Class object in C++ programming language description about lesson static variable example.
Class object in C++ programming language description about lesson base class and derived class example.
Class object in C++ programming language description about lesson base class and derived class example.
Class object in C++ programming language description about lesson unary overloading function example.
Class object in C++ programming language description about lesson unary overloading function example.
Code in C++ programming language description about chose favorite fruit and wage color of fruit in lesson Class object (Multiple class).
Code in C++ programming language description about chose favorite fruit and wage  color of fruit in lesson Class object (Multiple class).
Code in C++ programming language description about lesson Calculator (multiple, sum, dived, sub) example.
Code in C++ programming language description about lesson Calculator (multiple, sum, dived,  sub) example.
Code in C++ programming language description about read and write data to memory example.
Code in C++ programming language description about read and write data to memory example.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT