Inheritance (Virtual Functions & Abstract Classes) Flashcards
What is happening on lines 1, 2, and 3?
Student s;
Grad g;
Undergrad u;
Student *sp1, *sp2, *sp3; // Student pointers
sp1 = &s; // Line 1
sp2 = &g; // Line 2
sp3 = &u; // Line 3
Line 1: A Student pointer (sp1) has been created and is now pointing to a Student object (s).
Line2: A Student pointer (sp2) has been created and is now pointing to a Grad object (g);
Line 3: A Student pointer (sp3) has been created and is now pointing to an Undergrad object (u);
This demonstrates that pointers of a base class type can point to its derivatives
How would you create a single array that allows for a base class and all of its (separately typed) derived classes to be accessed from it?
Create a heterogeneous list – an array of pointers to the base class. The pointers can be pointed to any objects derived from that base.
In this way, it gets around the limitation of all the contents of an array needing to be the same type because the POINTERS technically are all the same type as each other.
What is happening on lines 1-4 of this code?
Student s;
Grad g;
Undergrad u;
Student * list[30000]; // Line 1
list[0] = &g; // Line 2
list[1] = &u; // Line 3
list[2] = new Grad; // Line 4
Line 1: An array of Student pointers called “list” and containing 30,000 indices has been declared.
Line 2: Index 0 of list array has been assigned the value of a Grad object.
Line 3: Index 1 of list array has been assigned the value of an Undergrad object.
Line 4: Index 2 of list array has been assigned to a dynamic Grad object.
Let’s say we have an array of Student pointers and we want to call the function GradeReport() as we loop through the array. What happens when the following code runs?
for (int i = 0; i < size; i++)
list[i]->GradeReport();
GradeReport() is called for the object being pointed to in the index i, however… the version of GradeReport() being called is the base class Student version even if the pointer is pointing to a derived class.
What is “binding”?
A function call is bound to its definition by the compiler, making it static. Since the compiler cannot know exactly what we will be pointing to with a pointer to a Class with derived classes, it cannot guess which version is intended to run so it assumes that the type of the pointer itself in the declaration (e.g. Student *) will be the version to run.
How do you create a specific binding (i.e. a dynamic binding) for a function to a specific derived class at run-time?
The ‘virtual’ keyword.
This overrides a base class function when it is called through a pointer so that it will instead run the target’s version of the function. The function must be declared as virtual.
class Student
{
public:
virtual void GradeReport();
…
};
Now that the function is virtual, it will check to see what object type the Student pointer is actually pointing to, then call that class’ version of the function.
class Student
{
public:
virtual void GradeReport();
…
};
Taking into consideration the above code block, what is happening on each line of the following code?
Student *sp1, *sp2, *sp3; // 1
Student s; Grad g; Undergrad u; // 2
sp1 = &s; // 3
sp2 = &g; // 4
sp3 = &u; // 5
sp1->GradeReport(); // 6
sp2->GradeReport(); // 7
sp3->GradeReport(); // 8
1: 3 pointers of type Student are created.
2: An object of type Student (s), an object of type Grad (g), and an object of type Undergrad (u) are created with default constructors.
3: Student pointer sp1 is pointed to a Student object (s)
4: Student pointer sp2 is pointed at a Grad object (g)
5: Student pointer sp3 is pointed at an Undergrad object (u)
6: This line runs the Student version of GradeReport()
7: This line runs the Grad version of GradeReport()
8: This line runs the Undergrad version of GradeReport()
What is a pure virtual function?
A virtual function which exists as part of a class but does not contain a definition (presumably because it has no use in the context of that class). It’s not required but is often done when the base class version of the function has no work of its own to do.
How is a pure virtual function declared?
By adding =0 to the end of the declaration, which tells the compiler that the declaration will not have a definition.
virtual void GradeReport()=0;
What is an Abstract Class?
Any class that has at least one pure virtual function.
Abstract classes are generally used as base classes for inheritance hierarchies, and they are intended only as a place to put data and functions common to classes derived from them, as well as a place for virtual functions. Abstract classes can still be used to build pointers, which is useful when taking advantage of virtual functions.
Shape is an abstract class.
Describe why these lines are legal or illegal:
Shape s; // Object of type Shape
Shape * sptr; // Pointer of type Shape
Shape s; – Illegal. Shape is an abstract class, and abstract classes cannot be instantiated (i.e. you cannot build an object of this class type).
Shape * sptr; – Legal. sptr could point to derived objects of Shape.