A base class is a class that provides properties and behaviors (methods) which can be inherited by other classes called derived classes.
Click to reveal answer
beginner
What is a derived class?
A derived class is a class that inherits properties and behaviors from a base class and can add its own features or modify existing ones.
Click to reveal answer
beginner
How do you declare a derived class from a base class in C++?
Use the syntax: <br> <code>class DerivedClass : access_specifier BaseClass { ... };</code> <br> where <code>access_specifier</code> is usually <code>public</code>.
Click to reveal answer
intermediate
What happens if a derived class does not define a constructor?
The derived class automatically calls the base class's default constructor to initialize the base part of the object.
Click to reveal answer
intermediate
Can a derived class access private members of its base class directly?
No, private members of a base class are not accessible directly by derived classes. They can be accessed through public or protected methods of the base class.
Click to reveal answer
Which keyword is used to inherit a class in C++?
Aextends
Binherit
Cclass
D:
✗ Incorrect
In C++, inheritance is declared using a colon ':' after the derived class name.
If class B inherits publicly from class A, which members of A are accessible in B?
Aprivate members only
Ball members
Cpublic and protected members
Dnone
✗ Incorrect
Public inheritance allows derived class to access public and protected members of the base class.
What is the default access specifier for inheritance in C++ classes?
Aprivate
Bnone
Cprotected
Dpublic
✗ Incorrect
If no access specifier is given, inheritance is private by default in C++ classes.
Which of these is true about constructors in derived classes?
ADerived class constructors must call base class constructors explicitly.
BBase class default constructor is called automatically if not specified.
CDerived class constructors do not call base class constructors.
DDerived class cannot have constructors.
✗ Incorrect
If a derived class constructor does not specify a base constructor, the base class's default constructor is called automatically.
Can a derived class override a base class method?
AYes, if the base method is virtual.
BNo, methods cannot be overridden.
COnly if the method is private.
DOnly if the method is static.
✗ Incorrect
In C++, a derived class can override a base class method if it is declared virtual in the base class.
Explain how inheritance works between a base class and a derived class in C++.
Think about how the derived class gets features from the base class.
You got /5 concepts.
Describe the role of constructors in base and derived classes during object creation.
Consider which constructor runs first and how the base part is initialized.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of a base class in C++?
easy
A. To store only private data members
B. To create objects directly without inheritance
C. To override functions in derived classes
D. To hold common features shared by derived classes
Solution
Step 1: Understand base class role
A base class is designed to hold common features that multiple derived classes can share.
Step 2: Compare options
To hold common features shared by derived classes correctly states the purpose. Other options describe derived class behavior or incorrect roles.
Final Answer:
To hold common features shared by derived classes -> Option D
Quick Check:
Base class = common features [OK]
Hint: Base class holds shared features for reuse [OK]
Common Mistakes:
Confusing base class with derived class
Thinking base class cannot have data members
Assuming base class is only for private data
2. Which of the following is the correct syntax to declare a derived class Car inheriting from base class Vehicle in C++?
easy
A. class Car : Vehicle {};
B. class Car : public Vehicle {};
C. class Car extends Vehicle {};
D. class Car inherits Vehicle {};
Solution
Step 1: Recall C++ inheritance syntax
In C++, a derived class inherits from a base class using a colon and access specifier like public.
Step 2: Check options
class Car : public Vehicle {}; uses correct syntax: class Car : public Vehicle {}. Others use incorrect keywords or omit access specifier.
Hint: Use colon and public keyword for inheritance [OK]
Common Mistakes:
Omitting access specifier like public
Using wrong keywords like inherits or extends
Forgetting colon before base class name
3. What will be the output of this C++ code?
class Base {
public:
void show() { std::cout << "Base"; }
};
class Derived : public Base {
public:
void show() { std::cout << "Derived"; }
};
int main() {
Derived d;
d.show();
return 0;
}
medium
A. Derived
B. Base
C. Compilation error
D. No output
Solution
Step 1: Understand method overriding in derived class
The derived class Derived has its own show() method which hides the base class method.
Step 2: Check which method is called
Calling d.show() on a Derived object calls the derived class method, printing "Derived".
Final Answer:
Derived -> Option A
Quick Check:
Derived method overrides base method [OK]
Hint: Derived class method hides base method if same name [OK]
Common Mistakes:
Assuming base class method runs without virtual keyword
Expecting compilation error due to method name clash
Thinking both outputs print
4. Identify the error in this code snippet:
class Base {
public:
void display() { std::cout << "Base"; }
};
class Derived : public Base {
public:
void display() { std::cout << "Derived"; }
};
int main() {
Base* ptr = new Derived();
ptr->display();
return 0;
}
medium
A. Missing virtual keyword in base class method
B. Incorrect pointer type for inheritance
C. Derived class method should be static
D. No error, code runs fine
Solution
Step 1: Understand polymorphism with base pointers
Calling display() via a base class pointer calls base method unless it is virtual.
Step 2: Check for virtual keyword
The base class method display() lacks virtual, so ptr->display() calls base version, not derived.
Final Answer:
Missing virtual keyword in base class method -> Option A
Quick Check:
Virtual needed for polymorphic calls [OK]
Hint: Add virtual to base method for polymorphism [OK]
Common Mistakes:
Thinking pointer type is wrong
Believing derived method runs without virtual
Assuming static keyword fixes this
5. Given these classes:
class Animal {
public:
virtual std::string sound() { return "Unknown"; }
};
class Dog : public Animal {
public:
std::string sound() override { return "Bark"; }
};
class Cat : public Animal {
public:
std::string sound() override { return "Meow"; }
};
Which code snippet correctly creates a list of animals and prints their sounds?
hard
A. std::vector<Animal> animals = {Dog(), Cat()}; for(auto& a : animals) std::cout << a.sound() << ' ';
B. std::vector<Dog> animals = {Dog(), Cat()}; for(auto& a : animals) std::cout << a.sound() << ' ';
C. std::vector animals = {new Dog(), new Cat()}; for(auto a : animals) std::cout << a->sound() << ' ';
D. std::vector<Animal> animals = {Dog(), Cat()}; for(auto a : animals) std::cout << a.sound() << ' ';
Solution
Step 1: Understand polymorphism with collections
To preserve derived class behavior, store pointers to base class, not objects by value.
Step 2: Analyze options
std::vector animals = {new Dog(), new Cat()}; for(auto a : animals) std::cout << a->sound() << ' '; uses std::vector<Animal*> with pointers to Dog and Cat, calling sound() polymorphically.
Final Answer:
std::vector<Animal*> animals = {new Dog(), new Cat()}; for(auto a : animals) std::cout << a->sound() << ' '; -> Option C
Quick Check:
Use base class pointers for polymorphic containers [OK]
Hint: Use base class pointers to keep derived behavior [OK]
Common Mistakes:
Storing objects by value causes slicing
Using wrong container type for polymorphism
Forgetting to use pointers or references
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.