Unformatted text preview:

Function OverloadingSquare functionUse overloaded functionFriend function and friend classDeclare a function as friendFriendshipExampleExample continueUse this pointerPowerPoint PresentationSlide 11Operator OverloadingCan we make Complex class easier to use as follow?Yes, Overload the operatorsWhere to define overloading operatorImplementationOverload operator >>implementationFunction OverloadingCan enables several functionOf same nameOf different sets of parameters (at least as far as their types are concerned)Used to create several functions of the same name that perform similar tasks but on different data typesSquare functionint square(int x){ return x*x; }double square(double y){ return y*y;}float square(float z){ return z*z;}Use overloaded functionComplier will search for the match function prototypeint main(){ int x=7; double y=7.5; cout<<“square of “<<x<<“ is “ <<square(x)<<endl; cout<<“Square of “<<y<<“is “ <<square(y)<<endl;}Friend function and friend classA friend function of a class is defined outside that class scopeHas right to access private members of the classUsing friend functions can enhance performanceOften appropriate when a member function cannot be used for certain operationsDeclare a function as friend To declare a function as a friend of a class, precede the function prototype in class definition with key word friendTo declare a class Two as a friend of One, place friend class Two; in the definition of class OneFriendshipFriendship is granted, not takenFriendship is not symmetricIf B is friend of A, I.e. A declared B as friend, you cannot infer A is friend of B unless B declare A as friendFriendship is not transitiveA is friend of B, and B is friend of C, you cannot say that A is friend of CExample class Count{ friend void setX(Count &, int); public: Count(){x=0;} void print() const {cout<<x<<endl;} private: int x;};Example continuevoid setX(Count &c, int val){ c.x = val;}int main(){ Count counter;counter.print();setX(counter, 8);return 0;}Use this pointerEvery object has access to its own address through a pointer called thisThis pointer is not part of objectThis pointer is passed into object (by compiler) as implicit first argument on every non-static member functionclass Test{int x;public:Test( int =0);void print() const;};Test::test(int a){ x = a;}void Test::print() const{ cout<<“ x = “<<x <<“\nthis->x = “<<this->x <<“\n(*this).x = “<<(*this).x <<endl;}int main(){ Test t(12); t.print(); return 0;}x = 12this->x = 12(*this).x = 12Output:Operator OverloadingEnable C++’s operators to work with class object<<, >>, +, -, *, / This operators perform differently depending on their context in integer, floatingThese operators are overloadedC++ allow programmer to overload most operatorsCan we make Complex class easier to use as follow?int main(){ Complex x,y,z; cout<<“Type in two complex numbers\n”; cin>>x >>y; z = x + y; cout<< x <<“+” <<y<<“=“ <<z<<endl;}Type in two complex numbers(2,3)(3,4)(2,3) + (3,4) = (5,7)Yes, Overload the operatorsOperator are overloaded by writing a function definition( header and body)Function name become the keyword operator followed by the symbol for the operator being overloaded operator+ would be used to overload the addition operator(+)Precedence and associativity of an operator cannot be changed by overloadingWhere to define overloading operatorAs non member functionMust be friend of the classSo it is in class definitionclass Complex{ friend ostream& operator<<(ostream&, const Complex &); ...}Function nameReturn typeparametersImplementationostream & operator<<(ostream & out, const Complex &c){ out<<“(“<<c.real<<“,”<<c.img<<“)”; return out; // enables cout<<x<<y;}Overload operator >>In class definition class Complex { friend ostream& operator<< (ostream &, const Complex &); friend istream& operator>> (istream&, Complex &);...}implementationistream & operator>>( istream & input,Complex & c){ input.ignore(); input>>c.real; input.ignore(); input>>c.img; input.ignore(); return


View Full Document

CUNY CS 503 - Function Overloading

Documents in this Course
Load more
Download Function Overloading
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Function Overloading and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Function Overloading 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?