- The is-a relationship represents inheritance.
- In a has-a relationship, an object contains one or more objects of other classes as members.
- One problem with inheritance is that a derived class can inherit data members and member functions it does not need or should not have. It is the class designer’s responsibility to ensure that the capabilities provided by a class are appropriate for future derived classes. Even when a base-class member function is appropriate for a derived class, the derived class often requires that member function to behave in a manner specific to the derived class. In such cases, the base-class member function can be redefined in the derived class with an appropriate implementation.
- Base-class initializer, which uses a member initializer to pass arguments to the base-class constructor.
- The software is said to be fragile or brittle, because a small change in the base class can “break” derived-class implementation. The programmer should be able to change the base-class implementation while still providing the same services to derived classes.
- Access redefined base-class members by qualifying their names with the base-class name and binary scope resolution operator (::).
- Run-time type information (RTTI) and dynamic casting, enable a program to determine the type of an object at execution time and act on that object accordingly.
- Once a function is declared virtual, it remains virtual all the way down the inheritance hierarchy from that point, even if that function is not explicitly declared virtual when a class overrides it.
- Choosing the appropriate function to call at execution time (rather than at compile time) is known as dynamic binding or late binding.
- When a virtual function is called by referencing a specific object by name and using the dot member-selection operator (e.g., squareObject.draw()), the function invocation is resolved at compile time (this is called static binding) and the virtual function that is called is the one defined for (or inherited by) the class of that particular object this is not polymorphic behavior.
- A class is made abstract by declaring one or more of its virtual functions to be “pure.” A pure virtual function is specified by placing “= 0” in its declaration. As in: virtual void draw() const = 0; // pure virtual draw
- The “=0” is known as a pure specifier.
- The difference between a virtual function and a pure virtual function is that a virtual function has an implementation and gives the derived class the option of overriding the function; by contrast, a pure virtual function does not provide an implementation and requires the derived class to override the function (for that derived class to be concrete; otherwise the derived class remains abstract).
Polymorphism, Virtual Functions and Dynamic Binding “Under the Hood”
- It is certainly possible to program for polymorphism in non-object-oriented languages such as C, but doing so requires complex and potentially dangerous pointer manipulations.
- Standard Template Library (STL), the STL components were implemented without polymorphism and virtual functions this was done to avoid the associated execution-time overhead and achieve optimal performance to meet the unique requirements of the STL.
- Polymorphism is accomplished through three levels of pointers (i.e., “triple indirection”).
- When C++ compiles a class that has one or more virtual functions, it builds a virtual function table (vtable) for that class. An executing program uses the vtable to select the proper function implementation each time a virtual function of that class is called.
- Thus, the compiler compiles an offset or displacement of four bytes (four bytes for each pointer on today’s popular 32-bit machines, and only one pointer needs to be skipped) into the table of machine-language object-code pointers to find the code that will execute the virtual function call.
- Use typeinfo class for more providing more info at run time.
- If a class has virtual functions, provide a virtual destructor, even if one is not required for the class. Classes derived from this class may contain destructors that must be called properly.
- Constructors cannot be virtual. Declaring a constructor virtual is a compilation error.
[…] Chapter 12 & 13 Object-Oriented Programming Inheritance … […]
[…] Chapter 12 & 13 Object-Oriented Programming Inheritance & Polymorphism « Abdelrahman A… […]