Presentation is loading. Please wait.

Presentation is loading. Please wait.

INHERITANCE IN C++ Amit Saxena PGT(CS) KV Rangapahar Cantt. (Nagaland)

Similar presentations


Presentation on theme: "INHERITANCE IN C++ Amit Saxena PGT(CS) KV Rangapahar Cantt. (Nagaland)"— Presentation transcript:

1 INHERITANCE IN C++ Amit Saxena PGT(CS) KV Rangapahar Cantt. (Nagaland)

2  Reusability is yet another important feature of OOP. Introduction  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.  Inheritance - mechanism of deriving a new class from an old one.  Base class – old class  Derived class – new class

3  Inherits some or all of the features of the base class. Introduction…  Multiple inheritance - Derived class with several base classes.  Single inheritance - Derived class with only one base class.

4 General Form class derived_class_name : visibility_mode base_class_name { … …//members of the derived class … };  Visibility mode - private or public  Optional (default = private)  If visibility mode is private: Public members of base class  Private members of derived class

5 General Form…  If visibility mode is public: Public members of base class  Public members of derived class  In both cases private members of base class is not inherited.  Private members of base class cannot be accessed by the derived class.

6 Examples 1.class ABC : private XYZ { //private derivation members of ABC }; Derived classVisibility mode Base class 2.class ABC : public XYZ { //public derivation members of ABC }; Derived classVisibility mode Base class 3.class ABC : XYZ { //private derivation by default members of ABC };

7 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 (); }; Public derivation Not inheritable Inheritable

8 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 ();

9 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 (); }; Private derivation int b; void get_ab (); int get_a (); void show_a ();

10  Private members of a base class are not inheritable to a derived class. Protected Members  If the private members of base class need to be inherited by a derived class  Change the visibility mode of private members to public.  Sol n : Use protected members instead of private members in the base class.  Eliminate the advantage of data hiding.  Accessible to immediately derived class.

11 Visibility of inherited members Base class visibility Derived class visibility Public derivationPrivate derivation Private Protected Public Not inherited ProtectedPrivate PublicPrivate

12  A class can inherit the members of two or more classes. Multiple Inheritance class derived_class_name : visibility_mode base_class_name1, visibility_mode base_class_name2, … { … …//members of the derived class … };

13 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: int m; void get_m ( int ); float x; void get_n ();

14  Suppose both base and derived classes have the same function name: Virtual Functions  Make the base function as virtual.  C++ determines which function to use at run time.  Based on the type of the object.

15 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 A { public: A ( ) {cout<< “A:default”<<endl;} A (int a) {cout<<“A:parameter”<<endl;} }; class B : public A { public: B (int a) {cout<<“B”<<endl;} }; B test(1);output: A:default B

16 THANK YOU


Download ppt "INHERITANCE IN C++ Amit Saxena PGT(CS) KV Rangapahar Cantt. (Nagaland)"

Similar presentations


Ads by Google