C++ 1. In C++, what is a pure virtual method ? 2. In C++, what is an abstract cl
ID: 3716059 • Letter: C
Question
C++
1. In C++, what is a pure virtual method?
2. In C++, what is an abstract class?
3. Assume that Pet is a base class, and Cat is a class derived from Pet using public inheritance. What is the correct way to call the print() method of the Pet class from the print() method of the Cat class?
a. Cat::print();
b. super.print();
c. Pet::print();
d. base.print();
4. When a derived class object is created, in what order are the constructor bodies executed?
a. First the derived class constructor body is executed, then the base class constructor body is executed.
b. First the base class constructor body is executed, then the derived class constructor body is executed.
c. Only the base class constructor body is executed.
d. Only the derived class constructor body is executed.
5. A base class pointer that points to a derived class object can be used to call
a. only methods declared in the derived class.
b. only methods declared in the base class.
c. any method declared in either the base class or the derived class.
d. any method in any class included in your program.
Explanation / Answer
1) What is a pure virtual method?
a) Pure virtual method does not contain implementation it only contains declaration.
b) If the method prototype is assigned to zero than the method becomes pure virtual function.
c) If a class contains pure virtual method then the class automatically becomes abstract class.
d) The definition of pure virtual method must be done in derived class.
e) If the derived class does not defines the pure virtual method then the derived class automatically becomes abstract, and its derived class must define the pure virtual function.
Example:
class Demo
{
// some data member of the class
public:
// Pure Virtual Function
virtual void display() = 0;
};
2) What is an abstract class?
a) An abstract class is basically designed to be used as a base class.
b) We can declare an object of the abstract class but we cannot instantiate the object.
c) An abstract class contains at least one pure virtual function.
d) Abstract class implementation is done in derived class.
e) We cannot use an abstract class object as a parameter type.
f) We cannot use an abstract class object as function return type.
g) We cannot use an abstract class for explicit conversion.
h) We cannot declare an object of an abstract class.
i) We can, declare pointers and references to an abstract class.
Example:
class Demo
{
public:
// Pure Virtual Function
virtual void display() = 0;
};
3. Assume that Pet is a base class, and Cat is a class derived from Pet using public inheritance. What is the correct way to call the print() method of the Pet class from the print() method of the Cat class?
c. Pet::print();
If we will only user print() as a function call within the derived class print() function then it becomes recursion.
4. When a derived class object is created, in what order are the constructor bodies executed?
b. First the base class constructor body is executed, then the derived class constructor body is executed.
Because base class provides its data member to derived class (except private). So base class constructed first then derived class.
5. A base class pointer that points to a derived class object can be used to call.
c. Any method declared in either the base class or the derived class.
Base class pointer can point to both base class object and derived class object. Hence, it can call both base and derived class members (except private)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.