C++ Inheritance Flashcards
(14 cards)
What is inheritance in C++?
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.
What is the general syntax for class inheritance in C++?
class derived-class-name : access-specifier base-class-name
{
// body of derived class
};
Example:
class MySpecialString: public Stringy
{
// derived class implementation
};
What happens to base class members when using public inheritance?
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
What are the key characteristics of private inheritance?
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 does protected inheritance modify base class member access?
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
What is the order of constructor execution in inheritance?
Constructors are executed in order of derivation:
Base class constructor is called first
Derived class constructor is called next
What is the order of destructor execution in inheritance?
Destructors are executed in reverse order of derivation:
Derived class destructor is called first
Base class destructor is called next
How can you pass parameters to a base class constructor?
Use the initialization list in the derived class constructor:
derived::derived(int x, int y) : base(y)
{
// Derived class constructor body
}
What is multiple inheritance in C++?
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
};
What is the “diamond problem” in multiple inheritance?
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.
What is a virtual base class and how does it solve the diamond problem?
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.
What is a virtual function in C++?
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();
};
What is a pure virtual function?
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
};
What are some key best practices for inheritance in C++?
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