Inheritance


  • Reusability is an important feature of OOP.
  • It is always nice if we could reuse something that already exists rather than trying to create the same all over again.
  • It would not only save time and money but also increases the productivity.
  • For instance, the reuse of a class that already been tested, debugged and used many times can save us the effort of developing and testing the same again.
  • C++ supports the concept of reusability.
  • The C++ classes can be reused in several ways.
  • Once a class has been written and tested, it can be adapted by other programmers to suit their requirements.
  • This is basically done by creating new classes, reusing the properties of the existing ones.
  • The mechanism of deriving a new class from an old class is called inheritance.
  • The old class is referred to as the base class and the new one is called the derived class.

    Derived Class and Base Class

  • A derived class is like an ordinary class, except that its definition is based on one or more existing classes, called base classes.
  • A derived class can share selected properties (function as well as data members) of its base classes, but makes no changes to the definition of any of its base classes.
  • A derived class can itself be the base class of another derived class.
  • The inheritance relationship between the classes of a program is called a class hierarchy.
  • A derived class is also called a subclass, because it becomes a subordinate of the base class in the hierarchy.
  • Similarly, a base class may be called a superclass, because from it many other classes may be derived.
    Syntax for derive class is
        class derived_c1ass_name: public/private/protected 
                                            base_class_name
    
    We will define two classes counter and countdown
    1.	class counter		//base class
    2.	{ protected: unsigned int count;	//Note: not private
    3.	public : counter () {count = 0;}
    4.	counter (int c) {count = c;}
    5.	int getcount() {return count;}
    6.	counter operator ++ () { count++; return counter(count); }
    7.	};
    8.	class countdown : public counter	// Derived class
    9.	{ public: counter operator -- ()
    10.	{count--; return counter(count); }
    11.	};
    12.	void main()
    13.	{ countdown c1;
    14.	cout << "\n c1 =" << c1.getcount()
    15.	c1++; c1++; c1++;
    16.	cout << "\n c1 = " << c1.getcount();
    17.	c1--; c1--;
    18.	cout << "\n c1 = " << c1.getcount();
    19.	}
    
  • In line no.- 8. A derived class header includes the base classes from which it is derived. A colon separates the two. Here, counter is specified to be the base class from which countdown is derived. The keyword public before counter specifies that counter is used as a public base class.

    Private, Public, and Protected Base Class

  • A base class may be specified to be private, public, or protected. Unless so specified, the base class is assumed to be private:
    class A
    {
     private: int x; void Fx (void);
     public: int y; void Fy (void); 
     protected: int z; void Fz (void);
    };
    1. class B : A { }; //A is a private base class of B
    2. class C : private A { }; //A is a private base class of C
    3. class D : public A { }; //A is a public base class of D
    4. class E : protected A { }; //A is a protected base class of E
    
    The behavior of these is as follows for line
    1 & 2: All the public and protected member function of base class A become private members of the derived class B. So y, Fy, z, and Fz
           all become private members of B and C.
    3: All public members of a public base class become public in derived class. All protected member of public base class become protected 
       in derived class. So, y and Fy become public members of D, and z and Fz become protected members of D.
    4: The public and protected members of a protected base class A become protected members of the derived class. So, y, Fy, z, and Fz
       become protected members of E.
         It is also possible to individually exempt a base class member from the access changes specified by a derived class, so that it 
    retains its original access characteristics. To do this, the exempted member is fully named in the derived class under its original
    access characteristic.  For example:
    class C : private A {
    //…
    public: A::Fy; 		//makes Fy a public member of C
    protected: A::z; 	//makes z a protected member of C
    };
    


Operator Overloading<< Previous

Next >> Constructors and Destructors

Our aim is to provide information to the knowledge seekers.


comments powered by Disqus


Footer1