Section 8 : Inheritance Flashcards
(30 cards)
How does public inheritance affect visibility?
Public members stay public, protected stay protected, private stay private (not inherited directly). Visibility is preserved.
How does protected inheritance affect visibility?
Both public and protected members become protected in the derived class.
How does private inheritance affect visibility?
Public and protected members become private in the derived class.
Do derived classes inherit private members of the base class?
No. They exist in the object, but cannot be accessed directly.
What happens if access specifier is not specified during inheritance?
It defaults to private.
What is method overriding?
When a derived class provides a new implementation for a method already defined in the base class.
Does the function signature need to match for overriding?
Yes, the method in the derived class must have the same name and parameters as in the base class.
What is the output of overriding example below?
Base b; b.work(); // base method
Derived d; d.work(); // overridden method
base is working…
derived is working…
Are constructors inherited in C++?
❌ No. Constructors and destructors are not inherited and must be defined in the derived class if needed.
What is the order of constructor calls in inheritance?
Base class constructor
Derived class constructor
What is the order of destructor calls in inheritance?
Derived class destructor
Base class destructor
Can you call a base class constructor from a derived class?
✅ Yes, using an initializer list:
Derived(int a, int b) : Base(a), val(b) { }
Why use initializer lists in inheritance?
To initialize base class members (especially private ones) and member variables before the body of the derived constructor runs.
Can you call a base constructor inside the derived constructor body?
❌ No. That would create a temporary object, not initialize the actual base part of the derived object.
When is the base class constructor called?
Before the derived class constructor, even if not explicitly written (calls default constructor if available).
What happens if base has no default constructor and the derived constructor doesn’t call it?
❌ Compiler error — must explicitly call the base constructor via initializer list.
Are friend functions or classes inherited?
❌ No. Friends of a base class are not friends of the derived class. You must declare them again.
Can you make a protected member in the base class public in the derived class?
❌ No. You can restrict visibility (e.g., public → private), but cannot widen it.
Can you access protected members of the base in the derived class?
✅ Yes, they can be accessed directly in the derived class.
What happens when you create an object of a derived class that doesn’t explicitly call a base constructor?
The default constructor of the base class is called automatically before the derived class constructor, even if the derived class constructor is parameterized.
If a derived class constructor is parameterized but doesn’t use an initializer list, which base constructor is invoked?
The default constructor of the base class is used by default.
C++ does not automatically match constructor arguments from Derived(int) to Base(int) unless you explicitly tell it to via an initializer list.
How can you explicitly call a base class’s parameterized constructor from a derived class
Using an initializer list:
Derived(int x, int y) : Base(x) {
cout «_space;“Derived constructor with y = “ «_space;y «_space;endl;
}
What will happen in this code?
class Base {
public:
Base(int x) { cout «_space;“Base: “ «_space;x «_space;endl; }
};
class Derived: public Base {
public:
Derived(int a) {
cout «_space;“Derived: “ «_space;a «_space;endl;
}
};
int main() {
Derived d(5);
}
compile-time error.
Reason: Base has no default constructor, and Derived does not explicitly call Base(int) — the compiler doesn’t know how to initialize the base part.
How would you fix the above code?
Add an initializer list in Derived to call the base constructor:
Derived(int a) : Base(a) {
cout «_space;“Derived: “ «_space;a «_space;endl;
}