Upload
fallee31188
View
425
Download
0
Embed Size (px)
Citation preview
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.
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.
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.
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.
Types of Polymorphism:
C++ provides three different types of polymorphism.
Virtual functions Function name overloading Operator overloading
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.
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.
Member functions
(constructor) Construct stackempty Test whether container is
emptysize Return sizetop Access next elementpush Add elementpop Remove element
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.
Member functions
(constructor) Construct queueempty Test whether container is
emptysize Return sizefront Access next elementback Access last elementpush Insert elementpop Delete next element