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
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Operator overloading, Class, Function implementation, Single mathematical statement, Alternative way, Mathematical expression, C pp code, Predefined types, Low level function for addition are the points you can learn in this object oriented programming subject.
Typology: Slides
1 / 32
class Complex{ private: double real, img; public: Complex Add(const Complex &); Complex Subtract(const Complex &); Complex Multiply(const Complex &); … } Docsity.com
Complex Complex::Add(
const Complex & c1){
Complex t;
t.real = real + c1.real;
t.img = img + c1.img;
return t;
}
Complex c3 = c1.Add(c2);
Adds the contents of c2 to c1 and assigns it to c3 (copy constructor)
c1+c2+c3+c
c1.Add(c2.Add(c3.Add(c4)))
t1 = c3.Add(c4);
t2 = c2.Add(t1);
t3 = c1.Add(t2);
“ Operator overloading ”
c1+c2+c3+c
int float double char long
float x;
int y;
x = 102.02 + 0.09;
Y = 50 + 47;
The compiler probably calls the correct
overloaded low level function for addition
i.e:
// for integer addition: Add(int a, int b)
// for float addition: Add(float a, float b)
called directly
evaluate the operations they
implement
value in their argument except for ?:
C++ and can’t be overloaded
c1c2+c*
c3+c2c*
both yield the same answer
c1 + c2 + c3 + c
a=b=c is same as a=(b=c)
c1+c2+c3 is same as
(c1+c2)+c
Adding subtraction code inside the
Division operator will take exactly two operands in any case:
b = c / d
Binary operators
Binary operators
Member function:
TYPE 1 CLASS::operator B_OP(
TYPE 2 rhs){
...
}
Binary operators
Non-member function:
TYPE 1 operator B_OP(TYPE 2 lhs, TYPE 3 rhs){
...
}
Binary operators
int operator + (int, int);