SlideShare a Scribd company logo
1 of 24
Module II
MCAE0405: OBJECT ORIENTED PROGRAMMING
USING C++
4/3/2020 1
Module No.
Content
Teaching Hours
I
Fundamentals of Object Oriented Programming: Procedure Oriented Programming vs. Object Oriented Programming
(OOP).
Object Oriented Programming Concepts: Classes, Objects, Encapsulation, Inheritance, Polymorphism, Dynamic Binding,
Message Passing and Abstraction; Benefits and Applications of Object Oriented Programming.
Introduction to C++: What is C++, A Simple C++ Program, Structure of C++ Program, Dynamic Initialization of
Variables, Reference Variables, Scope Resolution Operators, and Manipulators.
Functions in C++: Call by Value & Reference, Inline Function, Default Arguments, Function Overloading.
Classes and Objects: Creation of Class, Accessing Class Members, Private vs Public Objects, Member Functions, Method
Definition, Constant Member Functions, Overloading Member Functions, Array within a Class, Memory Allocation for
Objects, Static Data Members and Static Member Functions, Arrays of Objects, Objects as Function Arguments, Friend
Functions, and Returning Objects.
Constructor and Destructor: Introduction to Constructors Parameterized Constructors, Multiple Constructors in a Class,
Constructor with Default Arguments, Copy Constructors.
20
II
Operator Overloading: Defining Operator Overloading, Overloading Unary Operator, Overloading Binary Operators,
Overloading Binary Operators Using Friend, Rules for Overloading Operators.
Inheritance: Introduction, Defining Derived Classes, Types of Inheritance- Single, Multiple, Multilevel, Hierarchical,
Hybrid Inheritance, Virtual Base Classes, Abstract Classes, and Constructors to Derived Classes.
Pointers, Virtual Functions: Introduction, Pointers to Objects, Virtual Function Pure Virtual Functions, Object Slicing.
Templates Function & Class Templates, Class Templates with Multiple Parameters.
Exception Handing: Introduction, Basics of Exception Handling, Exception Handling Mechanism, Throwing Mechanism,
Catching Mechanism, Rethrowing an Exception
20
4/3/2020 2
Operator Overloading:
Defining Operator Overloading, Overloading Unary Operator, Overloading
Binary Operators, Overloading Binary Operators Using Friend, Rules for
Overloading Operators.
4/3/2020 3
Operator Overloading
• In C++, we can make operators to work for user defined classes. This means C++
has the ability to provide the operators with a special meaning for a data type, this
ability is known as operator overloading.
• For example, we can overload an operator ‘+’ in a class like String so that we can
concatenate two strings by just using +.
• Other example classes where arithmetic operators may be overloaded are Complex
Number, Fractional Number, Big Integer, etc.
4/3/2020 4
General Format
5
returnType operator*(parameters);

any type

keyword

operator symbol
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.
Can we overload all operators?
4/3/2020 6
Almost all operators can be overloaded except few.
Following is the list of operators that cannot be overloaded.
. (dot)
::
?:
sizeof
Rules For Overloading Operators
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.
7
Rules For Overloading Operators
The following operators that cannot be overloaded:
Size of Size of operator
. Membership operator
.* Pointer-to-member operator
: : Scope resolution operator
? ; Conditional operator
continue…
8
Rules For Overloading Operators continue…
28
The following operators can be over loaded with the use of member functions and
not by the use of friend functions:
Assignment operator =
Function call operator( )
Subscripting operator [ ]
Class member access operator ->
Unary operators, overloaded by means of a member function, take no explicit arguments
and return no explicit values, but, those overloaded by means of a Bfyr:-iGeounradvKfouttanwcartion, take one
reference argument.
Rules For Overloading Operators
Binary operators overloaded through a member function take one
explicit argument and those which are overloaded through a friend
function take two explicit arguments.
When using binary operators overloaded through a member function,
the left hand operand must be an object of the relevant class.
Binary arithmetic operators such as +, -, * and / must explicitly return a
value. They must not attempt to change their own arguments.
continue…
1
0
4/3/2020 11
4/3/2020 12
4/3/2020 13
4/3/2020 14
#include<iostream>
using namespace std;
class Demo
{
int a,b;
public:
Demo(int x,int y)
{
a=x;
b=y;
}
void operator ++()
{
++a;
++b;
}
4/3/2020 15
void operator ++(int)
{
a--;
b--;
}
void display()
{
cout<<"a:"<<a<<endl<<"b:"<<b<<endl;
}
};
main()
{
Demo d1(10,20);
d1.display();
++d1;
d1.display();
d1++;
d1.display();
}
program to overload unary operator.
• Sample Output:
a:10
b:20
a:11
b:21
a:10
b:20
4/3/2020 16
program to overload binary operator.
• #include<iostream>
• #include<string.h>
• using namespace std;
class A
• {
• char *a;
• public:
• void input()
• {
4/3/2020 17
cin>>a;
}
void operator+(A m)
{
strcat(a,m.a);
}
void display()
{
cout<<a;
}
};
void main()
{
A x,y;
x.input();
y.input();
x+y;
x.display();
return 0;
}
• Sample Output:
ABC
XYZ
ABC XYZ
4/3/2020 18
Important points about operator
overloading
1) For operator overloading to work, at least one of the operands
must be a user defined class object.
2) Assignment Operator: Compiler automatically creates a default
assignment operator with every class.
The default assignment operator does assign all members of right
side to the left side and works fine most of the
cases (this behavior is same as copy constructor).
4/3/2020 19
3) Conversion Operator: We can also write conversion operators that can be used
to convert one type to another type.
#include <iostream>
using namespace std;
class Fraction
{
int num, den;
public:
Fraction(int n, int d) { num = n; den = d; }
// conversion operator: return float value of fraction
operator float()
const {
return float(num) / float(den);
} }; 4/3/2020 20
int main() {
Fraction f(2, 5);
float val = f;
cout << val;
return 0;
}
4/3/2020 21
4/3/2020 22
4/3/2020 23
4/3/2020 24

More Related Content

What's hot (20)

Intake 38 data access 3
Intake 38 data access 3Intake 38 data access 3
Intake 38 data access 3
 
Intake 38 3
Intake 38 3Intake 38 3
Intake 38 3
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
Intake 37 ef2
Intake 37 ef2Intake 37 ef2
Intake 37 ef2
 
Intake 38 12
Intake 38 12Intake 38 12
Intake 38 12
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
 
Class and object
Class and objectClass and object
Class and object
 
C++ Basics
C++ BasicsC++ Basics
C++ Basics
 
Intake 38 5
Intake 38 5Intake 38 5
Intake 38 5
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5
 
Intake 38 4
Intake 38 4Intake 38 4
Intake 38 4
 
Structure and Enum in c#
Structure and Enum in c#Structure and Enum in c#
Structure and Enum in c#
 
C sharp chap5
C sharp chap5C sharp chap5
C sharp chap5
 
basics of c++
basics of c++basics of c++
basics of c++
 
OODP Unit 1 OOPs classes and objects
OODP Unit 1 OOPs classes and objectsOODP Unit 1 OOPs classes and objects
OODP Unit 1 OOPs classes and objects
 
Literals,variables,datatype in C#
Literals,variables,datatype in C#Literals,variables,datatype in C#
Literals,variables,datatype in C#
 
Intake 38_1
Intake 38_1Intake 38_1
Intake 38_1
 
Basic c#
Basic c#Basic c#
Basic c#
 
Top C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerTop C Language Interview Questions and Answer
Top C Language Interview Questions and Answer
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 

Similar to Operator Overloading

Operator overloaing
Operator overloaingOperator overloaing
Operator overloaingzindadili
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfesuEthopi
 
OOPS-Seminar.pdf
OOPS-Seminar.pdfOOPS-Seminar.pdf
OOPS-Seminar.pdfRithiga6
 
Unit3_OOP-converted.pdf
Unit3_OOP-converted.pdfUnit3_OOP-converted.pdf
Unit3_OOP-converted.pdfPowerfullBoy1
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKumar
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloadingPrincess Sam
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloadingBalajiGovindan5
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading Charndeep Sekhon
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANINikul4470
 
Object Oriented Programming Short Notes for Preperation of Exams
Object Oriented Programming Short Notes for Preperation of ExamsObject Oriented Programming Short Notes for Preperation of Exams
Object Oriented Programming Short Notes for Preperation of ExamsMuhammadTalha436
 
OOP in C++
OOP in C++OOP in C++
OOP in C++ppd1961
 
08 c-operator-overloadingppt2563
08 c-operator-overloadingppt256308 c-operator-overloadingppt2563
08 c-operator-overloadingppt2563Youth For Peace
 

Similar to Operator Overloading (20)

Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
 
Oops
OopsOops
Oops
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
OOPS-Seminar.pdf
OOPS-Seminar.pdfOOPS-Seminar.pdf
OOPS-Seminar.pdf
 
Unit3_OOP-converted.pdf
Unit3_OOP-converted.pdfUnit3_OOP-converted.pdf
Unit3_OOP-converted.pdf
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
Af7ff syllabuslablist
Af7ff syllabuslablistAf7ff syllabuslablist
Af7ff syllabuslablist
 
OOP_UnitIII.pdf
OOP_UnitIII.pdfOOP_UnitIII.pdf
OOP_UnitIII.pdf
 
3. Polymorphism.pptx
3. Polymorphism.pptx3. Polymorphism.pptx
3. Polymorphism.pptx
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANI
 
Object Oriented Programming Short Notes for Preperation of Exams
Object Oriented Programming Short Notes for Preperation of ExamsObject Oriented Programming Short Notes for Preperation of Exams
Object Oriented Programming Short Notes for Preperation of Exams
 
OOP in C++
OOP in C++OOP in C++
OOP in C++
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
08 c-operator-overloadingppt2563
08 c-operator-overloadingppt256308 c-operator-overloadingppt2563
08 c-operator-overloadingppt2563
 

Recently uploaded

Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...ranjana rawat
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 

Recently uploaded (20)

Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 

Operator Overloading

  • 1. Module II MCAE0405: OBJECT ORIENTED PROGRAMMING USING C++ 4/3/2020 1
  • 2. Module No. Content Teaching Hours I Fundamentals of Object Oriented Programming: Procedure Oriented Programming vs. Object Oriented Programming (OOP). Object Oriented Programming Concepts: Classes, Objects, Encapsulation, Inheritance, Polymorphism, Dynamic Binding, Message Passing and Abstraction; Benefits and Applications of Object Oriented Programming. Introduction to C++: What is C++, A Simple C++ Program, Structure of C++ Program, Dynamic Initialization of Variables, Reference Variables, Scope Resolution Operators, and Manipulators. Functions in C++: Call by Value & Reference, Inline Function, Default Arguments, Function Overloading. Classes and Objects: Creation of Class, Accessing Class Members, Private vs Public Objects, Member Functions, Method Definition, Constant Member Functions, Overloading Member Functions, Array within a Class, Memory Allocation for Objects, Static Data Members and Static Member Functions, Arrays of Objects, Objects as Function Arguments, Friend Functions, and Returning Objects. Constructor and Destructor: Introduction to Constructors Parameterized Constructors, Multiple Constructors in a Class, Constructor with Default Arguments, Copy Constructors. 20 II Operator Overloading: Defining Operator Overloading, Overloading Unary Operator, Overloading Binary Operators, Overloading Binary Operators Using Friend, Rules for Overloading Operators. Inheritance: Introduction, Defining Derived Classes, Types of Inheritance- Single, Multiple, Multilevel, Hierarchical, Hybrid Inheritance, Virtual Base Classes, Abstract Classes, and Constructors to Derived Classes. Pointers, Virtual Functions: Introduction, Pointers to Objects, Virtual Function Pure Virtual Functions, Object Slicing. Templates Function & Class Templates, Class Templates with Multiple Parameters. Exception Handing: Introduction, Basics of Exception Handling, Exception Handling Mechanism, Throwing Mechanism, Catching Mechanism, Rethrowing an Exception 20 4/3/2020 2
  • 3. Operator Overloading: Defining Operator Overloading, Overloading Unary Operator, Overloading Binary Operators, Overloading Binary Operators Using Friend, Rules for Overloading Operators. 4/3/2020 3
  • 4. Operator Overloading • In C++, we can make operators to work for user defined classes. This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. • For example, we can overload an operator ‘+’ in a class like String so that we can concatenate two strings by just using +. • Other example classes where arithmetic operators may be overloaded are Complex Number, Fractional Number, Big Integer, etc. 4/3/2020 4
  • 5. General Format 5 returnType operator*(parameters);  any type  keyword  operator symbol 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.
  • 6. Can we overload all operators? 4/3/2020 6 Almost all operators can be overloaded except few. Following is the list of operators that cannot be overloaded. . (dot) :: ?: sizeof
  • 7. Rules For Overloading Operators 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. 7
  • 8. Rules For Overloading Operators The following operators that cannot be overloaded: Size of Size of operator . Membership operator .* Pointer-to-member operator : : Scope resolution operator ? ; Conditional operator continue… 8
  • 9. Rules For Overloading Operators continue… 28 The following operators can be over loaded with the use of member functions and not by the use of friend functions: Assignment operator = Function call operator( ) Subscripting operator [ ] Class member access operator -> Unary operators, overloaded by means of a member function, take no explicit arguments and return no explicit values, but, those overloaded by means of a Bfyr:-iGeounradvKfouttanwcartion, take one reference argument.
  • 10. Rules For Overloading Operators Binary operators overloaded through a member function take one explicit argument and those which are overloaded through a friend function take two explicit arguments. When using binary operators overloaded through a member function, the left hand operand must be an object of the relevant class. Binary arithmetic operators such as +, -, * and / must explicitly return a value. They must not attempt to change their own arguments. continue… 1 0
  • 15. #include<iostream> using namespace std; class Demo { int a,b; public: Demo(int x,int y) { a=x; b=y; } void operator ++() { ++a; ++b; } 4/3/2020 15 void operator ++(int) { a--; b--; } void display() { cout<<"a:"<<a<<endl<<"b:"<<b<<endl; } }; main() { Demo d1(10,20); d1.display(); ++d1; d1.display(); d1++; d1.display(); } program to overload unary operator.
  • 17. program to overload binary operator. • #include<iostream> • #include<string.h> • using namespace std; class A • { • char *a; • public: • void input() • { 4/3/2020 17 cin>>a; } void operator+(A m) { strcat(a,m.a); } void display() { cout<<a; } }; void main() { A x,y; x.input(); y.input(); x+y; x.display(); return 0; }
  • 19. Important points about operator overloading 1) For operator overloading to work, at least one of the operands must be a user defined class object. 2) Assignment Operator: Compiler automatically creates a default assignment operator with every class. The default assignment operator does assign all members of right side to the left side and works fine most of the cases (this behavior is same as copy constructor). 4/3/2020 19
  • 20. 3) Conversion Operator: We can also write conversion operators that can be used to convert one type to another type. #include <iostream> using namespace std; class Fraction { int num, den; public: Fraction(int n, int d) { num = n; den = d; } // conversion operator: return float value of fraction operator float() const { return float(num) / float(den); } }; 4/3/2020 20 int main() { Fraction f(2, 5); float val = f; cout << val; return 0; }