C++ Topics and Explanations
Operator Overload

[ Follow Ups ] [ Post Follow Up ] [ C++ Topics and Explanations ]

Posted by Kwan Chan on December 08, 1999 at 03:59:28:

Overloading is yet another very useful feature of C++ that we did not cover in class. C++ allow user overload most of its extend operator. Programmer could increase the purpose and usage of any operator. ?: is the only operator can not be overload. There are a few exception of operator overload. It is not possible to create symbols for new operators, and it is not possible to change the number of operands and operator takes.

The usage of overload an operator is to write a function definition begin with "operator" and followed by the symbol for the operator being overloaded. In class object, there are two exception. (Operator "=" and "&".) The assignment operator "=" may be used with two object of the same class. The address operator "&" return the address of the object in memory and may be used with objects of any class without overloading.

Operator Overload is an advantage for programmer to manipulate on class object by sending messages to it. I enhanced and extended the Time class in chapter 6 in order to overload Time class so I can change the context in which Time class are used.

#ifndef TIME_H
#define TIME_H

class Time {
public:
Time(); //default constructor
Time(int); //overload with one integer
Time(int, int, int); //overload with three integer
Time(char *, int, int, int); //overload with three integer and one character
~Time(); //destructor
void printMilitary();
void printStandard();
void formatPrint();
private:
int hour, time1;
int minute, diff;
int second, time2;
char i[1];
};

#endif

In the declaration of the Time.h, I overloaded Time constructor with different number of integer and character in order to manipulate the Time output. The first constructor overload could be used to change and one integer of Time. The second constructor overload could change the hour, minute, and second. The third overload could change all data of Time including "am" and "pm".

This is useful to have a destructor because Time class use dynamically allocated memory. It will reclaim the dynamic storage allocated by Time. One good reason I found operator overload is useful is that it could save a lot of code.

#include "time.h"

static int n;

Time::Time() { hour = minute = second = 0; n++; }

Time::Time(int hr, int min, int sec) { }

Time::Time(int sec) { }

Time::~Time() { }

Time::Time(char *s, int hr, int min, int sec) { }


void Time::printMilitary()
{
cout << ( hour < 10 ? "0" : "" ) << hour << ":"
<< ( minute < 10 ? "0" : "" ) << minute;
}

void Time::printStandard()
{
cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
<< ":" << ( minute < 10 ? "0" : "" ) << minute
<< ":" << ( second < 10 ? "0" : "" ) << second
<< ( hour < 12 ? " AM " : " PM " );
}

void Time::formatPrint()
{
cout << i << ' ' << hour << ": " << minute << ": " << second << endl;
}


Demonstration an Time class with overloaded operators:

int main() {
Time t1,
t2(10, 10, 10),
t3(100001),
t4("a", 12, 11, 0),

return 0;
}

When the compiler see a declaration like Time t1;, it will automatically invokes the default constructor because the default constructor has no argument. In the above example, it will assigns 0 to hour, minute, and second.

In this declaration t2(10,10,10), the compiler will not send out an error message, instead it will look for a constructor that has 3 integer argument. In case of t3(100001), The compiler will look for a constructor that had one integer as argument, and for the t4("a", 12, 11, 10), the compiler will look for a constructor that had four argument: char, int, int, int.



Follow Ups:


Post a Follow Up:

Name:
E-Mail:

Subject:

Comments:


[ Follow Ups ] [ Post Follow Up ] [ C++ Topics and Explanations ]