Unit-3
C++
NEXT
Inheritance in C++
• The capability of a class to derive properties and
characteristics from another class is called
Inheritance. Inheritance is one of the most
important feature of Object Oriented
Programming.
Sub Class: The class that inherits properties from
another class is called Sub class or Derived Class.
Super Class:The class whose properties are
inherited by sub class is called Base Class or Super
class.
Why Inheritance is needed
Contd….
Implementation
Example
Visibility Modes of Inheritance
• Public mode: If we derive a sub class from a public base class. Then the
public member of the base class will become public in the derived class
and protected members of the base class will become protected in
derived class. Private members of the base class will never get inherited
in sub class.
• Protected mode: If we derive a sub class from a Protected base class.
Then both public member and protected members of the base class will
become protected in derived class. Private members of the base class
will never get inherited in sub class.
• Private mode: If we derive a sub class from a Private base class. Then
both public member and protected members of the base class will
become Private in derived class. Private members of the base class will
never get inherited in sub class.
Contd….
Demo
Types of Inheritance in C++
Example
Multiple Inheritance
Syntax
Example
Multilevel Inheritance
Example
Hierarchical Inheritance
Example
Hybrid Inheritance
Virtual Base Classes
• An ambiguity can arise when several paths exist
to a class from the same base class. This means
that a child class could have duplicate sets of
members inherited from a single base class.
- C++ solves this issue by introducing a virtual
base class. When a class is made virtual,
necessary care is taken so that the duplication
is avoided regardless of the number of paths
that exist to the child class.
Contd….
• When two or more objects are derived from a
common base class, we can prevent multiple
copies of the base class being present in an
object derived from those objects by declaring
the base class as virtual when it is being
inherited. Such a base class is known as virtual
base class. This can be achieved by preceding
the base class’ name with the word virtual.
Example
Virtual Function in C++
• A virtual function a member function which is
declared within base class and is re-defined
(Overriden) by derived class.When you refer to a
derived class object using a pointer or a reference to
the base class, you can call a virtual function for that
object and execute the derived class’s version of the
function.
• They are mainly used to achieve
Runtime polymorphism
• Functions are declared with a virtual keyword in
base class.
• The resolving of function call is done at Run-time.
Rules for Virtual Functions
• They Must be declared in public section of class.
• Virtual functions cannot be static and also cannot be a friend
function of another class.
• Virtual functions should be accessed using pointer or reference
of base class type to achieve run time polymorphism.
• The prototype of virtual functions should be same in base as
well as derived class.
• They are always defined in base class and overridden in derived
class. It is not mandatory for derived class to override (or re-
define the virtual function), in that case base class version of
function is used.
example
Constructors and Destructor in derived
classes.
• Constructors are called from „top” to
„bottom”: first from the base class, then from
the derived class. If a class inherits from
several classes, their constructors are invoked
in the order they are mentioned in the
inheritance list in the definition of the derived
class.
Constructor and destructor in single
inheritance
• Base class constructors are called first and the
derived class constructors are called next in
single inheritance.
• Destructor is called in reverse sequence of
constructor invocation i.e. The destructor of
the derived class is called first and the
destructor of the base is called next.
Constructor and destructor in single
inheritance
Constructor and destructor in multiple
inheritance
• Constructors from all base class are invoked
first and the derived class constructor is called.
• Order of constructor invocation depends on
the order of how the base is inherited.
Implementation
example
Polymorphism
• Polymorphism means more than one function with same
name, with different working. Polymorphism can be static or
dynamic. In static polymorphism memory will be allocated
at compile-time. In dynamic polymorphism memory will be
allocated at run-time. Both function overloading and
operator overloading are an examples of static
polymorphism. Virtual function is an example of dynamic
polymorphism.
• Static polymorphism is also known as early binding and
compile-time polymorphism.
• Dynamic polymorphism is also known as late binding and
run-time polymorphism.
Types
Function Overloading in C++
• More than one function with same name, with
different signature in a class or in a same scope is
called function overloading. Signature of function
includes :
• Number of arguments
• Type of arguments
• Sequence of arguments
• When we call an overloaded function, the compiler
determines the most appropriate definition to use
by comparing the signature of calling statement
with the signature specified in the definitions.
example
Contd…
C++ Operator Overloading
• Operator overloading is a way of providing
new implementation of existing operators to
work with user-defined data types.
• An operator can be overloaded by defining a
function to it. The function for operator is
declared by using the operator keyword
followed by the operator.
Types of operator overloading
• There are two types of operator overloading in
C++
• Binary Operator Overloading
• Unary Operator Overloading
Overloaded operators are functions with special
names the keyword operator followed by the
symbol for the operator being defined. Like
any other function, an overloaded operator
has a return type and a parameter list.
Overloading Binary Operator
Binary operator is an operator that takes two
operand(variable). Binary operator
overloading is similar to unary operator
overloading except that a binary operator
overloading requires an additional parameter.
• Binary Operators
• Arithmetic operators (+, -, *, /, %)
• Arithmetic assignment operators (+=, -=, *=,
/=, %=)
• Relational operators (>, <, >=, <=, !=, ==)
example
• class Rectangle Rectangle operator+(Rectangle Rec)
{ {
int L,B; Rectangle R;
public: R.L = L + Rec.L;
Rectangle() R.B = B + Rec.B;
{ return R;
L = 0; B = 0; }
} void Display()
Rectangle(int x,int y) {
{ cout<<"\n\tLength : "<<L;
L = x; cout<<"\n\tBreadth : "<<B;
B = y; }
} };
void main() • Output :
{ Rectangle 1 :
Rectangle R1(2,5),R2(3,4),R3; L:2B:5
cout<<"\n\tRectangle 1 : ";
Rectangle 2 :
R1.Display(); cout<<"\n\n\
tRectangle 2 : "; R2.Display(); L:3B:4
R3 = R1 + R2; cout<<"\n\n\ Rectangle 3 :
tRectangle 3 : "; R3.Display(); L:5B:9
}
Overloading Unary Operator
• Unary operator is an operator that takes single
operand(variable). Both increment(++) and
decrement(--) operators are unary operators.
example
class Rectangle • void Display()
{ {
int L,B; cout<<"\n\tLength : "<<L; cout<<"\n\tBreadth : "<<B;
public: }
Rectangle() };
{ void main()
L = 0; {
B = 0; Rectangle R;
} cout<<"\n\tLengthBreadth before increment";
void operator++() R.Display();
{ R++;
cout<<"\n\n\tLength Breadth after increment";
L+=2;
R.Display();
B+=2;
}
}
Relational Operators Overloading
•