inheritance

10
Inheritance Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the features of the base class and the programmer can choose to add new features specific to the newly created derived class.

Upload: fallee31188

Post on 29-Jun-2015

425 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Inheritance

Inheritance

Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the features of the base class and the programmer can choose to add new features specific to the newly created derived class.

Page 2: Inheritance

Example:

A programmer can create a base class named fruit and define derived classes as mango, orange, banana, etc. Each of these derived classes, (mango, orange, banana, etc.) has all the features of the base class (fruit) with additional attributes or features specific to these newly created derived classes. Mango would have its own defined features, orange would have its own defined features, banana would have its own defined features, etc.

Page 3: Inheritance

Advantages

Reusability: Inheritance helps the code to be reused in many

situations. The base class is defined and once it is compiled, it need not be reworked. Using the concept of inheritance, the programmer can create as many derived classes from the base class as needed while adding specific features to each derived class as needed.

Saves Time and Effort:The above concept of reusability achieved by

inheritance saves the programmer time and effort. Since the main code written can be reused in various situations as needed.

Page 4: Inheritance

PolymorphismPolymorphism is the ability to use an

operator or function in different ways. Polymorphism gives different meanings or functions to the operators or functions. Poly, referring to many, signifies the many uses of these operators and functions. A single function usage or an operator functioning in many ways can be called polymorphism. Polymorphism refers to codes, operations or objects that behave differently in different contexts.

Page 5: Inheritance

Types of Polymorphism:

C++ provides three different types of polymorphism.

Virtual functions Function name overloading Operator overloading

Page 6: Inheritance

Other types of polymorphism

run-time:The run-time polymorphism is implemented with inheritance

and virtual functions.

Compile-time: The compile-time polymorphism is implemented with

templates.

ad-hoc polymorphism:If the range of actual types that can be used is finite and the

combinations must be individually specified prior to use, this is called ad-hoc polymorphism.

parametric polymorphism: If all code is written without mention of any specific

type and thus can be used transparently with any number of new types it is called parametric polymorphism.

Page 7: Inheritance

stacks

Stacks are a type of container adaptors, specifically designed to operate in a LIFO context (last-in first-out), where elements are inserted and extracted only from the end of the container.

Page 9: Inheritance

Queues

Queues are a type of container adaptors, specifically designed to operate in a FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other.