"David White" <no@email.provided> wrote in message news:<u2*******************@nasal.pacific.net.au>. ..

An example main, which would normally be in a new source file:

#include <iostream>

#include "Polynomial.h"

int main()

{

Polynomial p(2);

p[0] = 1.;

p[1] = -2.;

p[2] = 2.;

Polynomial q(p);

Polynomial r = q + p;

std::cout << p << r;

}

You need to define operator== for your Polynomial since it has value

semantics. Then, you use your 'main' as an opportunity to verify your

code. For example, here is a class definition for a Point class:

class Point

{

double d_x;

double d_y;

public:

// CREATORS

Point();

Point(const Point& p);

Point(double x, double y);

~Point();

// MANIPULATORS

Point& operator=(const Point& p);

// ACCESSORS

double x() const;

double y() const;

};

// ==============

// FREE OPERATORS

// ==============

inline bool operator==(const Point& lhs, const Point& rhs);

inline bool operator!=(const Point& lhs, const Point& rhs);

std::ostream& operator<<(std::ostream& out, const Point& p);

-----

Here is a portion of a 'main' routine which validates the value

semantics of Point:

// main.cpp

#include "point.h"

#include <cstdlib>

#include <iostream>

using std::cout;

using std::endl;

#define ASSERT(X) aSsErT(!(X), #X, __LINE__)

int testStatus = 0;

static

void aSsErT(int c, const char *s, int i) {

if (c) {

cout << "Error " << __FILE__ << "(" << i << "): " << s

<< " (failed)" << endl;

if (testStatus >= 0 && testStatus <= 100) ++testStatus;

}

}

int main(int argc, char *argv[])

{

int test = std::atoi(argv[1]);

switch(test){

// Add new test cases here.

case 1: {

// -------------------------------------------------------------------

// Concerns:

// Verify the value semantics of the 'Point' class.

//

// Plan:

// create a default Point (0,0) {a:(0,0)}

// exercise equality operator {a==: a}

// create an explicit Point (0,0) {a:(0,0) b:(0,0)}

// exercise equality operator {b==: a b}

// create a copy of a {a:(0,0) b:(0,0) c:(0,0)}

// exercise equality operator {c==: a b c}

// create a Point at (1,2) {a:(0,0) b:(0,0) c:(0,0)

d:(1,2)}

// exercise equality operator {d==: a b c d}

// assign d to c {a:(0,0) b:(0,0) c:(1,2)

d:(1,2)}

// exercise equality operator {c==: a b c d}

// assign b to a temporary Point (3,4) {a:(0,0) b:(3,4) c:(1,2)

d:(1,2)}

// exercise equality operator {b==: a b c d}

// assign a to a {a:(0,0) b:(3,4) c:(1,2)

d:(1,2)}

// exercise equality operator {a==: a b c d}

//

// Tests:

// default constructor

// copy constructor

// constructor(x, y)

// destructor

// operator=()

// operator==()

// x() const

// y() const

// -------------------------------------------------------------------

cout << "case 1" << endl

<< "======" << endl;

Point a;

ASSERT(0.0 == a.x());

ASSERT(0.0 == a.y());

ASSERT(a == a);

Point b(0.0, 0.0);

ASSERT(0.0 == b.x());

ASSERT(0.0 == b.y());

ASSERT(b == a);

ASSERT(b == b);

Point c(a);

ASSERT(c == a);

ASSERT(c == b);

ASSERT(c == c);

Point d(1,2);

ASSERT(1.0 == d.x());

ASSERT(2.0 == d.y());

ASSERT(d != a);

ASSERT(d != b);

ASSERT(d != c);

ASSERT(d == d);

c = d;

ASSERT(c != a);

ASSERT(c != b);

ASSERT(c == c);

ASSERT(c == d);

b = Point(3,4); // important if you allocate from heap

ASSERT(3.0 == b.x());

ASSERT(4.0 == b.y());

ASSERT(b != a);

ASSERT(b == b);

ASSERT(b != c);

ASSERT(b != d);

a = a; // important if you allocate form heap

ASSERT(a == a);

ASSERT(a != b);

ASSERT(a != c);

ASSERT(a != d);

} break;

default: {

cerr << "WARNING: CASE " << test << " NOT FOUND" << endl;

testStatus = -1;

} break;

}

if(testStatus > 0){

cerr << "Error: non-zero test status: status=" << testStatus

<< endl;

}

return testStatus;

}