4. UnitIII- Syllabus
OOP-Unit III 4
Introduction to Polymorphism, Types of Polymorphism, Operator
Overloading- concept of operator overloading. Overloading Unary Operators,
Overloading Binary Operators Data Conversion, Type casting (implicit and
explicit), Pitfalls of Operator Overloading and Conversion, Keywords explicit
and mutable. Function overloading Run Time Polymorphism- Pointers to Base
class, virtual function and its significance in C++. pure virtual function and
virtual table, virtual destructor, abstract base class.
5. Introduction to Polymorphism
OOP-Unit III 5
• Ability to take more than one form.
• Afunction to exhibit different behavior with different instances. Behavior is
dependent on type and number of parameters.
6. Types of Polymorphism
OOP-Unit III 6
Static/Compile time Polymorphism:
• Memory allocation at compile time.
• Object is bound to the function call at the time of compilation. Also termed
as Early binding.
• Function Overloading
• Function Overriding
• Constructor Overloading
• Operator Overloading
7. Types of Polymorphism
OOP-Unit III 7
Dynamic/Runtime Polymorphism:
• Memory allocation at run time.
• Object is bound to the function call at the time of execution.Also termed as
Late binding
• Virtual Functions
8. Operator Overloading
OOP-Unit III 8
• The ability to overload operators is one of C++'s most powerful features. It allows
the full integration of new class types into the programming environment.
• It is to make the user defined data types behave in much the same way as the built in
types.
• This means that C++ has the ability to provide the operators with a special meaning
for a data type. The mechanism of giving such special meanings to an operator is
known as operator overloading.
• After overloading the appropriate operators, you can use objects in expressions in
just the same way that you use C++'s built-in data types.
• When an operator is overloaded, none of its original meanings are lost.
9. Operator overloading
OOP-Unit III 9
• For example, statements like
d3.addobjects(d1, d2);
or the similar
d3 = d1.addobjects(d2);
can be changed to the much more readable
d3 = d1 + d2;
10. Operator overloading
OOP-Unit III 10
The process of overloading involves the following steps:
1.Create a class that defines the data types that is to be used in the overloading
operations.
2. Declare operator function operator op() in public section of class.
3. Define the operator function to implement required operations
4. Overloaded function can be invoked as
Op x or x op
X op Y
- for unary operator
- for binary operator
11. Operator overloading
Operator Function can be defined as per below
OOP-Unit III 11
Op is the operator being overloaded.
The op is preceded by the keyword operator.
Operator op is the function name
12. Unary Operator
OOP-Unit III 12
• Unary operator takes only one operand.
• If x is int or float then we can write unary expression like..
Ex -x, ++x
• If x is an object then we will use it in the same way as –x to negate the
object.
Link for operator and expression
https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.4.0/com.ib
m.zos.v2r4.cbclx01/cplr185.htm
20. Binary Operator
OOP-Unit III 20
• Binary operator takes two operands.
• If x and y are is int or float then we can write binary expression like..
Ex x+y, x-y
• If x and y are two objects then we will write x+y to add two objects
23. Limitations/ Restrictions
OOP-Unit III 23
• No new operators can be created, only existing operators can be overloaded.
• The overloaded operator must have at least one operand that is of user defined
type.
• Can not change the meaning of an operator. Overloaded operators follow the
syntax rules of the original operators.
• Some operators can not be overloaded, can not use friend function for some
operators.
• Precedence and Associativity of an operator can not be changed.
• Arity (numbers of Operands) can not be changed. Unary operator remains unary,
binary remains binary etc.
24. Operators can not be overloaded
OOP-Unit III 24
• Membership operator .
• Scope resolution operator ::
• Size operator sizeof
• Ternary operator ?:
• Pointer to member operator .*
25. friend can not be overloaded
OOP-Unit III 25
• Assignment operator =
• Function call operator ( )
• Subscripting operator [ ]
• Class member access operator ->
26. Type Conversion
OOP-Unit III 26
The process of converting the data type of a value in another data type is
known as data type conversion.
The conversion can be performed in two ways:
Implicit (Automatic)
Explicit (User-defined)
27. Implicit Conversion
OOP-Unit III 27
It convert data type to another data type automatically.
Implicit conversion do not required any operator.
The operands in arithmetic operation must be of similar types.
An expression in which different data types is called Mixed-type
expression
In this case, the result of an expression is evaluated to larger data type in the
expression .
Suppose an expression contains an integer and double as operands.
The result will be evaluated to double data type.
28. Implicit Conversion
Expression Intermediate Type
Char + Float Float
Int - Long Long
Int * Double Double
Float / Long Double Long Double
Suppose x is an integer and y is long and we want to add up these two variables. x+y.
In the above data type of x is lower then data type y. So the value of x will be changed
or converted int long during execution.
OOP-Unit III 28
30. Explicit Conversion
OOP-Unit III 30
It can be converted or performed by the programmer .
It will be performed by the using of cast operator or operators.
Cast operator tells the compiler to convert the data type of a value.
Syntax :(type)expression
Type : It indicates the data type to which operand is to be converted.
32. Explicit and Mutable
OOP-Unit III 32
Explicit: restricts implicit conversion of argument received in a constructor
to object of the class.
Mutable: allows a const data member to be modified. Useful in classes that
have secret implementation details that do not contribute to the logical
value of an object.
35. Function Overloading
This feature allows us to have more than one function having same name
but different parameter list,
The data type and sequence of the parameters must be different,
for example
OOP-Unit III 35
void sum (int num1, int num2)
void sum (int num1, int num2, int num3)
void sum (int num1, double num2)
int sum (int num1, int num2)
float sum (int num1, int num2)
Invalid, creates ambiguity and get error
38. Default argument to function
Adefault argument is a value
provided in a function declaration
that is automatically assigned by the
compiler if the caller of the function
doesn’t provide a value for the
argument with a default value.
OOP-Unit III 38
39. Function Overriding
OOP-Unit III 39
• Function overriding is a feature that allows us to have a same function in
child class which is already present in the parent class.
• Achild class inherits the data members and member functions of parent
class, but when you want to override a functionality in the child class then
you can use function overriding.
• It is like creating a new version of an old function, in the child class.
41. Example to call overridden function from the child class
OOP-Unit III 41
42. Example to call overridden function from the child class
OOP-Unit III 42
43. Function Overloading VS Function Overriding
OOP-Unit III 43
1.Inheritance: Overriding of functions occurs when one class is inherited
from another class. Overloading can occur without inheritance.
2.Function Signature: Overloaded functions must differ in function
signature ie either number of parameters or type of parameters should
differ. In overriding, function signatures must be same.
3.Scope of functions: Overridden functions are in different scopes;
whereas overloaded functions are in same scope.
4.Behavior of functions: Overriding is needed when derived class
function has to do some added or different job than the base class
function. Overloading is used to have same name functions which behave
differently depending upon parameters passed to them.
44. Virtual Function
OOP-Unit III 44
• Virtual Function is a function in base class, which is overrided in the derived
class and which tell compiler to perform Late Binding on this function.
• virtual keyword is used to make a member function of the base class
virtual.
• Late Binding – function call is resolved at runtime. Complier determines the
type of object at runtime and then binds the function call.
• Late binding is also called Dynamic Binding or Runtime binding
45. Virtual Function
OOP-Unit III 45
• If there are member functions with same name in base class and derived
class, virtual functions gives programmer capability to call member function
of different class by a same function call depending upon different context.
This feature in c++ programming is known as polymorphism which is one
of the important feature of OOP.
• If a base class and derived class has same function and if you write code to
access that function using pointer of base class then, the function in the base
class is executed even if, the object of derived class is referenced with that
pointer variable.
46. Virtual Function
• Avirtual function is a member function of class that is declared within a
base class and re-defined in derived class.
• When you want to use same function name in both the base and derived
class, then the function is base class is declared as virtual by using the virtual
keyword.
virtual return_type function_name ( )
{
}
OOP-Unit III 46
49. Abstract Class
OOP-Unit III 49
• Abstract Class is a class which contains at least one Pure Virtual function in
it.
• Abstract classes are used to provide an interface for its sub classes.
• Classes inheriting an abstract Class must provide definition to the pure
virtual function, otherwise they will also become abstract class.
• Aclass with at least one pure virtual function or abstract function is called
abstract class.
• Pure virtual functional is also known as abstract function.
50. Characteristics of Abstract class
OOP-Unit III 50
• Abstract class can not be instantiated, but pointers and references of abstract
class type can be created
• Abstract class can have normal functions and variables along with a pure
virtual function
• Abstract classes are mainly used for upcasting, so that its derived classes can
use its interface.
• Classes inheriting an abstract class must implement all pure virtual functions,
or lese they will become abstract too.
51. Pure Virtual Functions
OOP-Unit III 51
• Pure virtual functions are virtual with no definition. They start with virtual
keyword and ends with=0. Here is the syntax for a pure virtual function,
virtual void display ( ) = 0;
• Aclass which have pure virtual function is called abstract class.
• We can not create object of abstract class to use that function.
52. Virtual Table
OOP-Unit III 52
• For every class that contains virtual functions, the compiler constructs
a virtual table, called vtable..
• The virtual table is a lookup table of functions used to resolve function calls
in a dynamic/late binding.
• The virtual table sometimes goes by other names, such as “vtable”, “virtual
function table”, “virtual method table”, or “dispatch table”.
• The vtable contains an entry for each virtual function accessible by the class
and stores a pointer to its definition.
• Entries in the vtable can point to either functions declared in the class itself
or virtual functions inherited from a base class.
53. Virtual Destructor
OOP-Unit III 53
• Destructors in the Base class can be Virtual. Whenever Upcasting is done,
Destructors of the Base class must be made virtual for proper destruction of
the object when the program exits.
• When we have Virtual destructor inside the base class, then first Derived
class's destructor is called and then Base class's destructor is called, which is
the desired behavior.
• NOTE:
Constructors are never Virtual, only Destructors can be Virtual.
56. Compile time vs run time polymorphism
OOP-Unit III 56
1 In Compile time Polymorphism, the call is
resolved by the compiler.
In Run time Polymorphism, the call is not
resolved by the compiler.
2 Static binding, Early binding. Dynamic binding, Late binding
3 Method overloading is the compile-time
polymorphism where more than one methods
share the same name with different parameters or
signature and different return type.
Method overriding is the runtime polymorphism
having same method with same parameters or
signature, but associated in different classes.
4 It is achieved by function overloading and
operator overloading.
It is achieved by virtual functions and pointers.
5 It provides fast execution because the method that
needs to be executed is known early at the compile
time.
It provides slow execution as compare to early
binding because the method that needs to be
executed is known at the runtime.
6 Compile time polymorphism is less flexible as all
things execute at compile time.
Run time polymorphism is more flexible as all
things execute at run time.