Unit 3
Unit 3
Unit 3 ;-
Polymorphism
Operator - An operator is a symbol that tells the compiler to perform specific mathematical, logical
manipulations, or some other special operation.
Example:
• arithmetic operator: + , −, ∗, /
Arithmetic operator such as + and / are already overloaded in C/C++ for different built-in
types.
For the same operator / , different algorithms are used to compute two types of divisions.
Subject :- Object Oriented Programming
Unit 3 ;-
Polymorphism
Operator Overloading
● In case of a non-static function, the binary operator should have only one argument and unary
should not have an argument.
● In the case of a friend function, the binary operator should have only two argument and unary
should have only one argument.
● All the class member object should be public if operator overloading is implemented.
● Operators that cannot be overloaded are . .* :: ?:
● Operator cannot be used to overload when declaring that function as friend function = () [] ->.
Subject :- Object Oriented Programming
Unit 3 ;-
Polymorphism
Here, + is a binary operator that works on the operands num1 and num2.
When we overload the binary operator for user-defined types by using the code:
obj3 = obj1 + obj2;
The operator function is called using the obj1 object and obj2 is passed as an argument
to the function.
Subject :- Object Oriented Programming
Unit 3 ;-
Polymorphism
Rules of Operator Overloading
1. The first and basic rule of operator overloading is: we can overload unary operator as only unary
operator, it cannot be overload as binary operator and vice versa.
2. We cannot overload those operators that are not a part of C++ language like ‘$’.
3. We can perform operator overloading in only user defined data types. We cannot change the
operators existing functionality.
4. Using operator overloading we cannot change the presidency and associativity of operators.
5. There are some operators cannot be overloaded that are given below:
○ :: Scope resolution operator.
○ . Class membership operator.
○ ?: ternary or conditional operator.
○ .* pointer to member operator.
○ ->* pointer to member operator.
Subject :- Object Oriented Programming
Unit 3 ;-
Polymorphism
Data Conversion
The type conversions are automatic only when the data types involved are built-in
/Primitive types.
int m;
float x = 3.14159;
m = x; // convert x to integer before its value is assigned // to m.
For user defined data types, the compiler does not support automatic type
conversions. We must design the conversion routines by ourselves.
Subject :- Object Oriented Programming
Unit 3 ;-
Polymorphism
Data Conversion
Class B int b
Subject :- Object Oriented Programming
Unit 3 ;-
Polymorphism
Type casting
#include <iostream>
using namespace std;
int main()
{
double x = 1.2;
return 0;
}
Subject :- Object Oriented Programming
Unit 3 ;-
Polymorphism
Explicit Keyword:-
re-defined(Overriden) by a 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
● Virtual functions ensure that the correct function is called for an object, regardless of the
type of reference (or pointer) used for function call.
● 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.
Subject :- Object Oriented Programming
Unit 3 ;-
Polymorphism
Virtual Function in C++
Rules for Virtual Functions
1. Virtual functions cannot be static and also cannot be a friend function of another class.
2. Virtual functions should be accessed using pointer or reference of base class type to
achieve run time polymorphism.
3. The prototype of virtual functions should be same in base as well as derived class.
4. 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.
5. A class may have virtual destructor but it cannot have a virtual constructor.
Subject :- Object Oriented Programming
Unit 3 ;-
Polymorphism
Virtual Destructor
● Deleting a derived class object using a pointer of base class type that has
a non-virtual destructor results in undefined behavior. To correct this
situation, the base class should be defined with a virtual destructor.
Subject :- Object Oriented Programming
Unit 3 ;-
Polymorphism