C++ operator overloading

C++ Operator overloading

Operator overloading is one of the many exciting features of C++ programming language. It is an important technique that has enhanced or increase the power of extensibility of c++. We have stated more than once that c++ language tries to make the user defined data types behaves in much the same way as the built-in-types. For instance, c++ language permits us to add two variables of user-defined types with the same syntax that is applied to the basic types. This means that c++ has the ability to provide the operators with a special meanings to an operator is known as operator overloading.

Operator overloading provides a flexible option for the creation of new definitions for most of the c++ programming operators. We can almost create a new language of our own by creative use of the function and operator overloading techniques.

We can overload(give additional meaning to) all the c++ operators except the following:

● Class member access operators ( ., . *).
● Scope resolution operator (: :).
● Size operator (sizeof).
● Conditional operator (?:).

The excluded operators are very few when compared to the large number of operators which qualify for the operator overloading definition.

Although the semantics of an operator can be extended, we can’t change its syntax, the grammatical rules that govern its use such as the number of operands, precedence and associativity. example manipulation operator will enjoy higher precedence then the addition operator. Remember always, when an operator is overloaded, its original meaning is not lost. For instance, the operator +, which has been overloaded two add two vectors, can still be used to add two integers.

DEFINING OPERATOR OVERLOADING

To define an additional task to an operator, we must specify what it means in relation to the class to which the operator is applied. This is done with the help of a special function, called operator function, which describes the task. The general form of an operator function is:

return type classname: : operator (op –arglist)
{
Function body // task defined
}

Where return type is the type of value returned by the specified operation and op is the operator being overloaded. The op is preceded by the keyword operator. Operator op is the function name.

Operator function must be either member functions (non – static) or friend functions. A basic difference between them is that a friend function will have only one argument for unary operators and two for binary operators, while a function has no arguments for unary operators and only one for binary operators. it is because the object used to invoke the member function is passed implicitly and therefore is available for the member function. This is not the case with friend functions. Arguments may be passed either by value or by reference. Operator functions are declared in the class using prototypes as follows:

vector operator+(vector); // vector addition
vector operator-(); // unary minus
friend vector operator+(vector,vector); // vector addition
friend vector operator – (vector); // unary minus
vector operator- (vector &a); // subtraction
int operator ==(vector); // comparison
friend int operator==(vector,vector) // comparison

vector is a data type of class and may represent both magnitude and direction (as in physics and engineering) or a series of points called elements ( as in mathematics).

The process of overloading involves the following steps:

1. Create a class that defines the data type that is to be used in the overloading operation.
2. Declare the operator function operator op() in the public Area of the class. It may be either a member function or a friend function.
3. Define the operator function to implement the required operations.

Overloaded operator functions can be invoked by expressions such as

op   x   or  x  op

for unary operators and

x  op  y

for binary operators. Op x(or x op) would be interpreted as

operator  op  (x)

for friend functions. Similarly the expression x op y would be interpreted as either

x.operator  op  (y)

in case of friend functions. When both the forms are declared, standard argument matching is applied to resolve any ambiguity.

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise