540 likes | 1.18k Views
INHERITANCE IN C++. Amit Saxena PGT(CS) KV Rangapahar Cantt. (Nagaland). Introduction. Reusability is yet another important feature of OOP. C ould use a class already tested, debugged and used many times. S ave our time, reduce our effort. D o not need to start from the scratch.
E N D
INHERITANCE IN C++ Amit Saxena PGT(CS) KV Rangapahar Cantt. (Nagaland)
Introduction • Reusability is yet another important feature of OOP. • Could use a class already tested, debugged and used many times. • Save our time, reduce our effort. • Do not need to start from the scratch. • Inheritance - mechanism of deriving a new class from an old one. • Base class – old class • Derived class – new class
Introduction… • Inherits some or all of the features of the base class. • Single inheritance - Derived class with only one base class. • Multiple inheritance - Derived class with several base classes.
General Form • classderived_class_name : visibility_modebase_class_name { • … • … //members of the derived class • … • }; • Visibility mode - private or public • Optional (default = private) • If visibility mode is private: Private members of derived class Public members of base class
General Form… • If visibility mode is public: Public members of derived class Public members of base class • In both cases private members of base class is not inherited. • Private members of base class cannot be accessed by the derived class.
Derived class Derived class Visibility mode Visibility mode Base class Base class Examples • classABC : private XYZ { //private derivation • members of ABC • }; • classABC : public XYZ { //public derivation • members of ABC • }; • classABC : XYZ { //private derivation by default • members of ABC • };
Public derivation Not inheritable Inheritable Single Inheritance • class B { • private: • int a; • public: • int b; • void get_ab (); • int get_a (); • void show_a (); • }; • class D : public B { • private: • int c; • public: • void mul (); • void display (); • };
Single Inheritance… • class B { • private: • int a; • public: • int b; • void get_ab (); • int get_a (); • void show_a (); • }; • class D : public B { • private: • int c; • public: • void mul (); • void display (); • }; • int b; • void get_ab (); • int get_a (); • void show_a ();
Private derivation Single Inheritance… • class B { • private: • int a; • public: • int b; • void get_ab (); • int get_a (); • void show_a (); • }; • class D : public B { • private: • int c; • public: • void mul (); • void display (); • }; • int b; • void get_ab (); • int get_a (); • void show_a ();
Protected Members • Private members of a base class are not inheritable to a derived class. • If the private members of base class need to be inherited by a derived class • Change the visibility mode of private members to public. • Eliminate the advantage of data hiding. • Soln: Use protected members instead of private members in the base class. • Accessible to immediately derived class.
Base class visibility Derived class visibility Public derivation Private derivation Private Protected Public Visibility of inherited members Not inherited Not inherited Protected Private Public Private
Multiple Inheritance • A class can inherit the members of two or more classes. • classderived_class_name • : visibility_mode base_class_name1, visibility_mode base_class_name2, … { • … • … //members of the derived class • … • };
float x; void get_n (); int m; Multiple Inheritance… • class M { • protected: • int m; • public: • void get_m ( int ); • }; • class N { • private: • int n; • protected: • float x; • public: • void get_n (); • }; • class P : public M, private N { • public: • void display (); • }; private: protected: void get_m ( int );
Virtual Functions • Suppose both base and derived classes have the same function name: • Make the base function as virtual. • C++ determines which function to use at run time. • Based on the type of the object.
Constructor Rules for Derived Classes The default constructor and the destructor of the base class are always called when a new object of a derived class is created or destroyed. class B : public A { public: B (int a) {cout<<“B”<<endl;} }; class A { public: A ( ) {cout<< “A:default”<<endl;} A (int a) {cout<<“A:parameter”<<endl;} }; A:default B B test(1); output: