Professional Documents
Culture Documents
Inheritance - Lect1
Inheritance - Lect1
Derived Class
Class A – Parent Class
Class B – Child Class (Derived Class)
Derived Class
For Class C, Class A and B are – Parent
Class
Class C – Child Class (Derived Class)
Syntax –
Class derived-class: access-specifier base-class
Access Specifiers with derived Classes
class Animal { };
class Dog : public Animal { };
class Dog: private Animal {};
class Dog: protected Animal {};
The various ways we can derive classes are known as access modes. These access
modes have the following effect:
1. Public – If a derived class is declared in Public mode, then the members of the
base class are inherited by the derived class just as they are.
2. Private – In this case, all the members of the base class become private
members in the derived class.
3. Protected – The public members of the base class become protected members
in the derived class.
- The Private Members are always inherited as Private members.
Polymorphism
Polymorphism in C++
Polymorphism means, having many or multiple forms.
In simple words, we can define polymorphism as the ability of a
message to be displayed in more than one form.
A real-life example of polymorphism, a person at the same time
can have different characteristics.
Like a man at the same time is a father, a husband, an employee.
So the same person posses different behavior in different
situations. This is called polymorphism.
Polymorphism is considered as one of the important features of
Object Oriented Programming.
Types of Polymorphism
1 – Compile Time
2 – Run Time
Compile Time - Function Overloading
Function Overloading:
When there are multiple functions with
same name but different parameters then
these functions are said to be overloaded.
Functions can be overloaded by change in
number of arguments or/and change in
type of arguments.
Compile Time – Operator Overloading
Operator Overloading:
C++ also provide option to overload operators. For
example, we can make the operator (‘+’) for string class
to concatenate two strings.
We know that this is the addition operator whose task is
to add two operands. So a single operator ‘+’ when
placed between integer operands , adds them and when
placed between string operands, concatenates them.
Remember the last lab ? Adding the
Complex Numbers ?
Complex val1(6,1);
Complex val2(2,5);
Complex val3;
val3= val1+val2;
Basic Syntax
Complex Class Operator
& Constructor Overloading
Main Function & Output
Run Time Polymorphism