SlideShare a Scribd company logo
1 of 37
OBJECT ORIENTED PROGRAMMING USING C++
Created By:
Kumar Vishal
(SCA), LPU
CAP444
OBJECT ORIENTED PROGRAMMING
USING C++
OBJECT ORIENTED PROGRAMMING USING C++
Topics Covered
Polymorphism :
 functions overloading,
 overloading unary operators,
 overloading binary operators,
 virtual base classes,
 abstract classes,
 pointer to object,
 this pointer,
 pointer to derived class,
 virtual function,
 pure virtual function
OBJECT ORIENTED PROGRAMMING USING C++
OBJECT ORIENTED PROGRAMMING USING C++
Polymorphism
OBJECT ORIENTED PROGRAMMING USING C++
Functions overloading
 same function name but different parameters.
 same function name with different signature
 example of polymorphism(compile time)
 overloaded functions should be there in same
class.
Example:
https://github.com/vishalamc/cap444/blob/main/functionOverl
oadingExample
OBJECT ORIENTED PROGRAMMING USING C++
Example
OBJECT ORIENTED PROGRAMMING USING C++
PlaySound(TEXT(“xyz.wav"),NULL,SND_SYNC);
TEXT(“xyz.wav"):
A string that specifies the sound to play.
NULL:
(hmod) handle to the executable file that contains the
resource to be loaded. This parameter must be NULL
unless SND_RESOURCE is specified as 3rd parameter.
SND_SYNC:
The sound is played synchronously, and PlaySound
returns after the sound event completes. This is the
default behavior.
OBJECT ORIENTED PROGRAMMING USING C++
Operator overloading
Operator overloading is a compile-time polymorphism
in which the operator is overloaded to provide the
special meaning to the user-defined data type.
• You can redefine built in operators except few:
• Scope resolution operator (::)
• sizeof
• member selector(.)
• member pointer selector(.*)
• ternary operator(?:)
OBJECT ORIENTED PROGRAMMING USING C++
Examples:
Unary Operator Overloading:
https://github.com/vishalamc/cap444/blob/main/unar
yOperatorOverlaodingEx
Binary Operator Overloading:
https://github.com/vishalamc/cap444/blob/mai
n/binaryOperatorOverloading
OBJECT ORIENTED PROGRAMMING USING C++
Operators which can overload
OBJECT ORIENTED PROGRAMMING USING C++
Consider this example
class A
{
};
int main()
{
A a1,a2,a3;
a3= a1 + a2;
return 0;
}
This is not allowed, because the
addition operator “+” is
predefined to operate only on
built-in data types. But here,
“class A” is a user-defined type,
so the compiler generates an
error. This is where the concept
of “Operator overloading”
comes in.
OBJECT ORIENTED PROGRAMMING USING C++
Operator Overloading Syntax:
return_type operator operator_Symbol(parameters)
{
}
Example:
class A
{
public:
A operator +(A const &obj)
{
}
};
OBJECT ORIENTED PROGRAMMING USING C++
Operator overloading for Unary operators:
The unary operators operate on a single operand :
• The increment (++) and decrement (--) operators.
• The unary minus (-) operator.
• The logical not (!) operator.
Example:
https://github.com/vishalamc/cap444/blob/main/unar
yOperatorOverlaodingEx
OBJECT ORIENTED PROGRAMMING USING C++
Operator overloading for binary operators:
The binary operators operate on two operands :
+,
-,
*,
/,
%
Example:
https://github.com/vishalamc/cap444/blob/main/b
inaryOperatorOverloading
a+b
OBJECT ORIENTED PROGRAMMING USING C++
Rules for Operator Overloading
Existing operators can only be overloaded.
The overloaded operator contains at least one
operand of the user-defined data type.
When unary operators are overloaded through a
member function take no explicit arguments, but, if
they are overloaded by a friend function, takes one
argument.
When binary operators are overloaded through a
member function takes one explicit argument, and if
they are overloaded through a friend function takes
two explicit arguments.
OBJECT ORIENTED PROGRAMMING USING C++
We know about Friend function
• It can access all private and protected member of
a class
• It can be call without object of the class
• It can define out side of the class scope
Rule:
Prototypes of friend function must be declare inside
the class
It can be declared either in the private or the public
part.
OBJECT ORIENTED PROGRAMMING USING C++
Overloading binary operators using friend function
Friend function takes two parameters in case when
we want to overload binary operators using
friend function
Ex:
friend A operator +(A &x, A &y);
Example:
https://github.com/vishalamc/cap444/blob/main/unaryOperato
rOverloadingUsingFriendFunction
OBJECT ORIENTED PROGRAMMING USING C++
Student
Exam Project
Result
regNo, Name
studentDetails()
regNo, Name
studentDetails()
regNo, Name
studentDetails()
regNo, Name
studentDetails()
ambiguity arises as to which
data/function member would
be called?
Situation
OBJECT ORIENTED PROGRAMMING USING C++
virtual base class introduce
OBJECT ORIENTED PROGRAMMING USING C++
virtual base classes
• It means we are making base class as virtual
• But why ???
• In situation???
int b int c
Let’s assume ( int a)
A
B C
D
int a
int d
(int a)
int a
OBJECT ORIENTED PROGRAMMING USING C++
Example:
Virtual Base Class:
https://github.com/vishalamc/cap444/blob/main/virtu
alBaseClassExample
OBJECT ORIENTED PROGRAMMING USING C++
PlayMovie()
PlaySong()
PlayMovie()
PlaySong()
PlayMovie()
PlaySong()
PlayMovie()
PlaySong()
Situation
OBJECT ORIENTED PROGRAMMING USING C++
What’s meaning it….
OBJECT ORIENTED PROGRAMMING USING C++
Its method overriding
OBJECT ORIENTED PROGRAMMING USING C++
• Problems in method overriding
– Overriding using base class pointer compiler don’t
know about pointer which address is pointing
because address will be decided at run time when
memory will be allocated.
– To over come this problem we use virtual function
– With the help of virtual function, we can override
function at run time
Example:
https://github.com/vishalamc/cap444/blob/main/vi
rtualFunctionExample
OBJECT ORIENTED PROGRAMMING USING C++
Abstract Class
• Sometimes implementation of function is not
required in a base class such a class is called
abstract class.
• In such cases we have to make function as abstract
function by using virtual keyword that is also called
pure virtual function.
• A pure virtual function is declared by assigning 0 in
declaration.
Example:
https://github.com/vishalamc/cap444/blob/main/abstra
ctClassExample
OBJECT ORIENTED PROGRAMMING USING C++
PB081234 BR012345
HP014567 KA023333
Transportation:
Vehicle_Registration()
Punjab_Transport
Vehicle_Registration()
Himachal_Transport
Vehicle_Registration()
Karnataka_Transport
Vehicle_Registration()
Bihar_Transport
Vehicle_Registration()
Situation
OBJECT ORIENTED PROGRAMMING USING C++
Area
getArea()
getArea()
Area = w × h
w = width
h = height
getArea()
Area = π × r2
OBJECT ORIENTED PROGRAMMING USING C++
Important points:
• A class is abstract if it has at least one pure virtual
function.
• If we do not override the pure virtual function in derived
class, then derived class also becomes abstract class.
• An abstract class can have constructors.
• We cannot create objects of abstract classes.
OBJECT ORIENTED PROGRAMMING USING C++
What will be the output of the following C++ code?
#include<iostream>
using namespace std;
class Mobile
{
long int contactno;
public:
virtual void getStatus() = 0;
Mobile()
{
contactno=9898989890;
cout<<contactno;
}
};
class Person: public Mobile
{
public:
void getDetails() { cout << "Blocked"; }
};
int main()
{
Person p;
p.getDetails();
return 0;
}
A. 9898989890
B. Blocked
C.9898989890Blocked
D. Error
OBJECT ORIENTED PROGRAMMING USING C++
What will be the output of the following C++ code?
#include<iostream>
using namespace std;
class Mobile
{
long int contactno;
public:
virtual void getStatus() = 0;
Mobile()
{
contactno=9898989890;
cout<<contactno;
}
};
class Person: public Mobile
{
public:
void getStatus(){ cout <<"Blocked"; }
void getDetails(){cout<<"Kumar";}
};
int main()
{
Person p;
p.getDetails();
return 0;
}
A. 9898989890
B. kumar
C. 9898989890kumar
D. Error
OBJECT ORIENTED PROGRAMMING USING C++
Pointer to object
• A variable that holds an address value is called a
pointer variable
• Object can also have an address , so there is also a
pointer that can point to the address of an object.
class Date
{
};
Date d1;
Date *d2;
d2=&d1;
d2->functions()
object pointer
Example:
https://github.com/vishalamc/cap444/b
lob/main/pointerToObjectExample
OBJECT ORIENTED PROGRAMMING USING C++
this pointer
It can be used to refer current class instance
variable.
Syntax:
this-> instance variable=value
Example:
https://github.com/vishalamc/cap444/blob/main/thisP
ointerExample
OBJECT ORIENTED PROGRAMMING USING C++
pointer to derived class
A pointer of one class can point to other class,
but classes must be a base and derived class,
then it is possible.
To access the variable of the base class, base
class pointer will be used.
OBJECT ORIENTED PROGRAMMING USING C++
Pointer to derived class
class base{};
class derive: public base{};
base b1,*b2;
derive d1;
b2=&d1;
Example:
https://github.com/vishalamc/cap444/blob/main/pointertoDeriv
edClassExample
OBJECT ORIENTED PROGRAMMING USING C++
What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class mobile
{
public:
void playRingTone()
{
cout<<"mobile ring tone"<<endl;
}
};
class samsung:public mobile
{
void playRingTone()
{
cout<<"samsung ring tone"<<endl;
}
};
class realme:public mobile
{
void playRingTone()
{
cout<<"real me ringtone"<<endl;
}
};
int main()
{
mobile *mptr;
samsung s1;
mptr=&s1;
mptr->playRingTone();
return 0;
}
A. mobile ring tone
B. samsung ring tone
C. real me rington
D. Error
OBJECT ORIENTED PROGRAMMING USING C++
Any Query?

More Related Content

Similar to CAP444-Unit-3-Polymorphism.pptx

C++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIAC++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIADheeraj Kataria
 
CPP-overviews notes variable data types notes
CPP-overviews notes variable data types notesCPP-overviews notes variable data types notes
CPP-overviews notes variable data types notesSukhpreetSingh519414
 
cppt-170218053903 (1).pptx
cppt-170218053903 (1).pptxcppt-170218053903 (1).pptx
cppt-170218053903 (1).pptxWatchDog13
 
The Present and The Future of Functional Programming in C++
The Present and The Future of Functional Programming in C++The Present and The Future of Functional Programming in C++
The Present and The Future of Functional Programming in C++Alexander Granin
 
iOS,From Development to Distribution
iOS,From Development to DistributioniOS,From Development to Distribution
iOS,From Development to DistributionTunvir Rahman Tusher
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classDeepak Singh
 
CAP444Unit6ExceptionHandling.pptx
CAP444Unit6ExceptionHandling.pptxCAP444Unit6ExceptionHandling.pptx
CAP444Unit6ExceptionHandling.pptxSatyajeetGaur2
 
Boost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationBoost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationGlobalLogic Ukraine
 
Functions in C++ programming language.pptx
Functions in  C++ programming language.pptxFunctions in  C++ programming language.pptx
Functions in C++ programming language.pptxrebin5725
 
The present and the future of functional programming in c++
The present and the future of functional programming in c++The present and the future of functional programming in c++
The present and the future of functional programming in c++Alexander Granin
 
How do you create a programming language for the JVM?
How do you create a programming language for the JVM?How do you create a programming language for the JVM?
How do you create a programming language for the JVM?Federico Tomassetti
 

Similar to CAP444-Unit-3-Polymorphism.pptx (20)

C++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIAC++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIA
 
CPP-overviews notes variable data types notes
CPP-overviews notes variable data types notesCPP-overviews notes variable data types notes
CPP-overviews notes variable data types notes
 
cppt-170218053903 (1).pptx
cppt-170218053903 (1).pptxcppt-170218053903 (1).pptx
cppt-170218053903 (1).pptx
 
The Present and The Future of Functional Programming in C++
The Present and The Future of Functional Programming in C++The Present and The Future of Functional Programming in C++
The Present and The Future of Functional Programming in C++
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
iOS,From Development to Distribution
iOS,From Development to DistributioniOS,From Development to Distribution
iOS,From Development to Distribution
 
C#2
C#2C#2
C#2
 
Intro to c++
Intro to c++Intro to c++
Intro to c++
 
L6
L6L6
L6
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
 
CAP444Unit6ExceptionHandling.pptx
CAP444Unit6ExceptionHandling.pptxCAP444Unit6ExceptionHandling.pptx
CAP444Unit6ExceptionHandling.pptx
 
Boost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationBoost.Python: C++ and Python Integration
Boost.Python: C++ and Python Integration
 
Functions in C++ programming language.pptx
Functions in  C++ programming language.pptxFunctions in  C++ programming language.pptx
Functions in C++ programming language.pptx
 
Objective c intro (1)
Objective c intro (1)Objective c intro (1)
Objective c intro (1)
 
The present and the future of functional programming in c++
The present and the future of functional programming in c++The present and the future of functional programming in c++
The present and the future of functional programming in c++
 
Polymorphismupload
PolymorphismuploadPolymorphismupload
Polymorphismupload
 
How do you create a programming language for the JVM?
How do you create a programming language for the JVM?How do you create a programming language for the JVM?
How do you create a programming language for the JVM?
 
What is c++ programming
What is c++ programmingWhat is c++ programming
What is c++ programming
 
Node.js extensions in C++
Node.js extensions in C++Node.js extensions in C++
Node.js extensions in C++
 

Recently uploaded

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 

Recently uploaded (20)

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 

CAP444-Unit-3-Polymorphism.pptx

  • 1. OBJECT ORIENTED PROGRAMMING USING C++ Created By: Kumar Vishal (SCA), LPU CAP444 OBJECT ORIENTED PROGRAMMING USING C++
  • 2. OBJECT ORIENTED PROGRAMMING USING C++ Topics Covered Polymorphism :  functions overloading,  overloading unary operators,  overloading binary operators,  virtual base classes,  abstract classes,  pointer to object,  this pointer,  pointer to derived class,  virtual function,  pure virtual function
  • 4. OBJECT ORIENTED PROGRAMMING USING C++ Polymorphism
  • 5. OBJECT ORIENTED PROGRAMMING USING C++ Functions overloading  same function name but different parameters.  same function name with different signature  example of polymorphism(compile time)  overloaded functions should be there in same class. Example: https://github.com/vishalamc/cap444/blob/main/functionOverl oadingExample
  • 6. OBJECT ORIENTED PROGRAMMING USING C++ Example
  • 7. OBJECT ORIENTED PROGRAMMING USING C++ PlaySound(TEXT(“xyz.wav"),NULL,SND_SYNC); TEXT(“xyz.wav"): A string that specifies the sound to play. NULL: (hmod) handle to the executable file that contains the resource to be loaded. This parameter must be NULL unless SND_RESOURCE is specified as 3rd parameter. SND_SYNC: The sound is played synchronously, and PlaySound returns after the sound event completes. This is the default behavior.
  • 8. OBJECT ORIENTED PROGRAMMING USING C++ Operator overloading Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. • You can redefine built in operators except few: • Scope resolution operator (::) • sizeof • member selector(.) • member pointer selector(.*) • ternary operator(?:)
  • 9. OBJECT ORIENTED PROGRAMMING USING C++ Examples: Unary Operator Overloading: https://github.com/vishalamc/cap444/blob/main/unar yOperatorOverlaodingEx Binary Operator Overloading: https://github.com/vishalamc/cap444/blob/mai n/binaryOperatorOverloading
  • 10. OBJECT ORIENTED PROGRAMMING USING C++ Operators which can overload
  • 11. OBJECT ORIENTED PROGRAMMING USING C++ Consider this example class A { }; int main() { A a1,a2,a3; a3= a1 + a2; return 0; } This is not allowed, because the addition operator “+” is predefined to operate only on built-in data types. But here, “class A” is a user-defined type, so the compiler generates an error. This is where the concept of “Operator overloading” comes in.
  • 12. OBJECT ORIENTED PROGRAMMING USING C++ Operator Overloading Syntax: return_type operator operator_Symbol(parameters) { } Example: class A { public: A operator +(A const &obj) { } };
  • 13. OBJECT ORIENTED PROGRAMMING USING C++ Operator overloading for Unary operators: The unary operators operate on a single operand : • The increment (++) and decrement (--) operators. • The unary minus (-) operator. • The logical not (!) operator. Example: https://github.com/vishalamc/cap444/blob/main/unar yOperatorOverlaodingEx
  • 14. OBJECT ORIENTED PROGRAMMING USING C++ Operator overloading for binary operators: The binary operators operate on two operands : +, -, *, /, % Example: https://github.com/vishalamc/cap444/blob/main/b inaryOperatorOverloading a+b
  • 15. OBJECT ORIENTED PROGRAMMING USING C++ Rules for Operator Overloading Existing operators can only be overloaded. The overloaded operator contains at least one operand of the user-defined data type. When unary operators are overloaded through a member function take no explicit arguments, but, if they are overloaded by a friend function, takes one argument. When binary operators are overloaded through a member function takes one explicit argument, and if they are overloaded through a friend function takes two explicit arguments.
  • 16. OBJECT ORIENTED PROGRAMMING USING C++ We know about Friend function • It can access all private and protected member of a class • It can be call without object of the class • It can define out side of the class scope Rule: Prototypes of friend function must be declare inside the class It can be declared either in the private or the public part.
  • 17. OBJECT ORIENTED PROGRAMMING USING C++ Overloading binary operators using friend function Friend function takes two parameters in case when we want to overload binary operators using friend function Ex: friend A operator +(A &x, A &y); Example: https://github.com/vishalamc/cap444/blob/main/unaryOperato rOverloadingUsingFriendFunction
  • 18. OBJECT ORIENTED PROGRAMMING USING C++ Student Exam Project Result regNo, Name studentDetails() regNo, Name studentDetails() regNo, Name studentDetails() regNo, Name studentDetails() ambiguity arises as to which data/function member would be called? Situation
  • 19. OBJECT ORIENTED PROGRAMMING USING C++ virtual base class introduce
  • 20. OBJECT ORIENTED PROGRAMMING USING C++ virtual base classes • It means we are making base class as virtual • But why ??? • In situation??? int b int c Let’s assume ( int a) A B C D int a int d (int a) int a
  • 21. OBJECT ORIENTED PROGRAMMING USING C++ Example: Virtual Base Class: https://github.com/vishalamc/cap444/blob/main/virtu alBaseClassExample
  • 22. OBJECT ORIENTED PROGRAMMING USING C++ PlayMovie() PlaySong() PlayMovie() PlaySong() PlayMovie() PlaySong() PlayMovie() PlaySong() Situation
  • 23. OBJECT ORIENTED PROGRAMMING USING C++ What’s meaning it….
  • 24. OBJECT ORIENTED PROGRAMMING USING C++ Its method overriding
  • 25. OBJECT ORIENTED PROGRAMMING USING C++ • Problems in method overriding – Overriding using base class pointer compiler don’t know about pointer which address is pointing because address will be decided at run time when memory will be allocated. – To over come this problem we use virtual function – With the help of virtual function, we can override function at run time Example: https://github.com/vishalamc/cap444/blob/main/vi rtualFunctionExample
  • 26. OBJECT ORIENTED PROGRAMMING USING C++ Abstract Class • Sometimes implementation of function is not required in a base class such a class is called abstract class. • In such cases we have to make function as abstract function by using virtual keyword that is also called pure virtual function. • A pure virtual function is declared by assigning 0 in declaration. Example: https://github.com/vishalamc/cap444/blob/main/abstra ctClassExample
  • 27. OBJECT ORIENTED PROGRAMMING USING C++ PB081234 BR012345 HP014567 KA023333 Transportation: Vehicle_Registration() Punjab_Transport Vehicle_Registration() Himachal_Transport Vehicle_Registration() Karnataka_Transport Vehicle_Registration() Bihar_Transport Vehicle_Registration() Situation
  • 28. OBJECT ORIENTED PROGRAMMING USING C++ Area getArea() getArea() Area = w × h w = width h = height getArea() Area = π × r2
  • 29. OBJECT ORIENTED PROGRAMMING USING C++ Important points: • A class is abstract if it has at least one pure virtual function. • If we do not override the pure virtual function in derived class, then derived class also becomes abstract class. • An abstract class can have constructors. • We cannot create objects of abstract classes.
  • 30. OBJECT ORIENTED PROGRAMMING USING C++ What will be the output of the following C++ code? #include<iostream> using namespace std; class Mobile { long int contactno; public: virtual void getStatus() = 0; Mobile() { contactno=9898989890; cout<<contactno; } }; class Person: public Mobile { public: void getDetails() { cout << "Blocked"; } }; int main() { Person p; p.getDetails(); return 0; } A. 9898989890 B. Blocked C.9898989890Blocked D. Error
  • 31. OBJECT ORIENTED PROGRAMMING USING C++ What will be the output of the following C++ code? #include<iostream> using namespace std; class Mobile { long int contactno; public: virtual void getStatus() = 0; Mobile() { contactno=9898989890; cout<<contactno; } }; class Person: public Mobile { public: void getStatus(){ cout <<"Blocked"; } void getDetails(){cout<<"Kumar";} }; int main() { Person p; p.getDetails(); return 0; } A. 9898989890 B. kumar C. 9898989890kumar D. Error
  • 32. OBJECT ORIENTED PROGRAMMING USING C++ Pointer to object • A variable that holds an address value is called a pointer variable • Object can also have an address , so there is also a pointer that can point to the address of an object. class Date { }; Date d1; Date *d2; d2=&d1; d2->functions() object pointer Example: https://github.com/vishalamc/cap444/b lob/main/pointerToObjectExample
  • 33. OBJECT ORIENTED PROGRAMMING USING C++ this pointer It can be used to refer current class instance variable. Syntax: this-> instance variable=value Example: https://github.com/vishalamc/cap444/blob/main/thisP ointerExample
  • 34. OBJECT ORIENTED PROGRAMMING USING C++ pointer to derived class A pointer of one class can point to other class, but classes must be a base and derived class, then it is possible. To access the variable of the base class, base class pointer will be used.
  • 35. OBJECT ORIENTED PROGRAMMING USING C++ Pointer to derived class class base{}; class derive: public base{}; base b1,*b2; derive d1; b2=&d1; Example: https://github.com/vishalamc/cap444/blob/main/pointertoDeriv edClassExample
  • 36. OBJECT ORIENTED PROGRAMMING USING C++ What will be the output of the following C++ code? #include <iostream> using namespace std; class mobile { public: void playRingTone() { cout<<"mobile ring tone"<<endl; } }; class samsung:public mobile { void playRingTone() { cout<<"samsung ring tone"<<endl; } }; class realme:public mobile { void playRingTone() { cout<<"real me ringtone"<<endl; } }; int main() { mobile *mptr; samsung s1; mptr=&s1; mptr->playRingTone(); return 0; } A. mobile ring tone B. samsung ring tone C. real me rington D. Error
  • 37. OBJECT ORIENTED PROGRAMMING USING C++ Any Query?