Sunday, June 28, 2009

An example of a complete class declaration



NOTE : -
THIS CONTENT IS NOT ORIGINAL

23. An example of a complete class declaration

Here is an example of a full class declaration:


using namespace std;
#include
#include

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;
}

int 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;

return 0;
}


Output
The module of vector c: 40.8167
The content of vector a: (3, 5)
The oposite of vector a: (-3, -5)
The content of vector b: (0.971275, 0.23796)
k contains: 1.41421



It is also possible to define a function to produces the sum of two vectors without mentioning it inside the vector class definition. Then it will not be a method of the class vector, but rather 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 of a full class definition, above, the multiplication of a vector by a double is defined. Suppose we want the multiplication of a double by a vector to 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, anddelete automatically calls the destructor before deallocating the memory the instance variables take:


using namespace std;
#include
#include

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;
}

int 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: " <<>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;

return 0;
}


Output
(0, 0)
(94, 345)
(94, 343)
(0.77992, 2.89685)
(-60.5984, -132.937)
(3, 5)
(78, 345)
x component of r: 78
x component of r: 78
6.40312
(432, 3)




BIBILOGRAPHY / REFERENCE : - http://www.4p8.com/eric.brasseur/cppcen.html

No comments:

3G S

JAIL BREAK

iPHONE

BILL GATES

StEvE JoBs

steve jobs

STEVE JOBS

Steve Jobs

Love Country

SD

Burroughs

Lift

Joseph Johnson

Apple Rainbow

Apple Blue

Apple Laser Printer