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 OverloadingCan enables several functionOf same nameOf 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 classA friend function of a class is defined outside that class scopeHas right to access private members of the classUsing friend functions can enhance performanceOften 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 friendTo declare a class Two as a friend of One, place friend class Two; in the definition of class OneFriendshipFriendship is granted, not takenFriendship is not symmetricIf 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 friendFriendship is not transitiveA 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 pointerEvery object has access to its own address through a pointer called thisThis pointer is not part of objectThis 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 OverloadingEnable C++’s operators to work with class object<<, >>, +, -, *, / This operators perform differently depending on their context in integer, floatingThese operators are overloadedC++ 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 operatorsOperator 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 operatorAs non member functionMust be friend of the classSo 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