1. (TCO 7) How do you distinguish between virtual functions and pure virtual fun
ID: 3851650 • Letter: 1
Question
1. (TCO 7) How do you distinguish between virtual functions and pure virtual functions? How do you use the abstract class and the methods defined inside the abstract class? (Points : 18)
Question 2. 2. (TCO 4) What is inheritance and what are the major benefits of using inheritance? What programming technique shall you follow when the base class methods are not appropriate for the derived class object? (Points : 18)
Question 3. 3. (TCO 6) How does polymorphism promote extensibility? How does polymorphism enables you to program "in the general" rather than "in the specific." Explain the key advantages of programming "in the general." (Points : 18)
Question 2. 2. (TCO 4) What is inheritance and what are the major benefits of using inheritance? What programming technique shall you follow when the base class methods are not appropriate for the derived class object? (Points : 18)
Explanation / Answer
1.
A virtual function is used for function overriding, i.e. dynamic polymorphism. Declaring a function virtual in a base class indicates that this function has to be overridden in the child class, i.e.n it has to be defined in the child classes. Virtual functions ensure that the correct function is called for an object, regardless what kind of pointer is used (base or derived).
While a pure virtual function is used to make a class abstract or concrete. It is a virtual function with no implementation. It is used when no specific definition can be provided and the implementation depends on the type of object. Ex. area method will have different implementations for different shapes.
2.2
The major advantage of inheritance is reusability of code which leads to less development time. Through inheritance we can structure real world problems very easily. Ex. An employee can be a part-time employee or full-time employee, here Employee is the base class and PartTimeEmployee and FullTimeEmployee are the derived classes.
The methods which are not appropriate for derived class can be made private so that they won't be inherited by the derived class.
3.3
Polymorphism allows new sub-classes and methods to be added to a class hierarchy without having to modify the existing code/classes that already use the hierarchy’s interface. Hence polymorphism promotes extensibility.
Dynamic polymorphisbm enables us to program "in the general" because we don't need to know the exact type
of object we have to deal with. All we know is, that the object is derived from some base class and knows how to implement the function we request it to do on it's own. This is achieved by virtual functions. We can use base class pointer to point to either base or derived class object and still call the correct function.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.