Saturday, May 29, 2010

INHERITANCE REVISITED


Types of Inheritance:--
1. Single
2. Multi-level ( class 1 derived to class 2 and class 2 derived to class 3)
3. Multiple ( class 3 derived from class 1 and class 2 )
4. Hierarchy type ( class 1 derives to class 2,3,4 )
5. Hybrid  ( deadly diamond )


eg,

class Derived-Class : access-specifier Base-class


Case 1 =
When the access specifier for a base class is public, all public members of the base become public members of the derived class, and all protected members of the base become protected members of the derived class. In all cases, the base's private elements remain private to the base and are not accessible by members of the derived class

Case 2 =
When the base class is inherited by using the private access specifier, all public and protected members of the base class become private members of the derived class
When a base class' access specifier is private, public and protected members of the base become private members of the derived class. This means that they are still accessible by members of the derived class but cannot be accessed by parts of your program that are not members of either the base or derived class.
Case 3 =
Inheritance of Protected members of a class, they behave same as private members of the class i.e. they cant be accessed by the member functions of the class only and cannot be accessed by the non-member functions outside of the class.
But the difference between the PRIVATE and PROTECTED members is this -- private members of the class when derived they cannot be accessed by derived class but protected members are accessed through derived class.
Case 4 =
When we inherit a base class as protected(access specifier). When this is done, all public and protected members of the base class become protected members of the derived class.

Multiple inheritance ( multiple base class - single derived class )
// Inherit multiple base classes.
syntex == class derived: public base1, public base2

Constructor and Destructor calls in inheritance:-
Constructors are executed in their order of derivation. Destructors are executed in reverse order of derivation.
Passing Parameters to Base-Class Constructors:-
#include
using namespace std;
  
class base {
protected:
  int i;
public:
  base(int x) { i=x; cout << "Constructing base\n"; }
  ~base() { cout << "Destructing base\n"; }
};
  
class derived: public base {
  int j;
public:
  // derived uses x; y is passed along to base.
  derived(int x, int y): base(y)     ===================> base class's paramter has to be passed in derived class's constructor even though it might not use it.
    { j=x; cout << "Constructing derived\n"; }
  
  ~derived() { cout << "Destructing derived\n"; }
  void show() { cout << i << " " << j << "\n"; }
};
  
int main()
{
  derived ob(3, 4);
  
  ob.show(); // displays 4 3
  
  return 0;
}

Special Case of accessing the public member of base class derived as privately making that member private for that derived class:
class base {
public:
  int j; // public in base
};
  
// Inherit base as private.
class derived: private base {
public:
  
  // here is access declaration
  base::j; // make j public again   //using this syntax base::member;
  .
  .
  .
};

Virtual base class ( deadly diamond -- hybrid inheritance )
// This program uses virtual base classes.
#include
using namespace std;
  
class base {
public:
  int i;
};
  
// derived1 inherits base as virtual.
class derived1 : virtual public base {
public:
  int j;
};
  
// derived2 inherits base as virtual.
class derived2 : virtual public base {
public:
  int k;
};
  
/* derived3 inherits both derived1 and derived2.
   This time, there is only one copy of base class. */
class derived3 : public derived1, public derived2 {
public:
  int sum;
};
  
int main()
{
  derived3 ob;
  
  ob.i = 10; // now unambiguous
  ob.j = 20;
  ob.k = 30;
  
  // unambiguous
  ob.sum = ob.i + ob.j + ob.k;
  
  // unambiguous
  cout << ob.i << " ";
  
  cout << ob.j << " " << ob.k << " ";
  cout << ob.sum;
  
  return 0;
}






No comments:

Post a Comment