#OOP_D_ITS - 5th - C++ Oop Operator Overloading
-
Upload
hadziq-fabroyir -
Category
Health & Medicine
-
view
3.910 -
download
0
description
Transcript of #OOP_D_ITS - 5th - C++ Oop Operator Overloading
11/04/2023Hadziq Fabroyir - Informatics ITS 1
C++ OOP :: Operator Overloading
11/04/2023Hadziq Fabroyir - Informatics ITS 2
Function Signatures
A function signature is what the compiler and linker use to identify a function.
In C , functions are identified only by their name
In C++ , a function’s signature includes its name, parameters, and (for member functions) const. It does NOT include the return type.
11/04/2023Hadziq Fabroyir - Informatics ITS 3
Ex: C++ swap( ) Function
We still need separate functions, but they can all have the same name.
For Examples:void swap (int& a, int& b);
void swap (double& a, double& b);
void swap (struct bob& a, struct bob& b);
11/04/2023Hadziq Fabroyir - Informatics ITS 4
Operator Overloading
OverviewMany C++ operator are already overloaded for primitive types. Examples:
+ - * / << >>It is often convenient for our classes to imitate the operations available on primitive types (e.g., + or - ).
Then we can use the same concise notation for manipulating our objects.
11/04/2023Hadziq Fabroyir - Informatics ITS 5
Ex: Complex Number Class
class Complex {public:
Complex (int real = 0, int imagine = 0);int getReal ( ) const;int getImagine ( ) const;void setReal (int n);void setImagine (int d);
private:int real;int imagine;
};
11/04/2023Hadziq Fabroyir - Informatics ITS 6
Using Complex Class
It makes sense to want to perform mathematical operations with Complex objects.
Complex C1 (3, 5), C2 (5, 9), C3;
C3 = C1 + C2; // addition
C2 = C3 * C1; // subtraction
C1 = -C2; // negation
11/04/2023Hadziq Fabroyir - Informatics ITS 7
Operators Are Really Functions
For user-defined types, when you use an operator, you are making a function call.
Consider the expression: C2 + C1This is translated into a function call.
The name of the function is “operator+”
The call is:
C2.operator+(C1);
11/04/2023Hadziq Fabroyir - Informatics ITS 8
Declaring operator+
As a Member Functionclass Complex {
public: const Complex
operator+ (const Complex &operand) const;
…};
Note all of the const’s!
11/04/2023Hadziq Fabroyir - Informatics ITS 9
operator+ Implementation
const Complex Complex :: operator+ (const Complex &operand) const {
Complex sum;// accessor and mutators not required
sum.imagine = imagine + operand.imagine;
// but preferred
sum.setReal( getReal( ) + operand.getReal ( ) ); return sum;
}
11/04/2023Hadziq Fabroyir - Informatics ITS 10
Using operator+ We can now write
C3 = C2 + C1;
We can also use cascading operators.C4 = C3 + C2 + C1;
And we can writeC3 = C2 + 7;
But C3 = 7 + C2 is a compiler error. (Why?)
11/04/2023Hadziq Fabroyir - Informatics ITS 11
operator+ As aNon-member, Non-
friendconst Complex operator+ (const Complex &lhs, // extra parameter
const Complex &rhs) // not const
{ // must use accessors and mutators
Complex sum;sum.setImagine (lhs.getImagine( )
+ rhs.getImagine( ) );sum.setReal (lhs.getReal ( ) + rhs.getReal( ) );return sum;
} // is now commutative
11/04/2023Hadziq Fabroyir - Informatics ITS 12
Printing ObjectsEach object should be responsible for printing itself.
This guarantees objects are always printed the same way.
It allows us to write intuitive output code:
Complex C5 (5, 3);
cout << C5 << endl;
11/04/2023Hadziq Fabroyir - Informatics ITS 13
Operator<<The insertion operator << is a function and can (and should) be overloaded. We can do operator>>, too.
<< is a binary operator.
The left-hand operand is of type ostream&
Therefore, operator<< cannot be a member function. It must be a non-member.
11/04/2023Hadziq Fabroyir - Informatics ITS 14
operator<<
ostream& operator<< (ostream& out, const
Complex& c) {out << c.getReal( );int imagine = c.getImagine( );out << (imagine < 0 ? “ - ” : “ + ” ) out << imagine << “i”;return out;
}
11/04/2023Hadziq Fabroyir - Informatics ITS 15
Operator<< Returns Type ‘ostream &’
Why? So we can write statements such as
cout << C5 << “is a complex number”
OR
cout << C3 << endl << C2 << endl;
<< associates from left to right.
11/04/2023Hadziq Fabroyir - Informatics ITS 16
Overloading Unary Operators
Complex C1(4, 5), C2;
C2 = -C1;
is an example of a unary operator (minus).
We can and should overload this operator as a member function.
11/04/2023Hadziq Fabroyir - Informatics ITS 17
Unary operator-
const Complex Complex :: operator- ( ) const
{Complex x;x.real = -real;x.imagine = imagine;return x;
}
11/04/2023Hadziq Fabroyir - Informatics ITS 18
Overloading =Remember that assignment performs a memberwise (shallow) copy by default.
This is not sufficient when a data member is dynamically allocated.
= must be overloaded to do a deep copy.
11/04/2023Hadziq Fabroyir - Informatics ITS 19
RestrictionsMost of operators can be overloaded.
You can’t make up your own operators.
You can’t overload operators for primitive types (like int).
You can’t change the precedence of an operator.
You can’t change the associativity of an operator.
11/04/2023Hadziq Fabroyir - Informatics ITS 20
Converting between Types
Cast operatorConvert objects into built-in types or other objects
Conversion operator must be a non-static member function.
Cannot be a friend function
Do not specify return type
For user-defined class AA::operator char *() const; // A to char
A::operator int() const; //A to int
A::operator otherClass() const; //A to otherClass
When compiler sees (char *) s it calls
s.operator char*()
11/04/2023Hadziq Fabroyir - Informatics ITS 21
Good Programming Practices
Overload operators so that they mimic the behavior of primitive data types.
Overloaded binary arithmetic operators shouldreturn const objects by valuebe written as non-member functions when appropriate to allow commutativitybe written as non-friend functions (if data member accessors are available)
Overload unary operators as member functions.
Always overload <<
Always overload = for objects with dynamic data members.
11/04/2023Hadziq Fabroyir - Informatics ITS 22
Another ExampleVectors in the Plane
Suppose we want to implement vectors in 2D and the operations involving them.
11/04/2023Hadziq Fabroyir - Informatics ITS 23
For your practice …
Exercise
Vector2D ClassProperties:
double X, double YMethod (Mutators):
(try) all possible operators that can be applied on Vector 2D(define methods of) the remains operation that can’t be overloaded
Lab Session for lower order (Senin, 15.00-17.00)Lab Session for upper order (Senin, 19.00-21.00)
Please provide:the softcopy (send it by email – deadline Sunday 23:59)the hardcopy (bring it when attending the lab session)
11/04/2023Hadziq Fabroyir - Informatics ITS
☺~ Next: C++ OOP :: Inheritance ~☺
[ 24 ]