Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Inheritance in C++: Accessing Base Class Members in Derived Classes, Exercises of Object Oriented Programming

An example of inheritance in c++ with different access specifiers (private, protected, public) for base class members. The example demonstrates how derived class objects can access base class members based on their access specifiers. The code includes constructors, destructors, and display functions.

Typology: Exercises

2011/2012

Uploaded on 07/31/2012

netu
netu ๐Ÿ‡ฎ๐Ÿ‡ณ

4.5

(4)

55 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1C:\Documents and Settings\usman.younis\my documents...\Projects\Inheritance\Inheritance\Inheritance.cpp
# include < iostream >
using namespace std ;
class baseClass
{
private :
int priv _ baseInt ;
protected :
int prot _ baseInt ;
public :
int publ _ baseInt ;
baseClass (): priv _ baseInt ( 11 ) , prot _ baseInt ( 12 ) , publ _ baseInt ( 13 )
{
cout << " Im the base class constructor " << endl ;
}
~ baseClass ()
{
cout << " Im the base class destructor " << endl ;
}
void display _ Vars ()
{
cout << " Private Member : " << priv _ baseInt << endl ;
cout << " Protected Member : " << prot _ baseInt << endl ;
cout << " Public Member : " << publ _ baseInt << endl ;
}
};
class pubInherited : public baseClass
{
private :
int priv _ baseInt ;
int priv _ InhrInt ;
protected :
int prot _ baseInt ;
int prot _ InhrInt ;
public :
int publ _ baseInt ;
int publ _ InhrInt ;
pubInherited (): priv _ baseInt ( 101 ) , prot _ baseInt ( 102 ) , publ _ baseInt ( 103 ) ,
priv _ InhrInt ( priv _ baseInt ) , prot _ InhrInt ( prot _ baseInt ) , publ _ InhrInt ( publ _ baseInt )
{
cout << " Im the publicly Inherited class constructor " << endl ;
}
~ pubInherited ()
{
cout << " Im the publicly Inherited class destructor " << endl ;
}
// Base Class function overridden in Inherited
void display _ Vars ()
{
// Inherited Class Data
cout << " Private Member of Inherited : " << priv _ InhrInt << endl ;
cout << " Protected Member of Inherited : " << prot _ InhrInt << endl ;
cout << " Public Member of Inherited : " << publ _ InhrInt << endl ;
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Inheritance in C++: Accessing Base Class Members in Derived Classes and more Exercises Object Oriented Programming in PDF only on Docsity!

#include using namespace std; class baseClass { private: int priv_baseInt; protected: int prot_baseInt; public: int publ_baseInt; baseClass(): priv_baseInt( 11 ), prot_baseInt( 12 ), publ_baseInt( 13 ) { cout<<"Im the base class constructor"<<endl; } ~baseClass() { cout<<"Im the base class destructor"<<endl; } void display_Vars() { cout<<"Private Member : "<<priv_baseInt<<endl; cout<<"Protected Member : "<<prot_baseInt<<endl; cout<<"Public Member : "<<publ_baseInt<<endl; } }; class pubInherited: public baseClass { private: int priv_baseInt; int priv_InhrInt; protected: int prot_baseInt; int prot_InhrInt; public: int publ_baseInt; int publ_InhrInt; pubInherited(): priv_baseInt( 101 ), prot_baseInt( 102 ), publ_baseInt( 103 ), priv_InhrInt(priv_baseInt), prot_InhrInt(prot_baseInt), publ_InhrInt(publ_baseInt) { cout<<"Im the publicly Inherited class constructor"<<endl; } ~pubInherited() { cout<<"Im the publicly Inherited class destructor"<<endl; } //Base Class function overridden in Inherited void display_Vars() { //Inherited Class Data cout<<"Private Member of Inherited: "<<priv_InhrInt<<endl; cout<<"Protected Member of Inherited: "<<prot_InhrInt<<endl; cout<<"Public Member of Inherited: "<<publ_InhrInt<<endl;

//Base Class data overridden in Inherited cout<<"Private Member of Inhr_Base: "<<priv_baseInt<<endl; cout<<"Protected Member of Inhr_Base: "<<prot_baseInt<<endl; cout<<"Public Member of Inhr_Base: "<<publ_baseInt<<endl; //Base Class data //cout<<"Private Member of Base: "<<baseClass::priv_baseInt<<endl; //Inaccessible cout<<"Protected Member of Base: "<<baseClass::prot_baseInt<<endl; cout<<"Public Member of Base: "<<baseClass::publ_baseInt<<endl; } void display_Inherited() { //Inherited Class own function display_Vars(); //Base Class function baseClass::display_Vars(); } }; class privateGChild:private pubInherited { public: privateGChild() { cout<<"I am a Test class, privately Inherited from pubInherited"<<endl; cout<<"I can access (in my Class) : "<<endl; //Inherited Class Data //cout<<"Private Member of Inherited: "<<priv_InhrInt<<endl; //Inaccessible cout<<"Protected Member of Inherited: "<<prot_InhrInt<<endl; cout<<"Public Member of Inherited: "<<publ_InhrInt<<endl; } ~privateGChild() { cout<<"destructed โ€> privateGChild"<<endl; } }; class GGChild_of_Private:public privateGChild { public: GGChild_of_Private() { cout<<"I am a Test class, inherited from a privately Inherited class"<<endl <<"Just to check the private access specifier"<<endl; cout<<"I cant access any thing"<<endl; //Inherited Class Data //cout<<"Private Member of Inherited: "<<priv_InhrInt<<endl; //Inaccessible //cout<<"Protected Member of Inherited: "<<prot_InhrInt<<endl; //Inaccessible //cout<<"Public Member of Inherited: "<<publ_InhrInt<<endl; //Inaccessible } ~GGChild_of_Private() { cout<<"destructed โ€> GGChild_of_Private"<<endl; } };

privateGChild Object 1 ; //I (an Object) can't DIRECTLY access the public data of my base //as it has become private, neither my derived will //be abble to access it //Object 1 .display_Inherited(); protectedGChild Object 2 ; //I (an Object) can't DIRECTLY access the public data of my base //as it has become protected, neither my derived will //be abble to access it //Object 2 .display_Inherited(); }

Untitled

Im the base class constructor

Private Member : 11

Protected Member : 12

Public Member : 13

Public Data of Base Class : 13

Im the base class constructor

Im the publicly Inherited class constructor

Private Member of Inherited: 101

Protected Member of Inherited: 102

Public Member of Inherited: 103

Private Member of Inhr_Base: 101

Protected Member of Inhr_Base: 102

Public Member of Inhr_Base: 103

Protected Member of Base: 12

Public Member of Base: 13

Private Member : 11

Protected Member : 12

Public Member : 13

Private Member of Inherited: 101

Protected Member of Inherited: 102

Public Member of Inherited: 103

Private Member of Inhr_Base: 101

Protected Member of Inhr_Base: 102

Public Member of Inhr_Base: 103

Protected Member of Base: 12

Public Member of Base: 13

Private Member : 11

Protected Member : 12

Public Member : 13

Public Data of Base Class : 13

Im the base class constructor

Im the publicly Inherited class constructor

I am a Test class, privately Inherited from pubInherited

I can access (in my Class) :

Protected Member of Inherited: 102

Public Member of Inherited: 103

Im the base class constructor

Im the publicly Inherited class constructor

I am a Test class, protected Inherited from pubInherited

I can access (in my Class) :

Protected Member of Inherited: 102

Public Member of Inherited: 103

destructed -> protectedGChild

Im the publicly Inherited class destructor

Im the base class destructor

destructed -> privateGChild

Im the publicly Inherited class destructor

Im the base class destructor

Im the publicly Inherited class destructor

Im the base class destructor

Im the base class destructor

Press any key to continue...

Page 1