Hint: Child class method overrides parent method when called [OK]
Common Mistakes:
Assuming parent method runs instead of child
Thinking code causes compilation error
Expecting no output from method call
4. Find the error in this code snippet:
class Base {
public:
void show() { std::cout << "Base"; }
};
class Derived : Base {
public:
void show() { std::cout << "Derived"; }
};
int main() {
Derived d;
d.show();
return 0;
}
medium
A. Derived class cannot have a function named show()
B. Function show() must be virtual
C. No error, code runs fine
D. Missing access specifier before Base in inheritance
Solution
Step 1: Verify inheritance syntax
class Derived : Base is valid syntax; class inheritance defaults to private.
Step 2: Trace program execution
Derived d; d.show(); calls Derived's public show(), printing "Derived". No errors.
Final Answer:
No error, code runs fine -> Option C
Quick Check:
Private inheritance defaults ok here [OK]
Hint: Classes inherit privately by default in C++ [OK]
Common Mistakes:
Assuming default inheritance is public
Thinking virtual keyword is mandatory here
Believing derived class cannot have same function name
5. You want to model a system where Car and Bike share common features like start() and stop(), but also have their own unique features. How should you use inheritance in C++ to design this?
hard
A. Create a base class Vehicle with common features, then inherit Car and Bike from it
B. Make Car inherit from Bike to reuse code
C. Write all features separately in Car and Bike without inheritance
D. Use inheritance only if Car and Bike have identical features
Solution
Step 1: Identify common features
Both Car and Bike share start() and stop(), so these belong in a common base class.
Step 2: Design inheritance structure
Create a base class Vehicle with common features, then inherit Car and Bike from it to add unique features.
Final Answer:
Create a base class Vehicle with common features, then inherit Car and Bike from it -> Option A
Quick Check:
Use base class for shared features [OK]
Hint: Put shared features in base class, inherit for unique parts [OK]
Common Mistakes:
Inheriting Car from Bike (wrong 'is-a' relationship)
Duplicating code instead of reusing via inheritance
Thinking inheritance requires identical features
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.