SlideShare uma empresa Scribd logo
1 de 19
Baixar para ler offline
OPERATOR OVERLOADING &
TYPE CONVERSION
Name : Yaksh Jethva
Contents
● 7.1 Defining operator Overloading
● 7.2 Overloading Unary Operator
● 7.3 Overloading Binary Operator
● 7.4 Overloading Operator Using Friend function
● 7.6 Type Conversion
Introduction
● C++ has ability to provide the operators with a special meanings for a data type.
● The mechanism of giving such special meaning to an operator is known as operator overloading.
Why Operator Overloading?
● Readable code
● Make operators sensitive to context
General Format for operator overloading
returnType operator operator_symbol(parameters);
➔ Return type may be whatever the operator returns
➔ Including a reference to the object of the operand
➔ Operator symbol may be any overloadable operator from the list.
Restrictions on Operator Overloading
● C++ operators that can be overloaded.
● C++ Operators that cannot be overloaded Operators that cannot be overloaded .
Overloading Unary Operator
#include <iostream.h>
class temp
{ Output Count: 6
private:
int count;
Public:
temp():count(5) { }
void operator ++()
{ count=count+1;}
void Display()
{ cout<<"Count: "<<count; }
};
int main()
{
temp t;
++t; /* operator function void operator ++() is called */
t.Display();
return 0;
}
Note
Operator overloading cannot be used to change the way operator works on built-in types. Operator
overloading only allows to redefine the meaning of operator for user-defined types.
Only existing operators can be overloaded. New operators cannot be created.
The overloaded operator must have at least one operand that is of user-defined type.
We cannot change the basic meaning of an operator.
Overloaded operators follow the syntax rules of the original operators.
Overloading Binary Operator
#include<iostream.h>
#include<conio.h>
class complex
{
int a,b;
public:
void getvalue() void display()
{ { cout<<a<<"+"<<b<<"i" <<"n"; }
cout<<"Enter the value of Complex Numbers a,b:"; };
cin>>a>>b;
}
complex operator+(complex ob)
{
complex t;
t.a=ob.a +a;
t.b=ob.b+b;
return(t);
}
complex operator-(complex ob)
{
complex t;
t.a=ob.a - a;
t.b=ob.b -b;
return(t);
}
Binary operator
void main()
{
clrscr();
complex obj1,obj2,result,result1;
obj1.getvalue();
obj2.getvalue();
result = obj1+obj2;
result1=obj1-obj2;
cout<<"Input Values:n";
obj1.display();
obj2.display();
cout<<"Result:";
result.display();
result1.display();
getch();
}
Overloading operators using Friend
● Friend function using operator overloading offers better flexibility to the class.
● These functions are not a members of the class and they do not have 'this' pointer.
● When you overload a unary operator you have to pass one argument.
● When you overload a binary operator you have to pass two arguments.
● Friend function can access private members of a class directly.
● friend return-type operator operator-symbol (Variable 1, Varibale2)
{
//Statements;
}
#include<iostream>
class UnaryFriend
{
int a=10;
int b=20;
int c=30;
public:
void getvalues()
{cout<<"Values of A, B & Cn";
cout<<a<<"n"<<b<<"n"<<c<<"n
"<<endl;
}
void show()
{cout<<a<<"n"<<b<<"n"<<c<<"
n"<<endl;
}
void friend operator-(UnaryFriend &x);
};
void operator-(UnaryFriend &x)
{
x.a = -x.a;
x.b = -x.b;
x.c = -x.c;
}
int main()
{
UnaryFriend x1;
x1.getvalues();
cout<<"Before Overloadingn";
x1.show();
cout<<"After Overloading n";
-x1;
x1.show();
return 0;
}
class time
{
Private:
int hours,minutes;
Public:
time( )
{
hours=0;
minutes=0;
}
time(int x,int y)
{
hours=x;
minutes=y;
}
void display( )
{
cout<<endl<<hours<<" hours and
"<<minutes<<" minutes.";
}
friend time operator + (time, time);
};
time operator + (time y, time z)
{
int h = y.hours + z.hours;
int m = y.minutes + z.minutes;
while (m>=60)
{
m = m-60;
h = h+1;
}
return time(h,m);
}
int main( )
{
time t1(2,40);
time t2(3,30);
time t3;
t3 = t1+t2;
t3.display( );
return 0;
}
class time
{
Private:
int hours,minutes;
Public:
time( )
{
hours=0;
minutes=0;
}
time(int x,int y)
{
hours=x;
minutes=y;
}
void display( )
{
cout<<endl<<hours<<" hours and
"<<minutes<<" minutes.";
}
friend time operator + (time, time);
};
time operator + (time y, time z)
{
int h = y.hours + z.hours;
int m = y.minutes + z.minutes;
while (m>=60)
{
m = m-60;
h = h+1;
}
return time(h,m);
}
int main( )
{
time t1(2,40);
time t2(3,30);
time t3;
t3 = t1+t2;
t3.display( );
return 0;
}
Type Conversions
● the type conversions are automatic only when the data types involved are built-in types.
● int m; float x = 3.14159; m = x;// convert x to integer before its value is assigned // to m.
● For user defined data types, the compiler does not support automatic type conversions.
● Different situations of data conversion between incompatible types.
● Conversion from basic type to class type.
● Conversion from class type to basic type.
● Conversion from one class type to another class type.
Basic to Class Type
A constructor to build a string type object from a char * type variable.
string : : string(char *a)
{
length = strlen(a);
P = new char[length+1];
strcpy(P,a);
}
The variables length and p are data members of the class string.
Class To Basic Type
● A constructor function do not support type conversion from a class type to a basic type.
● An overloaded casting operator is used to convert a class type data to a basic type. It is also
referred to as conversion function.
● operator typename( )
{ … ( function statements ) … }
● This function converts a class type data to typename.
vector : : operator double( )
{
double sum = 0;
for (int i=0; i < size ; i++)
sum = sum + v[i] * v[i];
return sqrt (sum);
}
● This function converts a vector to the square root of the sum of squares of its components.
Continue...
The casting operator function should satisfy the following conditions:
● It must be a class member.
● It must not specify a return type.
● It must not have any arguments.
One Class To Another Class Type
● Type objX = objY ; // objects of different types objX is an object of class X and objY is an object of
class Y.
● The class Y type data is converted to the class X type data and the converted value is assigned to
the objX.
● Conversion is takes place from class Y to class X.
● Y is known as source class.
● X is known as destination class.
Thank you!!!

Mais conteúdo relacionado

Mais procurados

Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloadingPrincess Sam
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadngpreethalal
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading Charndeep Sekhon
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyGrejoJoby1
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingabhay singh
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKumar
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator OverloadingHadziq Fabroyir
 
Data Type Conversion in C++
Data Type Conversion in C++Data Type Conversion in C++
Data Type Conversion in C++Danial Mirza
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++Aabha Tiwari
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKamal Acharya
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type ConversionsRokonuzzaman Rony
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingArunaDevi63
 
C++ overloading
C++ overloadingC++ overloading
C++ overloadingsanya6900
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloadingPrincess Sam
 

Mais procurados (20)

Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadng
 
operator overloading in C++
operator overloading in C++operator overloading in C++
operator overloading in C++
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Data Type Conversion in C++
Data Type Conversion in C++Data Type Conversion in C++
Data Type Conversion in C++
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type Conversions
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Lecture5
Lecture5Lecture5
Lecture5
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 

Semelhante a Operator_Overloaing_Type_Conversion_OOPC(C++)

Semelhante a Operator_Overloaing_Type_Conversion_OOPC(C++) (20)

Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
3d7b7 session4 c++
3d7b7 session4 c++3d7b7 session4 c++
3d7b7 session4 c++
 
chapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfchapter-8-function-overloading.pdf
chapter-8-function-overloading.pdf
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
 
Oops
OopsOops
Oops
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
 
parameter passing in c#
parameter passing in c#parameter passing in c#
parameter passing in c#
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
Object Oriented Programming using C++ - Part 3
Object Oriented Programming using C++ - Part 3Object Oriented Programming using C++ - Part 3
Object Oriented Programming using C++ - Part 3
 
Unit 4.pdf
Unit 4.pdfUnit 4.pdf
Unit 4.pdf
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
3. Polymorphism.pptx
3. Polymorphism.pptx3. Polymorphism.pptx
3. Polymorphism.pptx
 
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...
 
object oriented programming language.pptx
object oriented programming language.pptxobject oriented programming language.pptx
object oriented programming language.pptx
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Overloading
OverloadingOverloading
Overloading
 

Mais de Yaksh Jethva

About Markets (Types of markets) - Economics
About Markets (Types of markets) - EconomicsAbout Markets (Types of markets) - Economics
About Markets (Types of markets) - EconomicsYaksh Jethva
 
Cost and Various Cost Types
Cost and Various Cost TypesCost and Various Cost Types
Cost and Various Cost TypesYaksh Jethva
 
Logic Families ( Digital Electronics )
Logic Families ( Digital Electronics )Logic Families ( Digital Electronics )
Logic Families ( Digital Electronics )Yaksh Jethva
 
ANSI-SPARC Architecture - (3-Tier Architecture)
ANSI-SPARC Architecture - (3-Tier Architecture)ANSI-SPARC Architecture - (3-Tier Architecture)
ANSI-SPARC Architecture - (3-Tier Architecture)Yaksh Jethva
 
Transaction Properties(ACID Properties)
Transaction Properties(ACID Properties)Transaction Properties(ACID Properties)
Transaction Properties(ACID Properties)Yaksh Jethva
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureYaksh Jethva
 
AVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data StructureAVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data StructureYaksh Jethva
 

Mais de Yaksh Jethva (7)

About Markets (Types of markets) - Economics
About Markets (Types of markets) - EconomicsAbout Markets (Types of markets) - Economics
About Markets (Types of markets) - Economics
 
Cost and Various Cost Types
Cost and Various Cost TypesCost and Various Cost Types
Cost and Various Cost Types
 
Logic Families ( Digital Electronics )
Logic Families ( Digital Electronics )Logic Families ( Digital Electronics )
Logic Families ( Digital Electronics )
 
ANSI-SPARC Architecture - (3-Tier Architecture)
ANSI-SPARC Architecture - (3-Tier Architecture)ANSI-SPARC Architecture - (3-Tier Architecture)
ANSI-SPARC Architecture - (3-Tier Architecture)
 
Transaction Properties(ACID Properties)
Transaction Properties(ACID Properties)Transaction Properties(ACID Properties)
Transaction Properties(ACID Properties)
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
 
AVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data StructureAVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data Structure
 

Último

Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
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
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spaintimesproduction05
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdfSuman Jyoti
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 

Último (20)

Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
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...
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 

Operator_Overloaing_Type_Conversion_OOPC(C++)

  • 1. OPERATOR OVERLOADING & TYPE CONVERSION Name : Yaksh Jethva
  • 2. Contents ● 7.1 Defining operator Overloading ● 7.2 Overloading Unary Operator ● 7.3 Overloading Binary Operator ● 7.4 Overloading Operator Using Friend function ● 7.6 Type Conversion
  • 3. Introduction ● C++ has ability to provide the operators with a special meanings for a data type. ● The mechanism of giving such special meaning to an operator is known as operator overloading. Why Operator Overloading? ● Readable code ● Make operators sensitive to context
  • 4. General Format for operator overloading returnType operator operator_symbol(parameters); ➔ Return type may be whatever the operator returns ➔ Including a reference to the object of the operand ➔ Operator symbol may be any overloadable operator from the list.
  • 5. Restrictions on Operator Overloading ● C++ operators that can be overloaded. ● C++ Operators that cannot be overloaded Operators that cannot be overloaded .
  • 6. Overloading Unary Operator #include <iostream.h> class temp { Output Count: 6 private: int count; Public: temp():count(5) { } void operator ++() { count=count+1;} void Display() { cout<<"Count: "<<count; } }; int main() { temp t; ++t; /* operator function void operator ++() is called */ t.Display(); return 0; }
  • 7. Note Operator overloading cannot be used to change the way operator works on built-in types. Operator overloading only allows to redefine the meaning of operator for user-defined types. Only existing operators can be overloaded. New operators cannot be created. The overloaded operator must have at least one operand that is of user-defined type. We cannot change the basic meaning of an operator. Overloaded operators follow the syntax rules of the original operators.
  • 8. Overloading Binary Operator #include<iostream.h> #include<conio.h> class complex { int a,b; public: void getvalue() void display() { { cout<<a<<"+"<<b<<"i" <<"n"; } cout<<"Enter the value of Complex Numbers a,b:"; }; cin>>a>>b; } complex operator+(complex ob) { complex t; t.a=ob.a +a; t.b=ob.b+b; return(t); } complex operator-(complex ob) { complex t; t.a=ob.a - a; t.b=ob.b -b; return(t); }
  • 9. Binary operator void main() { clrscr(); complex obj1,obj2,result,result1; obj1.getvalue(); obj2.getvalue(); result = obj1+obj2; result1=obj1-obj2; cout<<"Input Values:n"; obj1.display(); obj2.display(); cout<<"Result:"; result.display(); result1.display(); getch(); }
  • 10. Overloading operators using Friend ● Friend function using operator overloading offers better flexibility to the class. ● These functions are not a members of the class and they do not have 'this' pointer. ● When you overload a unary operator you have to pass one argument. ● When you overload a binary operator you have to pass two arguments. ● Friend function can access private members of a class directly. ● friend return-type operator operator-symbol (Variable 1, Varibale2) { //Statements; }
  • 11. #include<iostream> class UnaryFriend { int a=10; int b=20; int c=30; public: void getvalues() {cout<<"Values of A, B & Cn"; cout<<a<<"n"<<b<<"n"<<c<<"n "<<endl; } void show() {cout<<a<<"n"<<b<<"n"<<c<<" n"<<endl; } void friend operator-(UnaryFriend &x); }; void operator-(UnaryFriend &x) { x.a = -x.a; x.b = -x.b; x.c = -x.c; } int main() { UnaryFriend x1; x1.getvalues(); cout<<"Before Overloadingn"; x1.show(); cout<<"After Overloading n"; -x1; x1.show(); return 0; }
  • 12. class time { Private: int hours,minutes; Public: time( ) { hours=0; minutes=0; } time(int x,int y) { hours=x; minutes=y; } void display( ) { cout<<endl<<hours<<" hours and "<<minutes<<" minutes."; } friend time operator + (time, time); }; time operator + (time y, time z) { int h = y.hours + z.hours; int m = y.minutes + z.minutes; while (m>=60) { m = m-60; h = h+1; } return time(h,m); } int main( ) { time t1(2,40); time t2(3,30); time t3; t3 = t1+t2; t3.display( ); return 0; }
  • 13. class time { Private: int hours,minutes; Public: time( ) { hours=0; minutes=0; } time(int x,int y) { hours=x; minutes=y; } void display( ) { cout<<endl<<hours<<" hours and "<<minutes<<" minutes."; } friend time operator + (time, time); }; time operator + (time y, time z) { int h = y.hours + z.hours; int m = y.minutes + z.minutes; while (m>=60) { m = m-60; h = h+1; } return time(h,m); } int main( ) { time t1(2,40); time t2(3,30); time t3; t3 = t1+t2; t3.display( ); return 0; }
  • 14. Type Conversions ● the type conversions are automatic only when the data types involved are built-in types. ● int m; float x = 3.14159; m = x;// convert x to integer before its value is assigned // to m. ● For user defined data types, the compiler does not support automatic type conversions. ● Different situations of data conversion between incompatible types. ● Conversion from basic type to class type. ● Conversion from class type to basic type. ● Conversion from one class type to another class type.
  • 15. Basic to Class Type A constructor to build a string type object from a char * type variable. string : : string(char *a) { length = strlen(a); P = new char[length+1]; strcpy(P,a); } The variables length and p are data members of the class string.
  • 16. Class To Basic Type ● A constructor function do not support type conversion from a class type to a basic type. ● An overloaded casting operator is used to convert a class type data to a basic type. It is also referred to as conversion function. ● operator typename( ) { … ( function statements ) … } ● This function converts a class type data to typename. vector : : operator double( ) { double sum = 0; for (int i=0; i < size ; i++) sum = sum + v[i] * v[i]; return sqrt (sum); } ● This function converts a vector to the square root of the sum of squares of its components.
  • 17. Continue... The casting operator function should satisfy the following conditions: ● It must be a class member. ● It must not specify a return type. ● It must not have any arguments.
  • 18. One Class To Another Class Type ● Type objX = objY ; // objects of different types objX is an object of class X and objY is an object of class Y. ● The class Y type data is converted to the class X type data and the converted value is assigned to the objX. ● Conversion is takes place from class Y to class X. ● Y is known as source class. ● X is known as destination class.