# Example of a full class declaration in C++

Posted On June 28, 2007 by Rajindera Garwal Language:

The following example will explain how to use full class declaration using C++.

#include <iostream.h>

#include <math.h>

class vector

{

public:

double x;

double y;

vector (double = 0, double = 0);

vector operator + (vector);

vector operator - (vector);

vector operator - ();

vector operator * (double a);

double module();

void set_length (double = 1);

};

vector::vector (double a, double b)

{

x = a;

y = b;

}

vector vector::operator + (vector a)

{

return vector (x + a.x, y + a.y);

}

vector vector::operator - (vector a)

{

return vector (x - a.x, y - a.y);

}

vector vector::operator - ()

{

return vector (-x, -y);

}

vector vector::operator * (double a)

{

return vector (x * a, y * a);

}

double vector::module()

{

return sqrt (x * x + y * y);

}

void vector::set_length (double a)

{

double length = this->module();

x = x / length * a;

y = y / length * a;

}

ostream& operator << (ostream& o, vector a)

{

o << "(" << a.x << ", " << a.y << ")";

return o;

}

void main ()

{

vector a;

vector b;

vector c (3, 5);

a = c * 3;

a = b + c;

c = b - c + a + (b - a) * 7;

c = -c;

cout << "The module of vector c: " << c.module() << endl;

cout << "The content of vector a: " << a << endl;

cout << "The oposite of vector a: " << -a << endl;

c.set_length(2); // Transforms c in a vector of size 2.

a = vector (56, -3);

b = vector (7, c.y);

b.set_length(); // Transforms b in an unitary vector.

cout << "The content of vector b: " << b << endl;

double k;

k = vector(1, 1).module(); // k will contain 1.4142.

cout << "k contains: " << k << endl;

}

It is also possible to define the sum of vectors without mentioning

it inside the vector class definition. Then it will not be a method

of the class vector. Just a function that uses vectors:

vector operator + (vector a, vector b)

{

return vector (a.x + b.x, a.y + b.y);

}

In the example above of a full class definition, the multiplication

of a vector by a double is defined. Suppose we want the multiplication

of a double by a vector be defined too. Then we must write an isolated

function outside the class:

vector operator * (double a, vector b)

{

return vector (a * b.x, a * b.y);

}

Of course the keywords new and delete work for class instances too.

What's more, new automatically calls the constructor in order to

initialize the objects, and delete automatically calls the destructor

before deallocating the zone of memory the instance variables take:

#include <iostream.h>

#include <math.h>

class vector

{

public:

double x;

double y;

vector (double = 0, double = 0);

vector operator + (vector);

vector operator - (vector);

vector operator - ();

vector operator * (double);

double module();

void set_length (double = 1);

};

vector::vector (double a, double b)

{

x = a;

y = b;

}

vector vector::operator + (vector a)

{

return vector (x + a.x, y + a.y);

}

vector vector::operator - (vector a)

{

return vector (x - a.x, y - a.y);

}

vector vector::operator - ()

{

return vector (-x, -y);

}

vector vector::operator * (double a)

{

return vector (a * x, a * y);

}

double vector::module()

{

return sqrt (x * x + y * y);

}

void vector::set_length (double a)

{

vector &the_vector = *this;

double length = the_vector.module();

x = x / length * a;

y = y / length * a;

}

ostream& operator << (ostream& o, vector a)

{

o << "(" << a.x << ", " << a.y << ")";

return o;

}

void main ()

{

vector c (3, 5);

vector *r; // r is a pointer to a vector.

r = new vector; // new allocates the memory necessary

cout << *r << endl; // to hold a vectors' variable,

// calls the constructor who will

// initialize it to 0, 0. Then finally

// new returns the address of the vector.

r->x = 94;

r->y = 345;

cout << *r << endl;

*r = vector (94, 343);

cout << *r << endl;

*r = *r - c;

r->set_length(3);

cout << *r << endl;

*r = (-c * 3 + -*r * 4) * 5;

cout << *r << endl;

delete (r); // Calls the vector destructor then

// frees the memory.

r = &c; // r points towards vector c

cout << *r << endl;

r = new vector (78, 345); // Creates a new vector.

cout << *r << endl; // The constructor will initialise

// the vector's x and y at 78 and 345

cout << "x component of r: " << r->x << endl;

cout << "x component of r: " << (*r).x << endl;

delete (r);

r = new vector[4]; // creates an array of 4 vectors

r[3] = vector (4, 5);

cout << r[3].module() << endl;

delete (r); // deletes the array

int n = 5;

r = new vector[n]; // Cute!

r[1] = vector (432, 3);

cout << r[1] << endl;

delete (r);

}