Section 8 : Inheritance Flashcards

(30 cards)

1
Q

How does public inheritance affect visibility?

A

Public members stay public, protected stay protected, private stay private (not inherited directly). Visibility is preserved.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does protected inheritance affect visibility?

A

Both public and protected members become protected in the derived class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How does private inheritance affect visibility?

A

Public and protected members become private in the derived class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Do derived classes inherit private members of the base class?

A

No. They exist in the object, but cannot be accessed directly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens if access specifier is not specified during inheritance?

A

It defaults to private.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is method overriding?

A

When a derived class provides a new implementation for a method already defined in the base class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Does the function signature need to match for overriding?

A

Yes, the method in the derived class must have the same name and parameters as in the base class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the output of overriding example below?

Base b; b.work(); // base method
Derived d; d.work(); // overridden method

A

base is working…
derived is working…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Are constructors inherited in C++?

A

❌ No. Constructors and destructors are not inherited and must be defined in the derived class if needed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the order of constructor calls in inheritance?

A

Base class constructor

Derived class constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the order of destructor calls in inheritance?

A

Derived class destructor

Base class destructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Can you call a base class constructor from a derived class?

A

✅ Yes, using an initializer list:
Derived(int a, int b) : Base(a), val(b) { }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Why use initializer lists in inheritance?

A

To initialize base class members (especially private ones) and member variables before the body of the derived constructor runs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can you call a base constructor inside the derived constructor body?

A

❌ No. That would create a temporary object, not initialize the actual base part of the derived object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When is the base class constructor called?

A

Before the derived class constructor, even if not explicitly written (calls default constructor if available).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What happens if base has no default constructor and the derived constructor doesn’t call it?

A

❌ Compiler error — must explicitly call the base constructor via initializer list.

17
Q

Are friend functions or classes inherited?

A

❌ No. Friends of a base class are not friends of the derived class. You must declare them again.

18
Q

Can you make a protected member in the base class public in the derived class?

A

❌ No. You can restrict visibility (e.g., public → private), but cannot widen it.

19
Q

Can you access protected members of the base in the derived class?

A

✅ Yes, they can be accessed directly in the derived class.

20
Q

What happens when you create an object of a derived class that doesn’t explicitly call a base constructor?

A

The default constructor of the base class is called automatically before the derived class constructor, even if the derived class constructor is parameterized.

21
Q

If a derived class constructor is parameterized but doesn’t use an initializer list, which base constructor is invoked?

A

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.

22
Q

How can you explicitly call a base class’s parameterized constructor from a derived class

A

Using an initializer list:
Derived(int x, int y) : Base(x) {
cout &laquo_space;“Derived constructor with y = “ &laquo_space;y &laquo_space;endl;
}

23
Q

What will happen in this code?

class Base {
public:
Base(int x) { cout &laquo_space;“Base: “ &laquo_space;x &laquo_space;endl; }
};

class Derived: public Base {
public:
Derived(int a) {
cout &laquo_space;“Derived: “ &laquo_space;a &laquo_space;endl;
}
};

int main() {
Derived d(5);
}

A

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.

24
Q

How would you fix the above code?

A

Add an initializer list in Derived to call the base constructor:

Derived(int a) : Base(a) {
cout &laquo_space;“Derived: “ &laquo_space;a &laquo_space;endl;
}

25
What is the order of constructor calls when creating a derived object?
The base class constructor is called first. The derived class constructor is called. Always.
26
What is the order of destructor calls when deleting a derived object?
Derived class destructor is called first Then base class destructor (Reverse of construction)
27
What happens if both base and derived have default and parameterized constructors, and you create a derived object with no arguments?
The default constructor of the base is called first, then the default constructor of the derived.
28
What happens if you create a derived object with arguments, but don’t call a base constructor explicitly?
The base class's default constructor is used — if it doesn't exist, you get a compiler error.
29
Why can't you call Base(x); inside the body of the Derived constructor?
That just creates a temporary object — it doesn’t initialize the base part of the derived object. Base must be initialized in the initializer list.
30
Can initializer lists be used to initialize both base and member variables in one line?
Yes. Example: class Derived : public Base { int val; public: Derived(int x, int y) : Base(x), val(y) { } };