Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 49 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
49
Dung lượng
149,5 KB
Nội dung
Object OrientedProgrammingwith C++/ Session 4/ 1 of 49
Operator Overloading
Session 4
Object OrientedProgrammingwith C++/ Session
4/ 2 of 49
Session Objectives
■
Describe Operator Overloading
•
Unary operators
•
Binary operators
•
Binary arithmetic operators
•
Compound assignment operators
•
Comparison operators
■
Describe overloading of the Assignment Operator
■
Describe Copy Constructors
Object OrientedProgrammingwith C++/ Session
4/ 3 of 49
Session Objectives (Contd.)
■
Describe conversion functions which help in
conversion
•
from Basic types to User-Defined types
•
from User-Defined types to Basic types
•
between Objects of different Classes
■
Identify operators that cannot be overloaded
Object OrientedProgrammingwith C++/ Session
4/ 4 of 49
Operator Overloading
■
It is the ability to associate an existing operator
with a member function and use it with objects of
its class as its operands
■
Expressions with operators like +, -, >, +=, ==,
etc. can be used only on basic data types like int
and float
■
Operator overloading allows statements like ,
if (obj1>obj2){ . . .}
where obj1 and obj2 are objects of a class.
Object OrientedProgrammingwith C++/ Session
4/ 5 of 49
Operator Overloading(Contd.)
■
Operation of comparing the objects can be
defined in a member function and associated
with the comparison operator.
■
Compiler can distinguish between overloaded
operators by examining the data type of its
operators.
■
Operator overloading is one form of
polymorphism - operational polymorphism.
Object OrientedProgrammingwith C++/ Session
4/ 6 of 49
Points to note
■
Overloading cannot alter the basic function of an
operator, nor change its place in the order of
precedence that is already defined in the
language.
•
++ (increment) and (decrement) can be used
only as unary operators.
•
an overloaded + operator can be used to
multiply two objects but this would make
your code unreadable.
Object OrientedProgrammingwith C++/ Session
4/ 7 of 49
Advantage
■
Makes programs easier to read and debug.
■
Easy to understand that two objects are being
added and the result assigned to a third object, if
you use the syntax
obj3 = obj1 + obj2;
instead of,
obj3.addobjects(obj1,obj2);
Object OrientedProgrammingwith C++/ Session
4/ 8 of 49
The Operator function
■
Operator function: Contains actual instructions
to overload an operator. The operator that has to
be overloaded follows the keyword "operator".
return_type operator op(argument list);
where op is the symbol for the operator that is being
overloaded.
Object OrientedProgrammingwith C++/ Session
4/ 9 of 49
Unary Operators
■
Unary operators have only one operand.
increment operator ++, decrement operator , and unary minus
operator.
■
The increment and decrement operators can be used as
either prefix or postfix operations.
class Sample{
private:
int counter;
public:
Sample()
{counter =0;}
void operator++()
{++counter;}
};
Object OrientedProgrammingwith C++/ Session
4/ 10 of 49
Unary Operators (Contd.)
void main()
{
Sample obj1;
obj1++; //increments counter to 1
++obj1; //increments counter to 2
}
•
If an operator function is found in the
class specifier, the statement to increment
the object obj1++;gets converted, by the
compiler to the following:
obj1.operator++();
[...]... changes to the object that is being copied ObjectOrientedProgrammingwith C++/ Session 4/ 31 of 49 Copy Constructor (Contd.) s Copy constructor is called in three contexts: • when an object of a class is initialised to another of the same class • when an object is passed as an argument to a function • when a function returns an object ObjectOrientedProgrammingwith C++/ Session 4/ 32 of 49 Copy Constructor... s1 object still points to it ObjectOrientedProgrammingwith C++/ Session 4/ 27 of 49 Assignment Operator (Contd.) • Solution: String& String: :operator= (String& s) { delete str; int length = strlen(s.str); str = new char[length+1]; strcpy(str,s.str); return(*this); } • By including this function in the program the error message will not appear ObjectOrientedProgrammingwith C++/ Session 4/ 28 of 49 ... nameless object is initialised to the same values as this object ObjectOrientedProgrammingwith C++/ Session 4/ 22 of 49 Comparison Operators s Comparison and logical operators are binary operators that need two objects to be compared The comparison operators that can be overloaded include =, ==, and != int String: :operator> (String ss) { return(strcmp(str,ss.str) > 0)); } ObjectOriented Programming. .. return temp; //return temp object } s Now we are able to perform addition of objects with a statement, obj3 = obj1 + obj2; //objects of class Sample Object OrientedProgrammingwith C++/ Session 4/ 18 of 49 Binary Arithmetic Operators (Contd.) s The operator + can access two objects • object on the left side of the operator, obj1, is the one that will invoke the function • object on the right hand side,... Object OrientedProgrammingwith C++/ Session 4/ 34 of 49 A typical class s When a class X has a data member of pointer type, the class should have a constructor, assignment operator function, copy constructor and destructor class X{ X(some_value); //constructor X(const X&); //copy constructor X& operator= (const X&); //assignment ~X(); //destructor }; Object OrientedProgrammingwith C++/ Session 4/ ...Unary Operators (Contd.) s A similar function to decrement the object can also be included in the class as: void operator () { counter;} • Can be invoked with the statement obj1; or obj ; s With the overloaded increment and decrement operators, the operator function is executed first, regardless of whether the operator is postfix or prefix ObjectOrientedProgrammingwith C++/ Session 4/ 11 of 49 Unary... operand (object obj1 ) is accessed directly since this is the object invoking the function • Right hand operand is accessed as the function's argument as a.counter s Also possible to do obj4 = obj3 + obj2 + obj1; Possible because return type of the + operator function is an object of type Sample ObjectOrientedProgrammingwith C++/ Session 4/ 19 of 49 Overloaded + Operator for strings String String: :operator+ (String... "to C++" ; String s3; s3 = s1 + s2; ObjectOrientedProgrammingwith C++/ Session 4/ 20 of 49 Compound Assignment Operators void Sample: :operator+ =(Sample a) { counter += a.counter; //addition } • No need for a temporary object The object, whose data member counter is changed, is the object that invokes the function • The function also needs no return value because the result of the assignment operator. .. Session 4/ 12 of 49 Using nameless temporary object s Another way is to create a nameless temporary object and return it class Sample{ private: int counter; public: Sample() //constructor with no argument {counter = 0;} Sample(int c) //constructor with one argument {counter = c;} Sample operator+ +(); }; ObjectOrientedProgrammingwith C++/ Session 4/ 13 of 49 Using nameless temporary object (Contd.)... with C++/ Session 4/ 23 of 49 Assignment OperatorOverloading s s Default assignment operator simply copies the source object to the destination object byte by byte If data members contain pointers and have been allocated memory using the new operator class String{ private: char *str; public: String(char *s = "") { int length = strlen(s); str = new char[length+1]; strcpy(str,s); } ObjectOrientedProgramming .
Object Oriented Programming with C++/ Session 4/ 1 of 49
Operator Overloading
Session 4
Object Oriented Programming with C++/ Session
4/ 2 of 49
Session. types
•
between Objects of different Classes
■
Identify operators that cannot be overloaded
Object Oriented Programming with C++/ Session
4/ 4 of 49
Operator Overloading
■
It