Computer Training and Examination Cetner
Under the Patronage of
      Online Payment     Online Payment
Your Ad Here

Chapter 9 Visibility modes in Inheritance

Back to -> C++ -Langauge Tutorial Index


Basically there are three types of visibility modes.
		1. Private
		2. Protected 
		3. Public

The visibility modes control the access specifier to be inheritable members of base-class in the derived class

Visibility mode table

Base class member 
public inheritance public
private inheritance private
protected inheritance 	protected

public inheritance  protected
private inheritance private
protected inheritance 	protected

public inheritance  Not inherited
private inheritance  Not inherited
protected inheritance  Not inherited

Derive publicly when the situation wants the class to have all the attributes of the base-class, plus some extra attributes.
Derive privately when the derived class requires to use some attributes of the base-class and these inherited features are intended not to be inherited further.
Derive protectedly when the features are required to be hidden from the outside world and at the same time required to be inheritable.

Accessibility of base-class
1. Knowing when a member function or data member of a base class can be used by objects of the derived class.
2. Class members can always be accessed by member functions within their own class, whether the members are private or public.
3. Objects defined outside the class can access class members only if the members are public.  

With inheritance: 

Derived class members can access members of the base class if its members are public. 

Derived class members cannot access the private members of the base class. 
Accessing base class members
Members of the derived class can access public and protected members; they cannot access the private members of the base class. 
No access to some of the class members. Those members can be put in the private section. 
Allow controlled access by providing some protected members. 
Inheritance does not work in reverse.  

Access rules for Base class members
Access specifer   public    
Accessiblefrom own class   yes   
Accessible fromderived class  yes
Accessible from object outside class   yes

Access specifer   protected    
Accessiblefrom own class   yes   
Accessible fromderived class  yes
Accessible from object outside class  no

Access specifer   private
Accessiblefrom own class   Yes   
Accessible fromderived class  No
Accessible from object outside class   No

EXAMPLE
class abc    //Super class
{
	private:
			 int priv_a;
	protected:
 			 int prot_b;
	public:
 			 int pub_c;
}; 
class xyz : public abc //sub class
{	
		public:
  	void fn();
};
  
class abc    //Super class
{
	private:
			 int priv_a;
	protected:
 			 int prot_b;
	public:
 			 int pub_c;
}; 
class xyz : public abc //sub class
{	
		public:
  	void fn();
};
  
void main()
{

abc o1;     //base class object
o1.priv_a = 1; //Error
o1.prot_b = 1; //Error
o1.pub_c  = 1; //valid

xyz obj1;  //derived class object
obj1.priv_a = 1; //Error
obj1.prot_b = 1; //Error
obj1.pub_c = 1;  //valid

 }
Hybrid Inheritance

Lets consider a situation where all the three kinds of information, namely multilevel , multiple and hierarchical inheritance, are involved.
All the public and protected members of ‘grand-Parent’ are inherited into child twice via two different path.
This means, ‘child’ would have duplicate sets of members inherited from grand-Parent . This introduces ambiguity.

How to avoid ambiguity using Virtual keyword

The duplication of inherited members due to these multiple paths can be avoided by making the common base class as virtual base class 
while declaring the direct or intermediate base classes .
When a class is made a virtual base class , C++ takes necessary care to see that only one copy of that class is inherited .
The keyword virtual and public may be used in either order.

Abstract class
An abstract class is one that is not used to create objects.
An abstract class is designed only to act as a base class ( to be inherited by other classes ).

Constructor and destructor with inheritance

When an object of a derived class is created , the program first calls the constructor for base class, then the constructor for derived class .
Similarly , when an object expires , the program first calls the derived destructor ,if any ,and then the base class destructor .
One important point is that constructors and destructors of base class are not inherited by the derived class.
Whenever the derived class needs to invoke base class’s constructor or destructor ,it can invoke them through explicitly calling them.
As long as no base class constructor takes any arguments , the derived class need not have a constructor function.
But if any base class contains a constructor with one or more arguments, then it is mandatory for the derived class to have a constructor 
and pass the arguments to the base class constructors.
To pass the base class arguments , the constructor of the derived class uses a special argument passing mechanism.

Syntax =>

Derived :: derived (type1 x, type2 y, …)  : base(x,y)
{
		-------
		-------
}


Constructor with virtual base class

The constructor for virtual base classes are invoked before any non virtual base classes.
If there are multiple virtual base classes, they are invoked in the order in which they are declared .
Any non-virtual bases are then constructed before the derived class constructor is executed.


Back to -> C++ Language Tutorial Index

Your Ad Here
© Copyright OBM-INDIA 2008-2013 Designed by OBM-INDIA Privacy Policy | Terms of Use