CPP Object-Oriented Programming & Class Design 3 — Questions and Answers
Question 1: Which rule of thumb states that if a class defines a destructor, copy constructor, or copy assignment operator, it likely needs all three?
- Rule of Zero
- Rule of Three (Correct answer)
- Rule of Five
- Single Responsibility Principle
Correct answer: Rule of Three
The Rule of Three states that if you explicitly define any of the destructor, copy constructor, or copy assignment operator, you should define all three.
Question 2: What is the effect of declaring a member function 'const' at the end of its declaration?
- It prevents the function from being overridden
- It guarantees the function will not modify the object's state (Correct answer)
- It makes the return value immutable
- It prevents the function from calling virtual functions
Correct answer: It guarantees the function will not modify the object's state
A const member function promises not to modify the object it is called on, allowing it to be called on const objects.
Question 3: In C++ inheritance, what is the order of constructor calls when a Derived class inherits from Base?
- Derived constructor first, then Base constructor
- Base constructor first, then Derived constructor (Correct answer)
- Both constructors run simultaneously
- Only the Derived constructor runs
Correct answer: Base constructor first, then Derived constructor
Base class constructors always run before the derived class constructor to ensure the base subobject is fully initialized first.
Question 4: What is the difference between method overloading and method overriding in C++?
- Overloading changes return type; overriding changes parameter types
- Overloading defines multiple functions with the same name but different signatures in the same scope; overriding redefines a virtual base function in a derived class (Correct answer)
- Overloading is for static methods; overriding is for virtual methods
- They are synonyms in C++
Correct answer: Overloading defines multiple functions with the same name but different signatures in the same scope; overriding redefines a virtual base function in a derived class
Overloading creates multiple functions with the same name but different parameters, while overriding provides a new implementation of a virtual function in a subclass.
Question 5: What does the 'override' specifier in C++11 do when applied to a virtual function in a derived class?
- It forces the function to be inlined
- It causes a compile-time error if the function does not actually override a base virtual function (Correct answer)
- It prevents further overriding in subclasses
- It makes the function pure virtual
Correct answer: It causes a compile-time error if the function does not actually override a base virtual function
The override specifier tells the compiler to verify that the function actually overrides a base class virtual function, catching signature mismatches at compile time.
Question 6: Which design scenario best justifies using multiple inheritance in C++?
- When a class needs multiple constructors
- When a class implements multiple unrelated interfaces or mix-in behaviors (Correct answer)
- When a class has more than one data member
- When a class overrides more than one virtual function
Correct answer: When a class implements multiple unrelated interfaces or mix-in behaviors
Multiple inheritance is most appropriate when a class must fulfill multiple distinct interface contracts or combine independent mix-in capabilities.
Question 7: What problem does the diamond inheritance problem describe in C++?
- A class with four pure virtual functions
- Ambiguity and multiple copies of a shared base class when two classes inherit from the same base and a fourth class inherits from both (Correct answer)
- A circular dependency between two class headers
- Excessive use of templates causing slow compilation
Correct answer: Ambiguity and multiple copies of a shared base class when two classes inherit from the same base and a fourth class inherits from both
The diamond problem arises when two classes share a common base and a class inherits from both, potentially creating two copies of the base subobject.
Which rule of thumb states that if a class defines a destructor, copy constructor, or copy assignment operator, it likely needs all three?