SlideShare a Scribd company logo
1 of 58
Download to read offline
Object Oriented Programming
Course Objectives
OOP-Unit III 2
Course Outcomes
OOP-Unit III 3
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.
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.
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
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
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.
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;
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
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
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
Unary operator Example
OOP-Unit III 13
Unary operator overloading
OOP-Unit III 14
Unary operator overloading with friend function
OOP-Unit III 15
Unary operator Example
OOP-Unit III 16
Unary operator Example
Output is
11 12 21 22
OOP-Unit III 17
Unary operator Example
OOP-Unit III 18
Unary operator overloading
OOP-Unit III 19
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
Example
OOP-Unit III 21
Example
OOP-Unit III 22
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.
Operators can not be overloaded
OOP-Unit III 24
• Membership operator .
• Scope resolution operator ::
• Size operator sizeof
• Ternary operator ?:
• Pointer to member operator .*
friend can not be overloaded
OOP-Unit III 25
• Assignment operator =
• Function call operator ( )
• Subscripting operator [ ]
• Class member access operator ->
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)
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.
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
Implicit Conversion-Example
OOP-Unit III 29
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.
Explicit Conversion
OOP-Unit III 31
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.
Example
OOP-Unit III 33
Mutable Example
OOP-Unit III 34
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
Example
OOP-Unit III 36
Example
OOP-Unit III 37
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
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.
Function Overriding
OOP-Unit III 40
Example to call overridden function from the child class
OOP-Unit III 41
Example to call overridden function from the child class
OOP-Unit III 42
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.
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
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.
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
Problem without virtual keyword
OOP-Unit III 47
Example-Virtual
OOP-Unit III 48
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.
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.
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.
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.
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.
Example without Virtual Destructor
OOP-Unit III 54
Example with Virtual Destructor
OOP-Unit III 55
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.
Case Study
OOP-Unit III 57
Study about use of C++ SDK’s wrappers for
Java and .Net
Thank You
OOP-Unit III 58

More Related Content

Similar to OOP_UnitIII.pdf

Operator overloaing
Operator overloaingOperator overloaing
Operator overloaingzindadili
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptxmiki304759
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKumar
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANINikul4470
 
C++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversionC++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversionHashni T
 
Polymorphism 140527082302-phpapp01
Polymorphism 140527082302-phpapp01Polymorphism 140527082302-phpapp01
Polymorphism 140527082302-phpapp01Engr.Tazeen Ahmed
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptxzueZ3
 
BASIC CONCEPTS OF C++ CLASS 12
BASIC CONCEPTS OF C++ CLASS 12BASIC CONCEPTS OF C++ CLASS 12
BASIC CONCEPTS OF C++ CLASS 12Dev Chauhan
 
OODP UNIT 2 Function overloading
OODP UNIT 2 Function overloadingOODP UNIT 2 Function overloading
OODP UNIT 2 Function overloadingShanmuganathan C
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloadingPrincess Sam
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadngpreethalal
 

Similar to OOP_UnitIII.pdf (20)

Basics of cpp
Basics of cppBasics of cpp
Basics of cpp
 
Question bank unit i
Question bank unit iQuestion bank unit i
Question bank unit i
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
c++ Unit I.pptx
c++ Unit I.pptxc++ Unit I.pptx
c++ Unit I.pptx
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANI
 
C++ first s lide
C++ first s lideC++ first s lide
C++ first s lide
 
oops.pptx
oops.pptxoops.pptx
oops.pptx
 
C++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversionC++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversion
 
Polymorphism 140527082302-phpapp01
Polymorphism 140527082302-phpapp01Polymorphism 140527082302-phpapp01
Polymorphism 140527082302-phpapp01
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
 
BASIC CONCEPTS OF C++ CLASS 12
BASIC CONCEPTS OF C++ CLASS 12BASIC CONCEPTS OF C++ CLASS 12
BASIC CONCEPTS OF C++ CLASS 12
 
OODP UNIT 2 Function overloading
OODP UNIT 2 Function overloadingOODP UNIT 2 Function overloading
OODP UNIT 2 Function overloading
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 
Function in C++
Function in C++Function in C++
Function in C++
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadng
 

Recently uploaded

Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 

Recently uploaded (20)

Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 

OOP_UnitIII.pdf

  • 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
  • 15. Unary operator overloading with friend function OOP-Unit III 15
  • 17. Unary operator Example Output is 11 12 21 22 OOP-Unit III 17
  • 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
  • 47. Problem without virtual keyword OOP-Unit III 47
  • 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.
  • 54. Example without Virtual Destructor OOP-Unit III 54
  • 55. Example with Virtual Destructor OOP-Unit III 55
  • 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.
  • 57. Case Study OOP-Unit III 57 Study about use of C++ SDK’s wrappers for Java and .Net