Programming and Graphics

(Kiana) #1

172 Introduction to C++ Programming and Graphics


void operator ++ ();

The associated class implementation is:


void algebra::operator ++ ()
{
x = x*x;
color= setcolor(x);
}

Once this is done, we can state in the main program:


++A;

where A is a declared point.


Even more interesting, we can twice overload the + operator by inserting
the following declaration in the public section of the class:


algebra operator + ();

accompanied by the implementation:


void algebra::operator + ()
{
x = 2*x;
color= setcolor(x);
}

Once this is done, we can write


+A;

where A is a point.


Consider the classes of circles and squares introduced in Section 6.7. The
following global function defined outside these classes overloads the + operator:


void operator + (circle A, square B)
{
A.print();
B.print();
}

Including in the main program the block of commands:


circle A = circle(0.1, 0.2, 0.3);
square B = square(0.9, 1.2, 5.3);
A+B;
Free download pdf