C++ Inheritance Flashcards

(14 cards)

1
Q

What is inheritance in C++?

A

A mechanism that allows a new class (derived class) to inherit properties and methods from an existing class (base class), enabling code reuse and establishing a hierarchical relationship between classes.

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

What is the general syntax for class inheritance in C++?

A

class derived-class-name : access-specifier base-class-name
{
// body of derived class
};

Example:
class MySpecialString: public Stringy
{
// derived class implementation
};

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

What happens to base class members when using public inheritance?

A

Public members of the base class remain public in the derived class

Protected members of the base class remain protected in the derived class

Private members of the base class are not directly accessible in the derived class

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

What are the key characteristics of private inheritance?

A

Public and protected members of the base class become private in the derived class

Base class members are not accessible outside the derived class

Typically used to implement composition or to hide base class interface

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

How does protected inheritance modify base class member access?

A

Public members of the base class become protected in the derived class

Protected members remain protected

Allows derived classes to access the inherited members, but prevents external access

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

What is the order of constructor execution in inheritance?

A

Constructors are executed in order of derivation:
Base class constructor is called first
Derived class constructor is called next

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

What is the order of destructor execution in inheritance?

A

Destructors are executed in reverse order of derivation:
Derived class destructor is called first
Base class destructor is called next

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

How can you pass parameters to a base class constructor?

A

Use the initialization list in the derived class constructor:
derived::derived(int x, int y) : base(y)
{
// Derived class constructor body
}

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

What is multiple inheritance in C++?

A

A feature that allows a derived class to inherit from more than one base class simultaneously:
class derived: public base1, public base2
{
// Multiple base classes
};

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

What is the “diamond problem” in multiple inheritance?

A

An ambiguity that occurs when a class inherits from two classes that have a common base class, potentially creating multiple copies of the base class members.

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

What is a virtual base class and how does it solve the diamond problem?

A

A virtual base class ensures only one instance of the base class exists in the inheritance hierarchy:

class derived1: virtual public base
class derived2: virtual public base
class derived3: public derived1, public derived2

This prevents multiple copies of the base class members.

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

What is a virtual function in C++?

A

A member function declared in a base class and redefined in derived classes, allowing runtime polymorphic behavior. Declared using the virtual keyword:

class Base {
public:
virtual void someFunction();
};

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

What is a pure virtual function?

A

A virtual function set to 0 in the base class, making the base class an abstract class:

class Base {
public:
virtual void someFunction() = 0; // Pure virtual function
};

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

What are some key best practices for inheritance in C++?

A

Prefer public inheritance unless there’s a specific reason

Use protected attributes sparingly

Prefer private members to insulate the class from changes

Be cautious with multiple inheritance

Use virtual base classes when dealing with diamond inheritance

Understand the implications of virtual functions for polymorphism

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