SlideShare a Scribd company logo
OBJECT ORIENTED
PROGRAMMING WITH C++
PRESENTED BY –
Darpen Patel (130110107032)
Jay Patel (130110107036)
Gujarat Technological University
G.H Patel College of Engineering and Technology
Department of Computer Engineering
Operator Overloading
Introduction
• Operator Overloading provides a flexible option for the creation of new
definitions for most of the c++ operators.
• We can overload all the c++ operators except the following:
 Class member access operators (., .*).
 Scope resolution operator (::).
 Size operator (sizeof).
 Conditional operator (?:).
• Overloading an operator
• Write function definition as normal
• Function name is keyword operator followed by the symbol for the
operator being overloaded
Ex. Operator+ used to overload the addition operator (+)
• Using operators
• To use an operator on a class object it must be overloaded unless the
assignment operator(=)or the address operator(&)
• Assignment operator by default performs memberwise assignment
• Address operator (&) by default returns the address of an object
Introduction
return type classname :: operator op(arglist)
{
function body //task defined
}
SYNTAX FOR OPERATOR OVERLOADING
• Operator functions are declared in the class using prototypes as follows:
 vector operator + (vector);
 vector operator – ();
 friend vector operator + (vector, vector);
 friend vector operator – (vector);
 vector operator – (vector &a);
 int operator == (vector);
 friend int operator == (vector, vector);
• Arguments may be passed either by value or by reference.
RULES FOR OVERLOADING OPERATOR
• Only existing operator can be overloaded .New operator cannot be
created.
• The overloaded operator must have at least one operand that is of user
defined type.
• Overloaded operator follow the syntax rules of original operators.
They cannot be changed.
• When using binary operator overloaded through member function the
left hand operand must be an object of the relevant class .
Restrictions on Operator Overloading
• C++ operators that can be overloaded
• C++ Operators that cannot be overloaded
Operators that cannot be overloaded
. .* :: ?: sizeof
Operators that can be overloaded
+ - * / % ^ & |
~ ! = < > += -= *=
/= %= ^= &= |= << >> >>=
<<= == != <= >= && || ++
-- ->* , -> [] () new delete
new[] delete[]
Overloading Unary Operators
• Overloading unary operators
• Can be overloaded with no arguments or one argument
• Should usually be implemented as member functions
• Example declaration as a member function:
class String {
public:
void operator+() const;
...
};
– Example declaration as a non-member function
class String {
friend void operator+( const String & )
...
}
#include<iostream.h>
#include<conio.h>
class un
{
int x;
public:
un(int a)
{
x=a;
}
operator -()
{
x=-x;
}
void display()
{
cout<<x<<"n";
}
};
Example
void main()
{
un a(10);
a.display();
-a;
a.display();
getch();
}
o/p:-
10
-10
Overloading Binary Operators
• Overloaded Binary operators
• Non-static member function, one argument
• Example:
class String {
public:
String operator+(String );
...
};
• Non-member function, two arguments
– Example:
class String {
friend String operator+(String , String );
...
};
#include<iostream.h>
#include<conio.h>
class bin
{
int a;
public:
bin(int x)
{
a=x;
}
void display()
{
cout<<a;
}
friend int operator +(bin ,bin);
};
Example int operator +(bin x,bin y)
{
int z;
z=x.a+y.a;
return(z);
}
void main()
{
bin x(10),y(20);
int z;
clrscr();
cout<<"x=";
x.display();
cout<<"ny=";
y.display();
z=x+y;
cout<<"nz="<<z;
getch();
}
o/p:-
x=10
y=20
z=30
Manipulation of Strings Using Operator
• In c++ we can add two string by using operator overloading
• We shall be able to use statement like this:
string3=string1+string2;
Example
#include<iostream.h>
#include<conio.h>
#include<string.h>
class str
{
char a[10];
public:
void get()
{
cin>>a;
}
str operator +(str b)
{
str c;
int l,i=0;
l=strlen(a);
for(i=0;i<l;i++)
{
c.a[i]=a[i];
}
l=strlen(b.a);
for(int j=0;j<l;j++)
{
c.a[i]=b.a[j];
i++;
}
c.a[i]=NULL;
return(c);
}
void display()
{
int l,i;
l=strlen(a);
for(i=0;i<l;i++)
cout<<a[i];
}
};
void main()
{
str x,y,z;
clrscr();
x.get();
y.get();
z=x+y;
cout<<"x=";
x.display();
cout<<"ny=";
y.display();
cout<<"nz=";
z.display();
getch();
}
o/p:-
x=hi
y=hello
z=hihello
Overloading of the Subscript[] Operator
#include<iostream.h>
#include<conio.h>
class extra
{
int a[5];
public:
void get()
{
for(int i=0;i<5;i++)
cin>>a[i];
}
int operator [](int x)
{
if(x<=5)
return(a[x-1]);
else
{
x=x%5;
return(a[x-1]);
}
}
};
void main()
{
extra y;
int x,z;
clrscr();
y.get();
cout<<"enter element:";
cin>>x;
z=y[x];
cout<<"element is "<<z;
getch();
}
o/p:-
1
2
3
4
5
enter element:3
3
THANK YOU

More Related Content

What's hot

Operator overloading
Operator overloadingOperator overloading
Operator overloading
Ramish Suleman
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKamal Acharya
 
Operator overloading and type conversion in cpp
Operator overloading and type conversion in cppOperator overloading and type conversion in cpp
Operator overloading and type conversion in cpp
rajshreemuthiah
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
gourav kottawar
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloadingPrincess Sam
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingabhay singh
 
operator overloading in C++
operator overloading in C++operator overloading in C++
Operator overloading
Operator overloadingOperator overloading
Operator overloading
piyush Kumar Sharma
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
Aabha Tiwari
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKumar
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadngpreethalal
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type Conversions
Rokonuzzaman Rony
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloading
Rai University
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
sanya6900
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Dustin Chase
 
Operator overloading
Operator overloading Operator overloading
Operator overloading
Northeastern University
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
ramya marichamy
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
ArunaDevi63
 

What's hot (20)

Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading and type conversion in cpp
Operator overloading and type conversion in cppOperator overloading and type conversion in cpp
Operator overloading and type conversion in cpp
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
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 IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadng
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type Conversions
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloading
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Operator overloading
Operator overloading Operator overloading
Operator overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 

Viewers also liked

NetSure Buyers' Guide
NetSure Buyers' GuideNetSure Buyers' Guide
NetSure Buyers' Guide
Curvature
 
Familia de los gatos
Familia de los gatosFamilia de los gatos
Familia de los gatos
Logos Academy
 
Plan Estrategico USP 2016-2021
Plan Estrategico USP 2016-2021Plan Estrategico USP 2016-2021
Plan Estrategico USP 2016-2021
Facultad de Ciencias de la Salud - USP
 
Trousers for-women
Trousers for-womenTrousers for-women
Trousers for-women
ishitakocher
 
A1 en mi_barrio_hay_actividad
A1 en mi_barrio_hay_actividadA1 en mi_barrio_hay_actividad
A1 en mi_barrio_hay_actividad
Curso Español
 
Mi habitación
Mi habitaciónMi habitación
Mi habitación
profesorasofiak
 
Арга зүйн туршлага
Арга зүйн туршлагаАрга зүйн туршлага
Арга зүйн туршлага
Сэтгэмж Цогцолбор Сургууль
 
3 4- computer organization and archietecture- COA- CO- Computer organization
3 4- computer organization and archietecture- COA- CO- Computer organization3 4- computer organization and archietecture- COA- CO- Computer organization
3 4- computer organization and archietecture- COA- CO- Computer organization
Jay Patel
 
Plan estratégico Tecnología Médica
Plan estratégico Tecnología MédicaPlan estratégico Tecnología Médica
Plan estratégico Tecnología Médica
Facultad de Ciencias de la Salud - USP
 
Defectos inyección
Defectos inyecciónDefectos inyección
Defectos inyección
Carlos Cardelo
 
Ажил мэргэжлийн зөвлөгөө
Ажил мэргэжлийн зөвлөгөөАжил мэргэжлийн зөвлөгөө
Ажил мэргэжлийн зөвлөгөө
Сэтгэмж Цогцолбор Сургууль
 
Cadena de frío
Cadena de fríoCadena de frío
Com documentar a l'escola?
Com documentar a l'escola?Com documentar a l'escola?
Com documentar a l'escola?MRURIS
 
How We Build Features
How We Build FeaturesHow We Build Features
How We Build FeaturesAsh Maurya
 
causes & prevention of cracks
causes & prevention of crackscauses & prevention of cracks
causes & prevention of cracks
Bhavek Sharma
 
Contributor Personality Development- L. D. College of Engineering
Contributor Personality Development- L. D. College of EngineeringContributor Personality Development- L. D. College of Engineering
Contributor Personality Development- L. D. College of Engineering
Mitul Lakhani
 
藥品管灌之投與
藥品管灌之投與藥品管灌之投與
藥品管灌之投與
Ming Chia Lee
 

Viewers also liked (20)

CrackGuard
CrackGuardCrackGuard
CrackGuard
 
NetSure Buyers' Guide
NetSure Buyers' GuideNetSure Buyers' Guide
NetSure Buyers' Guide
 
Familia de los gatos
Familia de los gatosFamilia de los gatos
Familia de los gatos
 
Plan Estrategico USP 2016-2021
Plan Estrategico USP 2016-2021Plan Estrategico USP 2016-2021
Plan Estrategico USP 2016-2021
 
Victor Mussi - CV
Victor Mussi - CVVictor Mussi - CV
Victor Mussi - CV
 
Trousers for-women
Trousers for-womenTrousers for-women
Trousers for-women
 
A1 en mi_barrio_hay_actividad
A1 en mi_barrio_hay_actividadA1 en mi_barrio_hay_actividad
A1 en mi_barrio_hay_actividad
 
Doc10 ficha de observación
Doc10 ficha de observaciónDoc10 ficha de observación
Doc10 ficha de observación
 
Mi habitación
Mi habitaciónMi habitación
Mi habitación
 
Арга зүйн туршлага
Арга зүйн туршлагаАрга зүйн туршлага
Арга зүйн туршлага
 
3 4- computer organization and archietecture- COA- CO- Computer organization
3 4- computer organization and archietecture- COA- CO- Computer organization3 4- computer organization and archietecture- COA- CO- Computer organization
3 4- computer organization and archietecture- COA- CO- Computer organization
 
Plan estratégico Tecnología Médica
Plan estratégico Tecnología MédicaPlan estratégico Tecnología Médica
Plan estratégico Tecnología Médica
 
Defectos inyección
Defectos inyecciónDefectos inyección
Defectos inyección
 
Ажил мэргэжлийн зөвлөгөө
Ажил мэргэжлийн зөвлөгөөАжил мэргэжлийн зөвлөгөө
Ажил мэргэжлийн зөвлөгөө
 
Cadena de frío
Cadena de fríoCadena de frío
Cadena de frío
 
Com documentar a l'escola?
Com documentar a l'escola?Com documentar a l'escola?
Com documentar a l'escola?
 
How We Build Features
How We Build FeaturesHow We Build Features
How We Build Features
 
causes & prevention of cracks
causes & prevention of crackscauses & prevention of cracks
causes & prevention of cracks
 
Contributor Personality Development- L. D. College of Engineering
Contributor Personality Development- L. D. College of EngineeringContributor Personality Development- L. D. College of Engineering
Contributor Personality Development- L. D. College of Engineering
 
藥品管灌之投與
藥品管灌之投與藥品管灌之投與
藥品管灌之投與
 

Similar to Cpp (C++)

OOPS-Seminar.pdf
OOPS-Seminar.pdfOOPS-Seminar.pdf
OOPS-Seminar.pdf
Rithiga6
 
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
University College of Engineering Kakinada, JNTUK - Kakinada, India
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
Lahiru Dilshan
 
Overloading
OverloadingOverloading
Overloading
Mukhtar_Hunzai
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
zindadili
 
OperatorOverloading.ppt
OperatorOverloading.pptOperatorOverloading.ppt
OperatorOverloading.ppt
asadmujtaba001
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloadingPrincess Sam
 
Week7a.pptx
Week7a.pptxWeek7a.pptx
Week7a.pptx
NasirAli233814
 
lecture12.ppt
lecture12.pptlecture12.ppt
lecture12.ppt
UmairMughal74
 
Operator overloading in c++ is the most required.
Operator overloading in c++ is the most required.Operator overloading in c++ is the most required.
Operator overloading in c++ is the most required.
iammukesh1075
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
Prof Ansari
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Juginder Pal Singh
 
Operator overloading (binary)
Operator overloading (binary)Operator overloading (binary)
Operator overloading (binary)
Tirthika Bandi
 
9781285852744 ppt ch13
9781285852744 ppt ch139781285852744 ppt ch13
9781285852744 ppt ch13
Terry Yoast
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
sana younas
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading Charndeep Sekhon
 
Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)
Yaksh Jethva
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
zindadili
 

Similar to Cpp (C++) (20)

OOPS-Seminar.pdf
OOPS-Seminar.pdfOOPS-Seminar.pdf
OOPS-Seminar.pdf
 
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
 
Oops
OopsOops
Oops
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
 
Overloading
OverloadingOverloading
Overloading
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
 
OperatorOverloading.ppt
OperatorOverloading.pptOperatorOverloading.ppt
OperatorOverloading.ppt
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 
Week7a.pptx
Week7a.pptxWeek7a.pptx
Week7a.pptx
 
3d7b7 session4 c++
3d7b7 session4 c++3d7b7 session4 c++
3d7b7 session4 c++
 
lecture12.ppt
lecture12.pptlecture12.ppt
lecture12.ppt
 
Operator overloading in c++ is the most required.
Operator overloading in c++ is the most required.Operator overloading in c++ is the most required.
Operator overloading in c++ is the most required.
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Operator overloading (binary)
Operator overloading (binary)Operator overloading (binary)
Operator overloading (binary)
 
9781285852744 ppt ch13
9781285852744 ppt ch139781285852744 ppt ch13
9781285852744 ppt ch13
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 

More from Jay Patel

Motivational hindi cpd ppt sem 2
Motivational hindi cpd ppt sem 2Motivational hindi cpd ppt sem 2
Motivational hindi cpd ppt sem 2
Jay Patel
 
Cpd- Contributor and Personality Development
Cpd- Contributor and Personality DevelopmentCpd- Contributor and Personality Development
Cpd- Contributor and Personality Development
Jay Patel
 
Cpd- Contributor and Personality Development
Cpd- Contributor and Personality DevelopmentCpd- Contributor and Personality Development
Cpd- Contributor and Personality Development
Jay Patel
 
Cpd- Contribution and Personality Development
Cpd- Contribution and Personality DevelopmentCpd- Contribution and Personality Development
Cpd- Contribution and Personality Development
Jay Patel
 
Coa 09-36-computer organization and archietecture-CO-COA
Coa 09-36-computer organization and archietecture-CO-COACoa 09-36-computer organization and archietecture-CO-COA
Coa 09-36-computer organization and archietecture-CO-COA
Jay Patel
 
Ch7 official=computer organization and archietectur- CO-COA
Ch7 official=computer organization and archietectur- CO-COACh7 official=computer organization and archietectur- CO-COA
Ch7 official=computer organization and archietectur- CO-COA
Jay Patel
 
15 control-computer organization and archietecture-CO-COA
15 control-computer organization and archietecture-CO-COA15 control-computer organization and archietecture-CO-COA
15 control-computer organization and archietecture-CO-COA
Jay Patel
 
9 36-computer organization and archietecture- CO- COA
9 36-computer organization and archietecture- CO- COA9 36-computer organization and archietecture- CO- COA
9 36-computer organization and archietecture- CO- COA
Jay Patel
 
Cn 04,32,36-Cn all chapters1- computer networks- gtu
Cn 04,32,36-Cn all chapters1- computer networks- gtuCn 04,32,36-Cn all chapters1- computer networks- gtu
Cn 04,32,36-Cn all chapters1- computer networks- gtu
Jay Patel
 
Greedy Algorithm-Dijkstra's algo
Greedy Algorithm-Dijkstra's algoGreedy Algorithm-Dijkstra's algo
Greedy Algorithm-Dijkstra's algo
Jay Patel
 
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sGreedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Jay Patel
 

More from Jay Patel (11)

Motivational hindi cpd ppt sem 2
Motivational hindi cpd ppt sem 2Motivational hindi cpd ppt sem 2
Motivational hindi cpd ppt sem 2
 
Cpd- Contributor and Personality Development
Cpd- Contributor and Personality DevelopmentCpd- Contributor and Personality Development
Cpd- Contributor and Personality Development
 
Cpd- Contributor and Personality Development
Cpd- Contributor and Personality DevelopmentCpd- Contributor and Personality Development
Cpd- Contributor and Personality Development
 
Cpd- Contribution and Personality Development
Cpd- Contribution and Personality DevelopmentCpd- Contribution and Personality Development
Cpd- Contribution and Personality Development
 
Coa 09-36-computer organization and archietecture-CO-COA
Coa 09-36-computer organization and archietecture-CO-COACoa 09-36-computer organization and archietecture-CO-COA
Coa 09-36-computer organization and archietecture-CO-COA
 
Ch7 official=computer organization and archietectur- CO-COA
Ch7 official=computer organization and archietectur- CO-COACh7 official=computer organization and archietectur- CO-COA
Ch7 official=computer organization and archietectur- CO-COA
 
15 control-computer organization and archietecture-CO-COA
15 control-computer organization and archietecture-CO-COA15 control-computer organization and archietecture-CO-COA
15 control-computer organization and archietecture-CO-COA
 
9 36-computer organization and archietecture- CO- COA
9 36-computer organization and archietecture- CO- COA9 36-computer organization and archietecture- CO- COA
9 36-computer organization and archietecture- CO- COA
 
Cn 04,32,36-Cn all chapters1- computer networks- gtu
Cn 04,32,36-Cn all chapters1- computer networks- gtuCn 04,32,36-Cn all chapters1- computer networks- gtu
Cn 04,32,36-Cn all chapters1- computer networks- gtu
 
Greedy Algorithm-Dijkstra's algo
Greedy Algorithm-Dijkstra's algoGreedy Algorithm-Dijkstra's algo
Greedy Algorithm-Dijkstra's algo
 
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sGreedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
 

Recently uploaded

The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 

Recently uploaded (20)

The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 

Cpp (C++)

  • 1. OBJECT ORIENTED PROGRAMMING WITH C++ PRESENTED BY – Darpen Patel (130110107032) Jay Patel (130110107036) Gujarat Technological University G.H Patel College of Engineering and Technology Department of Computer Engineering
  • 3. Introduction • Operator Overloading provides a flexible option for the creation of new definitions for most of the c++ operators. • We can overload all the c++ operators except the following:  Class member access operators (., .*).  Scope resolution operator (::).  Size operator (sizeof).  Conditional operator (?:).
  • 4. • Overloading an operator • Write function definition as normal • Function name is keyword operator followed by the symbol for the operator being overloaded Ex. Operator+ used to overload the addition operator (+) • Using operators • To use an operator on a class object it must be overloaded unless the assignment operator(=)or the address operator(&) • Assignment operator by default performs memberwise assignment • Address operator (&) by default returns the address of an object Introduction
  • 5. return type classname :: operator op(arglist) { function body //task defined } SYNTAX FOR OPERATOR OVERLOADING
  • 6. • Operator functions are declared in the class using prototypes as follows:  vector operator + (vector);  vector operator – ();  friend vector operator + (vector, vector);  friend vector operator – (vector);  vector operator – (vector &a);  int operator == (vector);  friend int operator == (vector, vector); • Arguments may be passed either by value or by reference.
  • 7. RULES FOR OVERLOADING OPERATOR • Only existing operator can be overloaded .New operator cannot be created. • The overloaded operator must have at least one operand that is of user defined type. • Overloaded operator follow the syntax rules of original operators. They cannot be changed. • When using binary operator overloaded through member function the left hand operand must be an object of the relevant class .
  • 8. Restrictions on Operator Overloading • C++ operators that can be overloaded • C++ Operators that cannot be overloaded Operators that cannot be overloaded . .* :: ?: sizeof Operators that can be overloaded + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new delete new[] delete[]
  • 9. Overloading Unary Operators • Overloading unary operators • Can be overloaded with no arguments or one argument • Should usually be implemented as member functions • Example declaration as a member function: class String { public: void operator+() const; ... }; – Example declaration as a non-member function class String { friend void operator+( const String & ) ... }
  • 10. #include<iostream.h> #include<conio.h> class un { int x; public: un(int a) { x=a; } operator -() { x=-x; } void display() { cout<<x<<"n"; } }; Example void main() { un a(10); a.display(); -a; a.display(); getch(); } o/p:- 10 -10
  • 11. Overloading Binary Operators • Overloaded Binary operators • Non-static member function, one argument • Example: class String { public: String operator+(String ); ... }; • Non-member function, two arguments – Example: class String { friend String operator+(String , String ); ... };
  • 12. #include<iostream.h> #include<conio.h> class bin { int a; public: bin(int x) { a=x; } void display() { cout<<a; } friend int operator +(bin ,bin); }; Example int operator +(bin x,bin y) { int z; z=x.a+y.a; return(z); } void main() { bin x(10),y(20); int z; clrscr(); cout<<"x="; x.display(); cout<<"ny="; y.display(); z=x+y; cout<<"nz="<<z; getch(); } o/p:- x=10 y=20 z=30
  • 13. Manipulation of Strings Using Operator • In c++ we can add two string by using operator overloading • We shall be able to use statement like this: string3=string1+string2;
  • 14. Example #include<iostream.h> #include<conio.h> #include<string.h> class str { char a[10]; public: void get() { cin>>a; } str operator +(str b) { str c; int l,i=0; l=strlen(a); for(i=0;i<l;i++) { c.a[i]=a[i]; } l=strlen(b.a); for(int j=0;j<l;j++) { c.a[i]=b.a[j]; i++; } c.a[i]=NULL; return(c); } void display() { int l,i; l=strlen(a); for(i=0;i<l;i++) cout<<a[i]; } }; void main() { str x,y,z; clrscr(); x.get(); y.get(); z=x+y; cout<<"x="; x.display(); cout<<"ny="; y.display(); cout<<"nz="; z.display(); getch(); } o/p:- x=hi y=hello z=hihello
  • 15. Overloading of the Subscript[] Operator #include<iostream.h> #include<conio.h> class extra { int a[5]; public: void get() { for(int i=0;i<5;i++) cin>>a[i]; } int operator [](int x) { if(x<=5) return(a[x-1]); else { x=x%5; return(a[x-1]); } } }; void main() { extra y; int x,z; clrscr(); y.get(); cout<<"enter element:"; cin>>x; z=y[x]; cout<<"element is "<<z; getch(); } o/p:- 1 2 3 4 5 enter element:3 3