Topic : C++ tutorial for C users
Author : Eric Brasseur
Page : << Previous 7  Next >>
Go to page :

double surface()
{
return x * y;
}
};

class trivector: public vector   // trivector is derived from vector
{
public:
double z;                      // added to x and y from vector

trivector (double m=0, double n=0, double p=0): vector (m, n)
{
z = p;                      // Vector constructor will
}                              // be called before trivector
// constructor, with parameters
// m and n.

trivector (vector a)           // What to do if a vector is
{                              // cast to a trivector
x = a.x;
y = a.y;
z = 0;
}

double module ()               // define module() for trivector
{
return sqrt (x*x + y*y + z*z);
}

double volume ()
{
return this->surface() * z;         // or x * y * z
}
};

void main()
{
vector a (4, 5);
trivector b (1, 2, 3);

cout << "a (4, 5)    b (1, 2, 3)    *r = b" << endl << endl;

cout << "Surface of a: " << a.surface() << endl;
cout << "Volume of b: " << b.volume() << endl;
cout << "Surface of base of b: " << b.surface() << endl;

cout << "Module of a: " << a.module() << endl;
cout << "Module of b: " << b.module() << endl;
cout << "Module of base of b: " << b.vector::module() << endl;

trivector k;
k = a;               // thanks to trivector(vector) definition
// copy of x and y,       k.z = 0
vector j;
j = b;               // copy of x and y.       b.z leaved out

vector *r;
r = &b;

cout << "Surface of r: " << r->surface() << endl;
cout << "Module of r: " << r->module() << endl;
}

25
In the program above, r->module() calculates the vector module, using x and y, because r has been declared a vector pointer. The fact r actually points towards a trivector is not taken into account. If you want the program to check the type of the pointed object and choose the appropriate method, then you must declare that method virtual inside the base class.

(If at least one of the methods of the base class is virtual then a "header" of 4 bytes is added to every instance of the classes. This allows the program to determine towards what a vector actually points.)

#include <iostream.h>
#include <math.h>

class vector
{
public:

double x;
double y;

vector (double a = 0, double b = 0)
{
x = a;
y = b;
}

virtual double module()
{
return sqrt (x*x + y*y);
}
};

class trivector: public vector
{
public:
double z;

trivector (double m = 0, double n = 0, double p = 0)
{
x = m;                  // Just for the game,
y = n;                  // here I do not call the vector
z = p;                  // constructor and I make the
}                          // trivector constructor do the
// whole job. Same result.

double module ()
{
return sqrt (x*x + y*y + z*z);
}
};

void test (vector &k)
{
cout << "Test result:          " << k.module() << endl;
}

void main()
{
vector a (4, 5);
trivector b (1, 2, 3);

cout << "a (4, 5)    b (1, 2, 3)" << endl << endl;

vector *r;

r = &a;
cout << "module of vector a: " << r->module() << endl;

r = &b;
cout << "module of trivector b: " << r->module() << endl;

test (a);

test (b);

vector &s = b;

cout << "module of trivector b: " << s.module() << endl;
}

26
Maybe you wonder if a class can be derived from more than one base class. Answer is yes:

#include <iostream.h>
#include <math.h>

class vector
{
public:

double x;
double y;

vector (double a = 0, double b = 0)
{
x = a;
y = b;
}

double surface()
{
return fabs (x * y);
}
};

class number
{
public:

double z;

number (double a)
{
z = a;
}

int is_negative ()
{
if (z < 0) return 1;
else       return 0;
}
};

class trivector: public vector, public number
{
public:

trivector(double a=0, double b=0, double c=0): vector(a,b), number(c)
{
}           // The trivector constructor calls the vector
// constructor, then the number constructor,
// and in this example does nothing more.

double volume()
{
return fabs (x * y * z);
}
};

void main()
{
trivector a(2, 3, -4);

cout << a.volume() << endl;
cout << a.surface() << endl;
cout << a.is_negative() << endl;
}

27
Class derivation allows to construct "more complicated" classes build above other classes. There is another application of class derivation: allow the programmer to write generic functions.

Suppose you define a base class with no variables. It makes no sense to use instances of that class inside your program. But you write a function whose purpose is to sort instances of that class. Well, that function will be able to sort any types of objects provided they belong to a class derived from that base class! The only condition

Page : << Previous 7  Next >>