![]() Virtual members and abstract classes grant C++ polymorphic characteristics, most useful for object-oriented projects. ![]() pure virtual members can be called // from the abstract base class #include using namespace std 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: This can be really useful in some circumstances. In this example, objects of different but related types are referred to using a unique type of pointer ( Polygon*) and the proper member function is called every time, just because they are virtual. abstract base class #include using namespace std The syntax is to replace their definition by =0 (an equal sign and a zero):Īn abstract base Polygon class could look like this: 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). Note that despite of the virtuality of one of its members, Polygon was a regular class, of which even an object was instantiated ( poly), with its own definition of member area that always returns 0.Ībstract base classes are something very similar to the Polygon class in the previous example. Therefore, essentially, what the 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.Ī class that declares or inherits a virtual function is called a polymorphic class. 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. The member function area has been declared as virtual in the base class because it is later redefined in each of the derived classes. In this example, all three classes ( Polygon, Rectangle and Triangle) have the same members: width, height, and functions set_values and area. virtual members #include using namespace std The syntax for a function to become virtual is to precede its declaration with the virtual keyword: Member 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 member is a member function that can be redefined in a derived class, while preserving its calling properties through references. 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. For example, the following two statements would be equivalent in the previous example:īut because the type of both 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. pointers to base class #include using namespace std Ĭout and ppoly2->) is valid and allows us to access the members of their pointed objects.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |