Assembly Language for Beginners

(nextflipdebug2) #1

3.18. C++


Thus,thesolid_object::get_density()methodwillbelievelikeitisdealingwiththeusualsolid_object
class, and thebox::get_volume()method will work with its three fields, believing this is just the usual
object of classbox.


Thus, we can say, an object of a class, that inherits from several other classes, is representing in memory
as aunitedclass, that contains all inherited fields. And each inherited method is called with a pointer to
the corresponding structure’s part.


Virtual methods


Yet another simple example:


#include <stdio.h>


class object
{
public:
int color;
object() { };
object (int color) { this->color=color; };
virtual void dump()
{
printf ("color=%d\n", color);
};
};


class box : public object
{
private:
int width, height, depth;
public:
box(int color, int width, int height, int depth)
{
this->color=color;
this->width=width;
this->height=height;
this->depth=depth;
};
void dump()
{
printf ("this is box. color=%d, width=%d, height=%d, depth=%d\n", color, width, ⤦
Çheight, depth);
};
};


class sphere : public object
{
private:
int radius;
public:
sphere(int color, int radius)
{
this->color=color;
this->radius=radius;
};
void dump()
{
printf ("this is sphere. color=%d, radius=%d\n", color, radius);
};
};


int main()
{
box b(1, 10, 20, 30);
sphere s(2, 40);


object *o1=&b;
object *o2=&s;
Free download pdf