SlideShare a Scribd company logo
1 of 15
Download to read offline
OBJECT ORIENTED PROGRAMMING USING C++
Created By:
Kumar Vishal
(SCA), LPU
CAP444
OBJECT ORIENTED PROGRAMMING
USING C++
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:
OBJECT ORIENTED PROGRAMMING USING C++
#include <iostream>
using namespace std;
class Sub
{
private:
int a;
int b;
public:
Sub()
{
a=10;
b=20;
}
friend Sub operator -(Sub &x, Sub &y);
void getresult()
{
cout<<a<<endl;
cout<<b<<endl;
}
};
Sub operator -(Sub &x,Sub &y)
{
Sub z;
z.a=x.b-x.a;
z.b=y.b-y.a;
return z;
}
int main()
{
Sub a1,a2,a3;
a3=a1-a2;
a3.getresult();
return 0;
}
A.
10
10
B.
-10
-10
C.
0
0
D. None
What will be output for following code?
OBJECT ORIENTED PROGRAMMING USING C++
Situation??
OBJECT ORIENTED PROGRAMMING USING C++
Type Conversion
• Basic data types conversion done automatic
by compiler
• User define data type conversion not done
automatically
• User define data type conversion done by
using either constructor or by using casting
operator
OBJECT ORIENTED PROGRAMMING USING C++
What will be output?
#include <iostream>
using namespace std;
int main()
{
double a = 21.09399;
float b = 10.20;
int c ;
c = a;
cout << c ;
c = b;
cout << c ;
return 0;
}
A) 2110
B) 1210
C) 21
D) 121
OBJECT ORIENTED PROGRAMMING USING C++
Three type of situation occurs during user define
type conversion:
• 1. basic type to class type(using constructor)
• 2. class type to basic type(using casting
operator function)
• 3. class type to class type (using constructor
and casting operator function both)
OBJECT ORIENTED PROGRAMMING USING C++
basic type to class type(using constructor)
#include <iostream>
using namespace std;
class A
{
};
int main()
{
A a1;
int x=8;
a1=x ;//basic to class type
return 0;
}
Basic type to class type achieved
by using constructor.
OBJECT ORIENTED PROGRAMMING USING C++
class type to basic type(using casting operator function)
Class type to basic type done by using casting operator
function
1. It must be a define inside in class.
2. It must not specify a return type in function signature.
3. It must not have any arguments.
class A
{};
A a1;
int x;
x=a1 //class type to basic type
Go through: cplusplus/Class to basic type conversionEx.pdf at master · vishalamc/cplusplus
(github.com)
OBJECT ORIENTED PROGRAMMING USING C++
casting operator function
Syntax:
operator dest_typename()
{
return type;
}
operator int()
{
return a;
}
OBJECT ORIENTED PROGRAMMING USING C++
class type to class type (using constructor and casting
operator function both)
Ex: A obj1; B obj2;
obj1 = obj2 ; // obj1 and obj2 are objects of different
classes
➢ First approach using Constructor:-
Left side of assignment operator(=) which is class object
we have to create constructor in that class here in Class A.
➢ Second approach using casting operator function:
Right side of assignment operator(=) which is class object
we have to create casting operator function in that class
here class B.
OBJECT ORIENTED PROGRAMMING USING C++
What is the return type of the casting
operator function?
a) void
b) int
c) float
d) no return type
OBJECT ORIENTED PROGRAMMING USING C++
OBJECT ORIENTED PROGRAMMING USING C++
OBJECT ORIENTED PROGRAMMING USING C++
Any Query?

More Related Content

Similar to (Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf

Similar to (Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf (20)

C++
C++C++
C++
 
Function notes
Function notesFunction notes
Function notes
 
Function notes 2
Function notes 2Function notes 2
Function notes 2
 
Functions in C++ programming language.pptx
Functions in  C++ programming language.pptxFunctions in  C++ programming language.pptx
Functions in C++ programming language.pptx
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Virtual function
Virtual functionVirtual function
Virtual function
 
Functions in c++,
Functions in c++,Functions in c++,
Functions in c++,
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUAL
 
Tut Constructor
Tut ConstructorTut Constructor
Tut Constructor
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
3. Polymorphism.pptx
3. Polymorphism.pptx3. Polymorphism.pptx
3. Polymorphism.pptx
 
Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.ppt
 
Overloading
OverloadingOverloading
Overloading
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Tutconstructordes
TutconstructordesTutconstructordes
Tutconstructordes
 

Recently uploaded

Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 

Recently uploaded (20)

Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 

(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf

  • 1. OBJECT ORIENTED PROGRAMMING USING C++ Created By: Kumar Vishal (SCA), LPU CAP444 OBJECT ORIENTED PROGRAMMING USING C++
  • 2. 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:
  • 3. OBJECT ORIENTED PROGRAMMING USING C++ #include <iostream> using namespace std; class Sub { private: int a; int b; public: Sub() { a=10; b=20; } friend Sub operator -(Sub &x, Sub &y); void getresult() { cout<<a<<endl; cout<<b<<endl; } }; Sub operator -(Sub &x,Sub &y) { Sub z; z.a=x.b-x.a; z.b=y.b-y.a; return z; } int main() { Sub a1,a2,a3; a3=a1-a2; a3.getresult(); return 0; } A. 10 10 B. -10 -10 C. 0 0 D. None What will be output for following code?
  • 4. OBJECT ORIENTED PROGRAMMING USING C++ Situation??
  • 5. OBJECT ORIENTED PROGRAMMING USING C++ Type Conversion • Basic data types conversion done automatic by compiler • User define data type conversion not done automatically • User define data type conversion done by using either constructor or by using casting operator
  • 6. OBJECT ORIENTED PROGRAMMING USING C++ What will be output? #include <iostream> using namespace std; int main() { double a = 21.09399; float b = 10.20; int c ; c = a; cout << c ; c = b; cout << c ; return 0; } A) 2110 B) 1210 C) 21 D) 121
  • 7. OBJECT ORIENTED PROGRAMMING USING C++ Three type of situation occurs during user define type conversion: • 1. basic type to class type(using constructor) • 2. class type to basic type(using casting operator function) • 3. class type to class type (using constructor and casting operator function both)
  • 8. OBJECT ORIENTED PROGRAMMING USING C++ basic type to class type(using constructor) #include <iostream> using namespace std; class A { }; int main() { A a1; int x=8; a1=x ;//basic to class type return 0; } Basic type to class type achieved by using constructor.
  • 9. OBJECT ORIENTED PROGRAMMING USING C++ class type to basic type(using casting operator function) Class type to basic type done by using casting operator function 1. It must be a define inside in class. 2. It must not specify a return type in function signature. 3. It must not have any arguments. class A {}; A a1; int x; x=a1 //class type to basic type Go through: cplusplus/Class to basic type conversionEx.pdf at master · vishalamc/cplusplus (github.com)
  • 10. OBJECT ORIENTED PROGRAMMING USING C++ casting operator function Syntax: operator dest_typename() { return type; } operator int() { return a; }
  • 11. OBJECT ORIENTED PROGRAMMING USING C++ class type to class type (using constructor and casting operator function both) Ex: A obj1; B obj2; obj1 = obj2 ; // obj1 and obj2 are objects of different classes ➢ First approach using Constructor:- Left side of assignment operator(=) which is class object we have to create constructor in that class here in Class A. ➢ Second approach using casting operator function: Right side of assignment operator(=) which is class object we have to create casting operator function in that class here class B.
  • 12. OBJECT ORIENTED PROGRAMMING USING C++ What is the return type of the casting operator function? a) void b) int c) float d) no return type
  • 15. OBJECT ORIENTED PROGRAMMING USING C++ Any Query?