Statement: | Explained in: |
---|---|
int A::b(int c) { } | Classes |
a->b | Data structures |
class A: public B {}; | Friendship and inheritance |
|
|
20 10 |
main
declares two pointers to Polygon
(named ppoly1
and ppoly2
). These are assigned the addresses of rect
and trgl
, respectively, which are objects of type Rectangle
and Triangle
. Such assignments are valid, since both Rectangle
and Triangle
are classes derived from Polygon
.ppoly1
and ppoly2
(with ppoly1->
and ppoly2->
) is valid and allows us to access the members of their pointed objects. For example, the following two statements would be equivalent in the previous example:
|
|
ppoly1
and ppoly2
is pointer to Polygon
(and not pointer to Rectangle
nor pointer to Triangle
), only the members inherited from Polygon
can be accessed, and not those of the derived classes Rectangle
and Triangle
. That is why the program above accesses the area
members of both objects using rect
and trgl
directly, instead of the pointers; the pointers to the base class cannot access the area
members.area
could have been accessed with the pointers to Polygon
if area
were a member of Polygon
instead of a member of its derived classes, but the problem is that Rectangle
and Triangle
implement different versions of area
, therefore there is not a single common version that could be implemented in the base class. virtual
keyword:
|
|
20 10 0 |
Polygon
, Rectangle
and Triangle
) have the same members: width
, height
, and functions set_values
and area
.area
has been declared as virtual
in the base class because it is later redefined in each of the derived classes. Non-virtual members can also be redefined in derived classes, but non-virtual members of derived classes cannot be accessed through a reference of the base class: i.e., if virtual
is removed from the declaration of area
in the example above, all three calls to area
would return zero, because in all cases, the version of the base class would have been called instead.virtual
keyword does is to allow a member of a derived class with the same name as one in the base class to be appropriately called from a pointer, and more precisely when the type of the pointer is a pointer to the base class that is pointing to an object of the derived class, as in the above example.Polygon
was a regular class, of which even an object was instantiated (poly
), with its own definition of member area
that always returns 0.Polygon
class in the previous example. They are classes that can only be used as base classes, and thus are allowed to have virtual member functions without definition (known as pure virtual functions). The syntax is to replace their definition by =0
(an equal sign and a zero):Polygon
class could look like this:
|
|
area
has no definition; this has been replaced by =0
, which makes it a pure virtual function. Classes that contain at least one pure virtual function are known as abstract base classes.Polygon
could not be used to declare objects like:
|
|
|
|
|
|
20 10 |
Polygon*
) and the proper member function is called every time, just because they are virtual. This can be really useful in some circumstances. For example, it is even possible for a member of the abstract base class Polygon
to use the special pointer this
to access the proper virtual members, even though Polygon
itself has no implementation for this function:
|
|
20 10 |
|
|
20 10 |
ppoly
pointers:
|
|
Polygon
", but the objects allocated have been declared having the derived class type directly (Rectangle
and Triangle
).Previous: Friendship and inheritance | Index | Next: Type conversions |