PRESENTED BY,
NAME: RITHIGA.M
REG NO:20BCA0052
OBJECT ORIENTED
PROGRAMMING
Contents:
• Introduction
• What is Operator overloading?
• Concept of Operator overloading
• Syntax of Operator overloading
• Rules for Operator overloading
• Advantages of Operator overloading
• Disadvantages of Operator overloading
• Example
• Conclusion
Introduction:
• Operator overloading is an important concept in C++.
• It is a type of polymorphism in which an operator is
overloaded to give user defined meaning to it.
• Overloaded operator is used to perform operation on user-
defined data type.
What is Operator overloading?
• C++ allows you to specify more than one definition for an
operator in the same scope, which is called operator
overloading.
• Operator overloading provides ability to an operator to
perform other manipulation.
• It preserves the original function of the operators and work
with user-defined types only.
Operations that can be perform:
1. Athematic operations: + – * / %
2. Logical operations: && and ||
3. Relational operations: == != >= <=
4. Pointer operators: & and *
5. Memory management operator: new, delete []
Operator overloading:
Concept of Operator overloading:
• Operator overloading is a compile-time polymorphism in
which the operator is overloaded to provide the special
meaning to the user-defined data type.
• Operators overloading is used to overloaded or refines
most of the operators available in C++.
Syntax of Operator overloading :
class ClassName
{
...
public:
…
return-type operator op(params-line)
{
//body of the function
…
}
…
};
Rules for Operator overloading:
• Only built-in operators can be overloaded. New operators can
not be created.
• Arity of the operators cannot be changed.
• Precedence and associativity of the operators cannot be
changed.
• Overloaded operators cannot have default arguments expect
the function call operator() which can have default arguments.
• Operators cannot be overloaded for built in types only. At
least one operand must be used defined type.
Rules cont:
• Assignment (=),subscript ([]),function call(“()”); and
member selection (->) operators must be defined as member
functions.
• Expect the operators specified in point 6, all other operators
can be either member functions or a non member functions.
• Some operators like (assignment)=, (address)& and comma(,)
are by default overloaded.
Advantages of Operator overloading:
• The main advantage of function overloading is that it
improves code readability and allows code reusability.
• Operator overloading enables programmers to use notation
closer to the target domain.
• Operator overloading provides similar syntactic support of
build-in types to user-defined types.
• Operator overloading makes the program easier to understand.
• It speeds up the execution of the program.
Disadvantages of Operator overloading:
• We can only overloaded the operators that exist and cannot
cerate new operators or rename existing operators.
• It is not possible to change the number of operands of an
operator supports.
• Conditional[?:], size of , scope(::), member selector(.),
member pointer selector(.*)and the casting operators.
• All operators keep their default precedence and
associations(what they use for). Only built-in operators can
be overloaded.
• Atleast one of the operands in overloaded must be user-
defined which means we cannot overload the minus operator
to work with one integer and one double.
Example:
• Write a program that adds and subtracts two integer values
using binary C++ Operator Overloading:
include <iostream>
#include<conio.h>
using namespace std;
class temp
{
private:
int data;
public:
void getvalue()
{
cin>>data;
}
temp operator+(temp ob)
{
temp t;
t.data=data + ob.data;
return t;
}
temp operator- (temp ob)
{
temp t;
t.data = data - ob.data;
return t;
}
int display()
{
return data;
}
};
int main()
{
temp obj1, obj2, sum, sub;
cout<<"enter an integer value for obj1: ";
obj1.getvalue();
cout<<"Enter an integer value for obj2: ";
obj2.getvalue();
sum= obj1+obj2;
sub=obj1-obj2;
cout<<"Addition result is = "<<sum.display()<<endl;
cout<<"Subtraction result is = "<<sub.display()<<endl;
getch();
}
Output:
Conclusion:
• Operator overloading is one of the feature of C++ that’s
implements compile time polymorphism.
• Overloading code should be written with greater care.
• Meaning of the operator should be preserved.
• Use any of the two method for declaring the operator
function.
THE END

OOPS-Seminar.pdf

  • 1.
    PRESENTED BY, NAME: RITHIGA.M REGNO:20BCA0052 OBJECT ORIENTED PROGRAMMING
  • 2.
    Contents: • Introduction • Whatis Operator overloading? • Concept of Operator overloading • Syntax of Operator overloading • Rules for Operator overloading • Advantages of Operator overloading • Disadvantages of Operator overloading • Example • Conclusion
  • 3.
    Introduction: • Operator overloadingis an important concept in C++. • It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it. • Overloaded operator is used to perform operation on user- defined data type.
  • 4.
    What is Operatoroverloading? • C++ allows you to specify more than one definition for an operator in the same scope, which is called operator overloading. • Operator overloading provides ability to an operator to perform other manipulation. • It preserves the original function of the operators and work with user-defined types only.
  • 5.
    Operations that canbe perform: 1. Athematic operations: + – * / % 2. Logical operations: && and || 3. Relational operations: == != >= <= 4. Pointer operators: & and * 5. Memory management operator: new, delete []
  • 6.
  • 7.
    Concept of Operatoroverloading: • Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. • Operators overloading is used to overloaded or refines most of the operators available in C++.
  • 8.
    Syntax of Operatoroverloading : class ClassName { ... public: … return-type operator op(params-line) { //body of the function … } … };
  • 9.
    Rules for Operatoroverloading: • Only built-in operators can be overloaded. New operators can not be created. • Arity of the operators cannot be changed. • Precedence and associativity of the operators cannot be changed. • Overloaded operators cannot have default arguments expect the function call operator() which can have default arguments. • Operators cannot be overloaded for built in types only. At least one operand must be used defined type.
  • 10.
    Rules cont: • Assignment(=),subscript ([]),function call(“()”); and member selection (->) operators must be defined as member functions. • Expect the operators specified in point 6, all other operators can be either member functions or a non member functions. • Some operators like (assignment)=, (address)& and comma(,) are by default overloaded.
  • 11.
    Advantages of Operatoroverloading: • The main advantage of function overloading is that it improves code readability and allows code reusability. • Operator overloading enables programmers to use notation closer to the target domain. • Operator overloading provides similar syntactic support of build-in types to user-defined types. • Operator overloading makes the program easier to understand. • It speeds up the execution of the program.
  • 12.
    Disadvantages of Operatoroverloading: • We can only overloaded the operators that exist and cannot cerate new operators or rename existing operators. • It is not possible to change the number of operands of an operator supports. • Conditional[?:], size of , scope(::), member selector(.), member pointer selector(.*)and the casting operators. • All operators keep their default precedence and associations(what they use for). Only built-in operators can be overloaded. • Atleast one of the operands in overloaded must be user- defined which means we cannot overload the minus operator to work with one integer and one double.
  • 13.
    Example: • Write aprogram that adds and subtracts two integer values using binary C++ Operator Overloading: include <iostream> #include<conio.h> using namespace std; class temp { private: int data; public: void getvalue() { cin>>data; }
  • 14.
    temp operator+(temp ob) { tempt; t.data=data + ob.data; return t; } temp operator- (temp ob) { temp t; t.data = data - ob.data; return t; } int display() { return data; } }; int main() { temp obj1, obj2, sum, sub;
  • 15.
    cout<<"enter an integervalue for obj1: "; obj1.getvalue(); cout<<"Enter an integer value for obj2: "; obj2.getvalue(); sum= obj1+obj2; sub=obj1-obj2; cout<<"Addition result is = "<<sum.display()<<endl; cout<<"Subtraction result is = "<<sub.display()<<endl; getch(); } Output:
  • 16.
    Conclusion: • Operator overloadingis one of the feature of C++ that’s implements compile time polymorphism. • Overloading code should be written with greater care. • Meaning of the operator should be preserved. • Use any of the two method for declaring the operator function.
  • 17.