This is an PPT of C++ Programming Language. This includes the topic of overloading such as Operator Overloading & Function Overloading.
Size: 561.95 KB
Language: en
Added: Apr 07, 2020
Slides: 20 pages
Slide Content
BIRLA INSTITUTE OF TECHNOLOGY,MESRA JAIPUR CAMPUS Submitted By: Apoorva Nagar MCA/25013/18 “OPERATOR OVERLOADING”
OVERLOADING OPERATOR OVERLOADING BY APOORVA NAGAR (MCA/25013/18) & FUNCTION OVERLOADING BY MEGHAJ KR MALLICK (MCA/25017/18)
OPERATOR OVERLOADING
In C++ operator overloading is a specific case of polymorphism, where different operator have different implementations depending upon their arguments. Overloading an operator is basically means assigning additional operation to operator. C++ allows to define the behaviour of operators when applied to objects of the class. Compiler generates appropriate code based on the manner in which operator is used
OPERATORS FUNCTION Operators are overloaded by creating operators functions. Operator function defines the operations that are overloaded operator will perform relative to the class upon which it will work. syntax:
To overload unary operator, operator function have single argument. To overload binary operator, operator function have two arguments. when an operator is used, the operands become the actual arguments of the ‘function call’. operator function can be implemented using function which can be: Member function Non-Member function Friend function
Operator overloading function can be a member function if the Left operand is an Object of the class. Operator overloading function can be non-member function if the left operand is not a object of the class. Operator overloading function can be friend function if it needs access to private and protected members of class
#include<iostream> using namespace std; class Complex { private: int real, imag; public: Complex(int r = 0, int i =0) { real = r; imag = i; } void print() { cout << real << " + i" << imag << endl; } Complex operator + (Complex const & obj ); { Complex res; res.real = real+obj.real ; res.imag = imag+obj.imag ; return res ; } }; int main() { Complex c1(10, 5), c2(2, 4); Complex c3 = c1 + c2; // An example call to "operator+" c3.print(); return 0; } OUTPUT: 12+i9
OVERLOADABLE OPERATORS NON-OVERLOADABLE OPERATORS
RESTRICTIONS ON OPERATOR OVERLOADING Precedence and associativity of an operator cannot be changed. No new operators can be created. Number of operands cannot be changed.i.e. Unary operators remains unary, binary operators remains binary. Meaning of operator should not be changed.i.e. + should not mean subtraction.
ADVANTAGES OF OPERATOR OVERLOADING Extensibility: operator act differently depending on the operands provided. Not limited to work with primitive data type. User can easily access the object to perform any operation.
THANK YOU!!!
FUNCTION OVERLOADING BY MEGHAJ KUMAR MALLICK MCA/25017/18 1 ST YEAR,2 ND SEMESTER
DEFINITION Function Overloading is defined as the process of having two or more function with the same name, but different in parameters is known as function overloading in C++. Function overloading is commonly used to create several function of the same name that perform similar task, but on different data type.
CONTINUED:
WHY OVERLOADING IS USEFUL ? Function overloading allows function that conceptually perform the same task on object of different type to be given same the same name. It provides a convenient notation for manipulating user defined object with conventional operator.
EXAMPLE class printData { public: void print(int i) { cout << "Printing int: " << i << endl; } void print(double f) { cout << "Printing float: " << f << endl; } void print(char* c) { cout << "Printing character: " << c << endl; } };
EXAMPLE CONTINUED: When the above code is compiled and executed, it produces the following result: Printing int: 5 Printing float: 500.263 Printing character: Hello C++
ADVANTAGES Overloaded methods give programmers the flexibility to call a similar method for different types of data. Function overloading is done for code reusability, to save efforts, and also to save memory.