


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This lecture was delivered by Prof. Prabhat Patel at Ankit Institute of Technology and Science for Advanced Programming for Engineers. It includes: Operator, Overloading, Implementation, Arithmetic, Division, Primitive, Floats, Binary, User-defined, Objects, Compiler
Typology: Slides
1 / 26
This page cannot be seen from the preview
Don't miss anything!



















Fundamentals of Operator Overloading
Restrictions on Operator Overloading
Operators that cannot be overloaded
. .* :: ?: sizeof
Operators that can be overloaded
Restrictions on Operator Overloading
Operator Functions as Class Members vs.
as friend Functions
Example: Complex.h
class Complex {
public: int a,b;
Complex(int ,int); Complex(); ~Complex(); int getA(); void setA(int); int getB(); void setB(int); void printNum(); Complex operator+(Complex&); };
Complex::Complex(int i,int j) { a=i;b=j; } Complex::Complex() {} Complex::~Complex(){} int Complex::getA() { return a; }
void Complex::printNum() { cout<<"The number is:"<<a<<"+"<<b<<"i"<<endl; } Complex Complex::operator+(Complex& compNo) { cout<<"Operator + being Called"<<endl; Complex x(a + compNo.a , b + compNo.b); return (x); }
#include
The member functions ‘addTwo’ and operator+
double Employee::addTwo(const Employee& emp) { double total; total = salary + emp.getSalary(); return total; }
double Employee::operator+(const Employee& emp) { double total; total = salary + emp.getSalary(); return(total); }
Using the Member Functions
void main(void) { double sum Employee Clerk (111, 10000), Driver (222, 6000);
// these three statements do the same thing
sum = Clerk.addTwo(Driver);
sum = Clerk.operator+(Driver); cout<<“sum :”<<sum<<endl; sum = Clerk + Driver; cout<<“sum :”<<sum<<endl; // the syntax for the last one is the most natural // and is easy to remember because it is consistent // with how the + operator works for everything else }