Over the past few years, I've written several articles about representing and manipulating memory-mapped devices in C and C++. Some readers commented that the C++ techniques I presented were lacking in that they failed to use more advanced features such as inheritance and virtual functions.
Classes with virtual functions can be very useful, but they aren't the solution to every problem. Classes that represent memory-mapped devices, such as the ones I've presented, work with real hardware specifically because they don't use virtual functions.
Last month ("Discriminated unions," March 2012), I looked at the sort of problem that virtual functions are good at solving. I described a typical C solution using discriminated unions and discussed the limitations of that approach. This month, I'll explain how to use virtual functions in C++ to solve the same problem. In the not-too-distant future, I'll also show you how C++ typically implements virtual functions by showing how you can emulate them in C.
Although my primary focus here is virtual functions, of necessity I'll discuss class derivation as well. However, I'll cover only those aspects of derivation that I need to explain virtual functions.
Deriving a class
The problem I presented last month involved representing a collection of two-dimensional geometric shapes such as circles, rectangles, and triangles. Each shape object contains some linear or angular distances sufficient to characterise the physical extent of the shape. For example, a circle has a radius, a rectangle has a height and a width, and a triangle has two sides and an angle. Each shape also had common attributes, such as position (planar coordinates), or outline and fill colours.
In C++, you can represent the various shapes as a collection of classes. You start by defining a shape class which captures the properties common to all shapes:
class shape {
public:
shape(); // constructor
double area() const;
double perimeter() const;
~~~
private:
coordinates position;
colour outline, fill;
};
The class declares several functions as public members, including area and perimeter. The intent is that every shape—whether it's a circle, rectangle, triangle, or anything else—will provide these functions. In effect, class shape defines a common interface shared by every specific type of shape.
The shape class also declares data members position, outline, and fill. The intent is that every shape object—no matter what kind of shape it is—will contain storage for these members.
Now you can define classes for each specific shape by deriving them from class shape. For example, you can define the circle class as:
class circle: public shape {
public:
circle(double r); // constructor
double area() const;
double perimeter() const;
~~~
private:
double radius;
};
In the class definition above, the class heading:
class circle: public shape
specifies that class circle is derived from class shape. The "derived from" class is commonly called the "base" class.
The keyword public in the class heading indicates that the derivation is public. C++ also permits private and protected derivation, but I'm going to ignore those variants here, and make the simplifying assumption that all derivation is public. It's by far the most common usage.
A derived class inherits all the data members of its base class. That is, the data members declared in the base class occupy storage in the derived class as well. Members declared private in the base class won't be accessible in the derived class, but they will still occupy storage in derived class objects.
For example, the base class shape has three data members, declared as:
coordinates position;
colour outline, fill;
The derived class circle declares one more data member:
double radius;
Thus, the data storage for a circle object is essentially the same as that of a structure defined as:
struct circle {
coordinates position;
colour outline, fill;
double radius;
};
A derived class also inherits member functions from its base class. However, special member functions such as constructors, destructors, and copy assignment operators aren't inherited.
If the circle class didn't declare area and perimeter as member functions, circle would inherit the functions exactly as they're defined in the shape base class. For example, I have yet to show the definition for shape's area functions, but whatever it is, it can't be right for circle. The problem is that a circle's area depends on its radius, but the radius data member is defined in class circle, not in class shape. Although a derived class may have access to data members inherited from a base class, a base class normally can't see the members declared in its derived classes. Thus, the shape class doesn't know that a circle has a radius.
The definition for the circle's area function is very simple:
double circle::area() const {
return pi * radius * radius;
}
where pi is presumably a previously-defined constant representing Π. The keyword const appearing after the function parameter list indicates that area function treats each circle as a constant object. That is, computing the area of a circle doesn't alter the circle.
A hierarchy of shapes
You can easily create derived classes for additional shapes. For example, here are class definitions for rectangle:
class rectangle: public shape {
public:
rectangle(double h, double w);
double area() const;
double perimeter() const;
~~~
private:
double height, width;
};
and for triangle:
class triangle: public shape {
public:
triangle(double s1, double s2, double a);
double area() const;
double perimeter() const;
~~~
private:
double side1, side2, angle;
};
All of these shapes can be viewed as a hierarchy, typically drawn with the base class at the top, shown in Figure 1.
The base class doesn't know that it has any derived classes. Each derived class knows of its base class, but doesn't know about any of the other derived classes.
[To be continued at Understanding virtual functions in C++ (Part 2)]
文章评论(0条评论)
登录后参与讨论