Presentation is loading. Please wait.

Presentation is loading. Please wait.

| Website for students | VTU NOTES

Similar presentations


Presentation on theme: "| Website for students | VTU NOTES"— Presentation transcript:

1 www.bookspar.com | Website for students | VTU NOTES
Inheritance Inheritance – most important and a useful feature of OOPs supported by C++

2 Inheritance – process of creating new class from the existing class.
| Website for students | VTU NOTES Inheritance – process of creating new class from the existing class. New class is called the Derived class / Child class /subclass Existing class is called the Base class / Parent class / Super class. Derived Class derives or inherits the features of existing class. It contains features of existing class and some of its own features. Base class is the original class that remains unchanged and features of exising class is fully or partially inherited.

3 Syntax for derivation :
| Website for students | VTU NOTES Syntax for derivation : class <name of derived class> : <access specifier> <name of base class> { /*definition of derived class*/ }

4 www.bookspar.com | Website for students | VTU NOTES
Suppose class A already exists. Then a class B is derived from class A as follows Class B : public A { /*new features of class B*/ }; Public access specifier is used in the foregoing example

5 www.bookspar.com | Website for students | VTU NOTES
A pointer from derived class to the base class diagrammatically shows derivation Diagrammatic depiction of Inheritance A B

6 Effects of inheritance
| Website for students | VTU NOTES Effects of inheritance Inheritance affects the size and behavior of the derived class objects in 2 ways An object of derived class contain all data members of the derived class. It contains data members of the base class also. Hence an object of derived class will always be larger than object of base class.

7 www.bookspar.com | Website for students | VTU NOTES
With respect to an object of the derived class, we can call public member functions of the derived class in any global non-member function. However we can call public member functions of the base class also with exceptions.

8 www.bookspar.com | Website for students | VTU NOTES
An object of derived class will contain the data members of the base class and data members of the derived class. Hence the size of object of derived class = sum of the sizes of data members of the base class + sum of sizes of the data members of the derived class.

9 www.bookspar.com | Website for students | VTU NOTES
Inheritance implements an ‘is-a’ relationship. A derived class is a type of the base class like an aircraft (derived class) is a type of vehicle (base class). A class may contain an object of another class / pointer to datastructure that contain set of objects of another class. Such a class is called container class

10 www.bookspar.com | Website for students | VTU NOTES
Eg – An aircraft has 1 engine or an array of engines. Another eg – manager class & employee class. A manager (i.e. an object of the class manager) is an employee (i.e. object of class employee). It has some features that are not possessed by an employee.

11 www.bookspar.com | Website for students | VTU NOTES
Eg – it may have a pointer to an array of employees that report to him. Derived class object is also a base class object shown in the code.

12 www.bookspar.com | Website for students | VTU NOTES
Class Employee { String name; Double basic; Date doj; /*rest of employee class*/ };

13 www.bookspar.com | Website for students | VTU NOTES
Class manager : public employee { employee *list; /*rest of the class manager*/ }


Download ppt "| Website for students | VTU NOTES"

Similar presentations


Ads by Google