Operator Overloading  «Prev  Next»
Lesson 3 Types of operators that can be overloaded
Objective Identify the operators that can be overloaded.

Types of Overloadable Operators in C++

Almost all operators can be overloaded, including:
  1. All the arithmetic, logical, comparison, equality, assignment, and bit operators. In addition, the autoincrement and autodecrement operators (++ and --) can have distinct prefix and postfix meanings.
  2. The subscript or index operator [] and the function call ().
  3. The class pointer operator (->) and the member pointer selector operator (->*).
  4. The operators new and delete.

The assignment, function call, subscripting, and class pointer operators can be overloaded only by nonstatic member functions.

Exceptions

The following operators cannot be overloaded:
  1. the member operator (.)
  2. the member object selector operator (.*)
  3. the ternary conditional expression operator (?:)
  4. the sizeof operator
  5. the scope resolution operator (::)

Operator Overloading - Quiz

Click the Quiz link below to take a brief multiple-choice quiz on which operator can be overloaded.
Operator Overloading - Quiz

Member Function Overloading

Let us assume we have decided to standardize and purchase our laptop computers from only one manufacturer. We could then introduce a new constructor with one less parameter for class Lap_Top.
Lap_Top(const std::string& proc, int ram, int disk,
double screen, double wei) :
Computer(DEFAULT_LT_MAN, proc, ram, disk), screen_size(screen),
weight(wei) {}

We now have two constructors with different signatures in class Lap_Top. Having multiple member functions with the same name but different signatures in a class is called member function overloading.

Calling Base-Class Functions

Form:
base-class::function-name()
base-class::function-name(argument-list)

Example:
Computer::to_string()

Meaning: Using the prefix base-class:: in a call to function function-name calls the function with that name defined in the base class of the current class.
Now we have two ways to create new Lap_Top objects. Both of the following statements are valid:
Lap_Top ltp1("Intel P4 2.8", 256, 40, 14, 6.5);
Lap_Top ltp2("MicroSys", "AMD Athlon 2000", 256, 40, 15, 7.5);

Because the manufacturer string is not specified for ltp1, its manufacturer is DEFAULT_LT_MAN.