290 likes | 432 Views
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: 0511261 Fall 2014 Overloading. Friend functions.
E N D
AL-HUSEEN BIN TALAL UNIVERSITY • College of Engineering • Department of Computer Engineering • Object-Oriented Programming • Course No.: 0511261 • Fall 2014 • Overloading
Friend functions • A friend function is a function that can access the non-public members of a class, even though the function itself is not a member of the class. A friend function is a normal function with special access privileges. • Non-public members of a class include all private and protected data fields and functions.
Friend functions…cont. • displayCustomer()function has access to the private fields custNum and balanceDue.Thefunction meets the following conditions: » It requires the class name Customer and the scope resolution operator in the function header. » It must be declared in the public section of the Customer class definition, so that a main()function (or any other client function) can use it. » It does not use the keyword friend in its declaration. • The function displayAsAFriend() is not a Customer member function. As a nonmember function that has access to the private data members custNum and balanceDue: » It cannot use the class name Customer and the scope resolution operator in the function header. » It need not be declared in the public section of the class definition. » It must use the C++ keyword friend in its declaration.
Friend functions…cont. • The prototype void displayCustomer() ;shows that the displayCustomer()function takes no parameters • In reality, the function does take one parameter—the thispointer • All member functions receive a this pointer that contains a reference to the object using the function • displayAsAFriend() does not receive a this pointer, it must receive a copy of the object whose custNum and balanceDue it will use.
Friend functions…cont. • When any function tries to access an object’s private member, the compiler examines the list of function prototypes in the class declaration, and one of three things happens: » The function is found to be a member function of the same class as the object, and access is approved. » The function is found to be a friend function of the class of the object, and access is approved. » The function is not found to be a member or a friend of the class of the object, and access is denied; you receive an error message. • friend functions are useful when you overload input and output operators for a class.
Overloading • You overload the word “open” in your vocabulary when you distinguish between opening a door, opening a bank account, opening your eyes, and opening a computer file. You interpret the meaning of “open” based on the context. • You overload C++ constructors by creating two or more constructors for the same class. Therefore, when a class contains multiple constructors, they are overloaded; they simply have different parameter lists. • You overload ordinary C++ functions when you give two functions the same name, but different parameter lists. For example, you might create multiple versions of an add() function that can accept two, three, or four integer parameters.
Operators Overloading • C++ operators are the symbols you use to perform operations on objects. • C++ operators :arithmetic operators(+, -, *, /), logical operators , insertion and extraction operators (<<,>>). • The C++ operators can be divided roughly into binary and unary, Binary operators take two arguments. Examples are a+b, a-b, a/b, and so on. Unary operators take only one argument: -a, ++a, a--. • Operator overloading is the process by which you apply operators to your own abstract data types. • In C++ it is also possible to overload the built-in C++ operators such as +, -, = and ++ so that they too invoke different functions, depending on their operands. • That is, the + in a+b will add the variables if a and b are integers, but will call a different function if a and b are variables of a user defined type (ex. Point type).
Rules of overloading operators • Operators are overloaded by writing a function , the function name is the keyword operator followed by the symbol for the operator being overloaded (if we overload the assignment operator =, the function name become operator =() ). • You can’t overload operators that don’t already exist in C++. • If a built-in operator is binary, then all overloads of it remain binary. It is also true for unary operators. • Operator precedence and syntax (number of arguments) cannot be changed through overloading.
Overloading unary operator • Unary operators operate on a single operand. Examples are the increment (++) and decrement (--) operators; the unary minus, as in -5; and the logical not (!) operator. • Unary operators take no arguments, they operate on the object for which they were called. Normally, this operator appears on the left side of the object, as in !obj, -obj, and ++obj.
Overloading +,-,~,and ! Operators #include<iostream.h> class point { intx,y; public: point (int=0, int=0) ; //default constructor void print(){cout<<“the x-cor.”<<x<<“\t the y-cor.”<<y<<endl;} point operator+ (){point z(+x,+y);return z;} Point operator- (){point z(-x,-y);return z;} Point operator! (){point z(!x,!y);return z;}//NOT Operator }; Point::Point (intxcor, intycor){ x=xcor; y=ycor;} main(){ point a(1,3);a.print(); a=+a;a.print(); a=-a;a.print(); a=!a;a.print(); }
Overloading the prefix ++and – – Operators • The ++ and -- operators come in “pre” and “post” form • If these operators are used with an assignment statement than different forms has different meanings. • z= ++ z; // preincrement • z= z++; // postincrement • ►The declaration, operator ++ ( ) with no parameters overloads the preincrement operator. • ►The declaration, operator ++ (int) with a single intparameter overloads the postincrement operator. Here, the int parameter serves to distinguish the postincrement form from the preincrement form. This parameter is not used.
Overloading the prefix ++ and – – Operators #include<iostream.h> class point { intx,y; public: point (int=0, int=0) ; //default constructor void print(){cout<<“the x-cor.”<<x<<“\t the y-cor.”<<y<<endl;} point operator++ (){point z(++x,++y);return z;}//pre-increment Point operator++ (int ){point z(x++,y++);return z;}//post-increment point operator-- (){point z(--x,--y);return z;}//pre-decrement Point operator-- (int ){point z(x--,y--);return z;}//post-decrement }; Point::Point (intxcor, intycor){ x=xcor; y=ycor;} main(){ point a(1,3);a.print(); point b,c,d,e; B=c=d=e=a; b=++b; b.print(); c=c++; c.print(); d=--d; d.print(); e=e--; e.print(); }
Overloading the assignment operator(=) #include <iostream.h> class Point { intx,y; public: Point (int=0, int=0) ; //default constructor Point (const Point &c); //copy constructor void operator= (const point &r); }; void Point:: operator= (const point &r) { x = r.x; y = r.y; } Point::Point (intxcor, intycor){ X=xcor; Y=ycor;} Point::Point (const Point &c){ X=c.x; Y=c.y;} main(){ Point a(1,3),b(21,8); Point c; C=b; }
Return value of the assignment operator • When there’s a void return value, you can’t chain the assignment operator (as in a = b = c ). • To fix this, the assignment operator must return a reference to the object that called the operator function (its address). Point & operator= (const point &r){ X=r.x; Y=r.y; Return *this; // returns a reference to the object }
Copy constructor and assignment operator • The difference between the assignment operator and the copy constructor is that the copy constructor actually creates a new object before copying data from another object into it, whereas the assignment operator copies data into an already existing object. • Ex: Pointa,b,d; a=b; //callassignment operator Point c=d; //call copy constructor;
Overloading binary operators #include<iostream.h> class point { intx,y; public: point (int=0, int=0) ; //default constructor void print(){cout<<“the x-cor.”<<x<<“\t the y-cor.”<<y<<endl;} point operator+ (const point & r){point z(x+ r.x, y+ r.y);return z;}//Addition operator point operator- (const point & r){point z(x- r.x, y- r.y);return z;}//Subtraction operator point operator* (const point & r){point z(x* r.x, y* r.y);return z;}//Multiplication operator point operator/ (const point & r){point z(x/ r.x, y/ r.y);return z;}//Division operator }; point::point (intxcor, intycor){ x=xcor; y=ycor;} main(){ point a(5,7);point k(2,4); point b; b=a+ k; b.print(); b=a- k; b.print(); b=a* k; b.print(); b=a/ k; b.print();}
Overloading the compound assignment operators #include<iostream.h> class point { intx,y; public: point (int=0, int=0) ; //default constructor void print(){cout<<“the x-cor.”<<x<<“\t the y-cor.”<<y<<endl;} point & operator+= (const point & r){point z(x+= r.x, y+= r.y);return *this;}//Add and assign operator point & operator-= (const point & r){point z(x-= r.x, y-= r.y);return *this;}//Subtract and assign operator point & operator*= (const point & r){point z(x*= r.x, y*= r.y);return *this;}//Multiply and assign operator point & operator/= (const point & r){point z(x/= r.x, y/= r.y);return *this;}//Divide and assign }; point::point (intxcor, intycor){ x=xcor; y=ycor;} main(){ point a(2,3);point b(6,7); b+=a; b.print(); b-= a; b.print(); b*= a; b.print(); b/= a; b.print(); }
Overloading relational operations #include<iostream.h> class point { intx,y; public: point (int=0, int=0) ; //default constructor void print(){cout<<“the x-cor.”<<x<<“\t the y-cor.”<<y<<endl;} int operator== (const point & r){ int re=0; If (x==r.x) re=1; return re; } int operator!= (const point & r){ int re=0; if (x!=r.x && y!=r.y) re=1; return re; } int operator< (const point & r){ return(x<r.x); } int operator>= (const point & r){ return (x>=r.x && y>= .y); } };
Cont. point::point (intxcor, intycor){ x=xcor; y=ycor;} main(){ point a(2,3);point b(6,7); Cout<<(b==a)<<endl; 0 Cout<<(b!=a)<<endl; 1 Cout<<(b<a)<<endl; 0 Cout<<(b>=a)<<endl; 1 }
Overloading [ ] operator • C++ provides two special operators, the subscript operator[] and the parenthesis operator() • The subscript operator, operator[], is declared like any other function, but called in a manner similar to accessing an array element. • It can be declared as: Class C{ returntype & operator [] (paramtype); • If c is an object of class C, the expression c[i] is interpreted as c.operator[ ](i)
Overloading [ ] operator..cont. #include<iostream.h> class point { intx,y; public: point (int=0, int=0) ; //default constructor void print(){cout<<“the x-cor.”<<x<<“\t the y-cor.”<<y<<endl;} int & operator[](inti){ Switch(i){ case1: return x; break; case 2: return y; break; default : cout<<“out of range”<<endl; } }; point::point (intxcor, intycor){ x=xcor; y=ycor;} main(){ point a(2,3); cout<<a[1]<<a[2]<<a[3]<<endl; }
Overloading function call operator() • The function call operator is unique in that it allows any number of arguments. class C{ returntype operator ( ) (paramtypes); }; If c is an object of class C, the expression c(i, j, k) is interpreted as c.operator( )( i, j, k )
Overloading () operator #include<iostream.h> class point { intx,y; public: point (int=0, int=0) ; //default constructor void print(){cout<<“the x-cor.”<<x<<“\t the y-cor.”<<y<<endl;} int & operator[](intI,int j){ If (i==1|| j==2) return x; If (i==2|| j==1) return y; } }; point::point (intxcor, intycor){ x=xcor; y=ycor;} main(){ point a(2,3); cout<<a(1,2)<<a(2,1)<<a(1,1)<<endl;
Overloading << operator • The << operator is overloaded by C++. It is both a bitwise left-shift operator and an output operator; it is called the insertion operator when used for output. • The << operator acts as an output operator only when cout (or another output stream object) appears on the left side. • When you use cout in a program, you must include a file that defines the cout object, usually with a statement such as #include<iostream>. • The cout object is a member of a class named ostream.
Overloading << operator…cont. #include<iostream.h> class point { intx,y; public: point (int=0, int=0) ; //default constructor void print(){cout<<“the x-cor.”<<x<<“\t the y-cor.”<<y<<endl;} friend ostream& operator<<(ostream& out,const point &r){ return out<<“the x-cor.”<<r.x<<“\t the y-cor.”<<r.y<<endl; } }; point::point (intxcor, intycor){ x=xcor; y=ycor;} main(){ point a(2,3); cout<<a<<endl;