1 / 29

Inheritance

Inheritance. Presented By : Mr. Paritosh Srivastava PGT (CS) Kendriya Vidyalaya Joshimath. What is Inheritance ?. It is a feature of OOPS in which one class can inherit the properties from other class. Why Inheritance ???. Capability to express the inheritance relationship.

efocht
Download Presentation

Inheritance

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 Presented By : Mr. Paritosh Srivastava PGT (CS) Kendriya Vidyalaya Joshimath

  2. What is Inheritance ? It is a feature of OOPS in which one class can inherit the properties from other class.

  3. Why Inheritance ??? • Capability to express the inheritance relationship. • Code Reusability • Transitive Nature of Inheritance.

  4. Types of Inheritance • Single Inheritance • Multiple Inheritance • Hierarchical Inheritance • Multilevel Inheritance • Hybrid Inheritance

  5. Employee Manager Single Inheritance Class Manager : visibility mode Employee { };

  6. Multiple Inheritance Class C : v_mode A , v_mode B { Data Members of Derived Class }; A B C

  7. Multilevel Inheritance Vehicles Class Automobiles : v_mode Vehicles { }; Class Cars : v_mode Automobiles { }; Automobiles Cars

  8. Hierarchical Inheritance Class B : vis_mode A { }; Class C : vis_mode A { }; Class D : vis_mode A { }; A B D C

  9. Hybrid Inheritance Manager Supervisor Employee Salaried Wages

  10. What is Base Class??? The main class from which the new class is derived is known as Base Class In the example shown before VEHICLES class is a Base Class

  11. What is Derived Class??? The new class created is called a Derived class. In the example shown beforeAUTOMOBILES Class was derived class as it was derived from VEHICLES class and CARSClass is also derived class as it is derived from AUTOMOBILES Class.

  12. What can be Inherited ??? • Data Members • Member Functions

  13. Few Exceptions ... • The constructor and destructor of a base class are not inherited • The assignment operator is not inherited • The Friend functions and friend classes of the base class are also not inherited.

  14. Visibility Modes It controls the access-specifier to be for inheritable members of Base Class , In the Derived class.

  15. Public Visibilty Mode Public derivation means that the derived class can access the public and protected members of the base class but not private members of the base class. (Public Derivation does not changes the access specifiers for inherited members in the derived class)

  16. Publicly Derived Class Base Class Class derived : public X { Private: int a; void init(void); Public: int b; void read_data(void); Protected: int c; void write_data(void); }; Class x { Private: Int aa; Void abc(); Public: int bb; void disp(); Protected: int cc; void getdata(); };

  17. Derived Class Base Class a Init() aa Abc() Private Section Private Section bb Disp() bb Disp() b Read_data() Public Section Public Section cc Getdata() Protected Section Getdata() cc Protected Section Write_data() c

  18. Private Visibilty Mode Private Derivation means the derived class can access the public and private members of the base class privately. With privately derived class the public and protected data members of the base class becomes private members of the derived class.

  19. Publicly Derived Class Base Class Class derived : private X { Private: int a; void init(void); Public: int b; void read_data(void); Protected: int c; void write_data(void); }; Class x { Private: Int aa; Void abc(); Public: int bb; void disp(); Protected: int cc; void getdata(); };

  20. Derived Class Base Class a Init() aa Abc() bb Disp() Private Section cc Getdata() Private Section bb Disp() b Read_data() Public Section Public Section cc Getdata() c Write_data() Protected Section Protected Section

  21. Protected Visibility Mode The protected derivation means that the derived class can access the public and private members of the class protectedly.With protectedly derived class , the public and protected members of the base class become protected members of the derived class. The protected derivation also inherits public and protected members of the base class and makes them protected in the derived class.

  22. Publicly Derived Class Base Class Class derived : protected X { Private: int a; void init(void); Public: int b; void read_data(void); Protected: int c; void write_data(void); }; Class x { Private: Int aa; Void abc(); Public: int bb; void disp(); Protected: int cc; void getdata(); };

  23. Derived Class Base Class a Init() aa Abc() Private Section Private Section bb Disp() b Read_data() Public Section bb Write_data() cc Getdata() c Disp() Getdata() cc Protected Section

  24. Inheritance of Constructors and Destructors When an object of derived class is created , first the base class constructor is invoked , followed by the derived class constructor . When an object of a derived class expires , first the derived destructor is invoked followed by the base class destructor.

  25. Void main() { manager m1; // declaring object of derived class } Class : Employee Public: Employee(); Order of Invocation First base class constructor will be invoked M1::employee(); Then derived class constructor will be invoked M1::manager(); Class : Manager Public: Manager();

  26. Virtual Base Class When two or more objects are derived from a common base class we can prevent multiple copies of base class from being present in an object derived from those objects by declaring the base class as “VIRTUAL” when it is inherited.

  27. Base D1 D2 D3

  28. Containership When a class contains objects of other class types as its member , it is referred to as Containership or Containment or Aggregation.

More Related