What''s the deal along with operator overloading?, C/C++ Programming

A: It let you to provide an intuitive interface to users of your class, as well as makes it possible for templates to equally work well with classes and built-in/intrinsic types.

Operator overloading let C/C++ operators to have user-defined meanings on user-defined types (classes). Overloaded operators are syntactic sugar for function calls:

class Fred {                      

public:

...

};

#if 0

// Without operator overloading:

Fred add(const Fred& x, const Fred& y); Fred mul(const Fred& x, const Fred& y);

Fred f(const Fred& a, const Fred& b, const Fred& c)

{

return add(add(mul(a,b), mul(b,c)), mul(c,a)); // Yuk...

}

#else

// With operator overloading:

Fred operator+ (const Fred& x, const Fred& y); Fred operator* (const Fred& x, const Fred& y);

Fred f(const Fred& a, const Fred& b, const Fred& c)

{

return a*b + b*c + c*a;

}

#endif

 

Posted Date: 3/20/2013 3:43:07 AM | Location : United States







Related Discussions:- What''s the deal along with operator overloading?, Assignment Help, Ask Question on What''s the deal along with operator overloading?, Get Answer, Expert's Help, What''s the deal along with operator overloading? Discussions

Write discussion on What''s the deal along with operator overloading?
Your posts are moderated
Related Questions
A: Use operator overloading to present a friend left-shift operator, operator #include class Fred { public: friend std::ostream& operator ... private: int i_; // onl

Selection sort - C program: Write a program to define a selection sort. void main()  {   clrscr();   int a[100],ch,n;   cout   cin>>n;   for (int i=0;i

how to convert a general tree into binary tree with example

Presently we are using several spreadsheets to give weekly activity and income reports. Much of the information is being frequently re-keyed into several spreadsheets - we are look

please may i have a brief theory about a c++ program to find net salary of employees

In the final project assignment you are asked to develop an OOP C++ class hierarchy for derivative pricing, using the binomial tree and Black-Scholes option pricing methods. You wi

Aim: To implement a program for following string operations: Length of String. String Concatenation. Substring (provide start index and length) Find character a

Write a program to find minimum number of shelves

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Rahul is a newbie to the programming and while learning the programming language he came to know the following rules: ???• Each program must start with ''{'' and end with '