const int MON=0, TUE=1, WED=2, THU=3, FRI=4, SAT=5, SUN=6;
enum Day {MON, TUE, WED, THU, FRI, SAT, SUN};
Day d = MON;
class Complex
{
public:
float re, im;
Complex( float newRe = 0.0, float newIm = 0.0 )
: re( newRe ), im( newIm )
{
}
Complex add( Complex c )
{
return Complex( re + c.re, im + c.im );
}
void print( ostream & out )
{
out << "(" << re << "+" << im << "i)";
}
~Complex()
{
}
};
float re, im;
int main()
{
Complex c( 1.0 , 2.5 );
c.print( cout );
}
class Complex
{
public:
float re, im;
void print( ostream & out )
{
out << "(";
out << re; // re of this
out << "+";
out << im; // im of this
out << "i)";
}
...
};
class Complex
{
public:
float re, im;
Complex( float newRe = 0.0, float newIm = 0.0 )
: re( newRe ), im( newIm )
{
cout << "Complex number ";
print( cout );
cout << " is born.\n";
}
};
class Complex
{
public:
float re, im;
Complex add( Complex c )
{
return Complex( re + c.re, im + c.im );
}
};
class Complex
{
public:
float re, im;
~Complex()
{
cout << "Complex number: ";
print( cout );
cout << " has died.\n";
}
};
#include "Complex.h"
int main()
{
Complex c1( 1.5, 5.3); /// c1 is born
Complex c2( 2.5, 2.7 ); /// c2 is born
c1.print( cout );
c2.print( cout );
{
Complex result; /// What happens here?
result.print( cout );
result = c1.add( c2 ); /// and here??
result.print( cout );
} /// and here???
c1 = Complex( 2.0, 3.0 ); /// a literal Complex number
c1.print( cout );
return 0;
} /// and here????
ostream & operator << ( ostream & out, Complex c )
{
c.print( out );
return out;
}
int main()
{
Complex mySink( 3.4, 2.2 );
cout << "Sink at location: " << mySink << endl;
...
}
class Complex
{
private:
float re, im;
public:
Complex( float newRe, float newIm );
Complex add( Complex c );
void print( ostream & out );
...
};
ostream & operator << ( ostream & out, Complex c );
#include "Complex.h"
Complex :: Complex( float newRe, float newIm )
: re( newRe ), im( newIm )
{
}
Complex Complex :: add( Complex c )
{
return Complex( re + c.re, im + c.im );
}
void Complex :: print( ostream & out )
{
out << "(" << re "+" << im << "i)";
}
...
#include <iostream.h>
class Integer
{
private:
int value;
public:
Integer( int i = 0 );
void print( ostream & out );
int getValue();
void setValue( int i );
~Integer();
};
ostream & operator << ( ostream & out, Integer i );
#include "Integer.h"
Integer :: Integer( int i )
: value(i)
{
cout << "Integer " << value << " was just born.\n";
}
void Integer :: print( ostream & out )
{
out << value;
}
int Integer :: getValue()
{
return value;
}
void Integer :: setValue( int i )
{
value = i;
}
Integer :: ~Integer()
{
cout << "Integer " << value << " has just died.\n";
}
ostream & operator << ( ostream & out, Integer i )
{
i.print( out );
return out;
}
#include "Integer.h"
int main()
{
Integer i(20);
{
Integer j(30);
{
Integer k(40);
k.setValue( i.getValue() + j.getValue() );
cout << "k is " << k << endl;
}
cout << "j is " << j << endl;
}
cout << "i is " << i << endl;
return 0;
}
Integer 20 was just born. Integer 30 was just born. Integer 40 was just born. k is 50 Integer 50 has just died. j is 30 Integer 30 has just died. i is 20 Integer 20 has just died.