Welcome to pickSourcecode.com Login | Register    
pickZy.com
 Home  | search  | games  | General  | C  | C++  | Java  | Php  | Networking  | Visual Basic  | VC++  | Win32  | MFC  | JavaScript  | Jobs  | JavaScript  | Post jobs
Cpp - Operator Overloading       Share
2008-01-05 |  RatheeshTR  | Viewed: 1527  |    1

Operator Overloading

type operator operator-symbol ( parameter-list )

The operator keyword declares a function specifying what operator-symbol means
whenapplied to instances of a class. This gives the operator more than one meaning, 
or "overloads" it. The compiler distinguishes between the different meanings of an
 operator by examining the types of its operands.

You can redefine the function of most built-in operators globally or on a class-by-class
 basis. Overloaded operators are implemented as functions.

The name of an overloaded operator is operatorx, where x is the operator as it appears in
 Table 12.2. For example, to overload the addition operator, you define a function called
 operator+. Similarly, to overload the addition/assignment operator, +=, define a 
function called operator+=.

Although overloaded operators are usually called implicitly by the compiler when they are
 encountered in code, they can be invoked explicitly the same way as any member or 
nonmember function is called:


Po
int pt;
pt.operator+( 3 );  
//  Call  addition  operator  to  add  3  to  pt.

Example


The following example overloads the + operator to add two complex numbers and returns the

 result.


#include <iostream.h>

class Complex

{

public:

  Complex( double r, double i ) : re(r), im(i) {}

  Complex operator+( Complex &other );

  
void Display( ) {  cout << re << ", " << im << endl; }
private:

  double re, im;

};


//  Operator  overloaded  using  a  member  function
Complex Complex::operator+( Complex &other )

{

return Complex( re + other.re, im + other.im );

}


void main()
{

  Complex a = Complex( 1.2, 3.4 );

  Complex b = Complex( 5.6, 7.8 );

  Complex c = Complex( 0.0, 0.0 );


  c = a + b;

  c.Display();

}




Latest topics
The Preprocessor  Viewed: 303
Type Casting  Viewed: 306
What is conversion constructor?  Viewed: 311
Input/Output Library  Viewed: 314
Definition of OOP:  Viewed: 315
Objects  Viewed: 316
What is Multiple Inheritance?  Viewed: 321
What is Message passing  Viewed: 327
Differentiate between the message and method?  Viewed: 332
What are C++ storage classes?  Viewed: 333

Comments:





Submit comment's

Type:

User Comment's:

Submitted By:
Prof: Software
Tech: C ,Cpp
Send Mail: jmcdoss_05



Related topics
Cpp - c++ interview tips
Cpp - c++ Destructors
What is Encapsulation?
What is Data Abstraction?
What is Inheritance?
What is Polymorphism?
What is Message passing
What is Extensibility?
What is Persistence?
What is Delegation and Genericity?
What is Multiple Inheritance?
Basic concepts of OOPS and Structure of C++ program
what is memory leak?how to avoid that?
When are copy constructors called?
What are all the implicit member functions of the class?

Related References
cpp - pointers in c++
cpp - how to use dll file in the c++ program
cpp - creating and linking dll files to c++ program
cpp - append c++
fibonacci series - c++ - cpp
c++ - factorial sample program
binary search cpp program , c++ samples and tutorials
cpp lexicographical_compare sample program
reversecompare cpp example

Web site contents © Copyright 2007, All rights reserved.
Help | Terms and Conditions | Privacy Policy | About Us