1 / 16

INHERITANCE IN C++

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.

yovela
Télécharger la présentation

INHERITANCE IN C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. 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.

  4. 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 

  5. 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.

  6. 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 • };

  7. 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 (); • };

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

  10. 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.

  11. 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

  12. 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 • … • };

  13. 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 );

  14. 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.

  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 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:

  16. THANK YOU

More Related