1/36
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Inheritance in C++
Inheritance allows a new class to be based on an existing class, inheriting its member variables and functions.
Base and Derived Classes
A base class is the general class, and a derived class is a specialized version that inherits from the base class.
Is a Relationship
It means the derived class is a specialized type of the base class (e.g., a truck is a vehicle).
Inherited Class Members
All member variables and functions, except constructors and destructors.
Constructors Inheritance
No, constructors and destructors are not inherited.
Protected Access Modifier
It allows derived classes to access base class members like private but more permissively.
GradedActivity Class Purpose
It demonstrates a base class holding a numeric score and calculating a letter grade.
FinalExam Inheritance
Using public inheritance: class FinalExam : public GradedActivity.
set() Function in FinalExam
Calculates the score from missed questions and sets it using setScore().
Base Class Access Specification
It determines how inherited members are treated (e.g., public stays public if inherited publicly).
Private Base Class Members
They are inherited but not directly accessible.
Protected Member Purpose
It allows access from derived classes but not from outside the class.
Constructor Chaining
A derived class constructor calls a base class constructor using an initialization list.
Destructor Execution Order
Derived class destructor executes first, followed by the base class destructor.
Redefining Base Class Function
By implementing a function with the same name in the derived class.
Calling Base Class Function
Using scope resolution: BaseClass::functionName().
Overloading vs Redefining
Overloading changes the parameter list; redefining changes the behavior in a derived class.
Abstract Base Classes
Classes with at least one pure virtual function, used to define interfaces.
Pure Virtual Function
A virtual function declared with = 0, requiring derived classes to implement it.
Polymorphism in C++
The ability to use derived class objects through base class pointers or references.
Output Order of Constructors and Destructors
Base constructor → Derived constructor → Derived destructor → Base destructor.
Benefit of Base Class
It avoids duplication and allows easier expansion or modification.
Function Overriding
When a derived class provides a new version of a base class function.
Virtual Member Function
It allows the function to be overridden in a derived class and used polymorphically.
Instantiating Abstract Base Class
No, it can only be used as a base for other classes.
Q: Why must a virtual function be declared in the base class and not just in the derived class?
A: Because declaring it in the base class enables dynamic binding. Without it, calls through base class pointers will bind statically and not invoke the derived version.
Q: What will the following code print and why? class Base { public: virtual void greet() { cout << "Hello from Base"; } }; class Derived : public Base { public: void greet() override { cout << "Hello from Derived"; } }; int main() { Base* ptr = new Derived; ptr->greet(); }
A: It prints "Hello from Derived" due to dynamic binding of the virtual function.
Q: When does redefining a base class function not result in polymorphism?
A: When the base class function is not marked virtual, redefinition hides the function instead of overriding it.
Q: How can you access a hidden base class function in a derived class?
A: Use the scope resolution operator like Base::functionName().
Q: What is the impact of declaring a destructor virtual in a base class?
A: It ensures derived destructors execute properly during deletion through a base class pointer, preventing memory leaks.
Q: Given a class with a pure virtual function, what happens if a derived class does not override it?
A: The derived class also becomes abstract and cannot be instantiated.
Q: What's the difference between overloading and overriding a function in C++?
A: Overloading uses the same function name with different parameter lists; overriding uses the same signature in a derived class to replace base class behavior.
Q: What does = 0 mean in this declaration? virtual void speak() = 0;
A: It indicates that speak is a pure virtual function and must be implemented by derived classes.
Q: How do you prevent a derived class from overriding a virtual function?
A: Use the final keyword: virtual void display() final;
Q: What happens in this code and why? class Base { public: virtual void func(int x) { cout << "Base"; } }; class Derived : public Base { public: void func(long x) { cout << "Derived"; } }; int main() { Derived d; d.func(5); }
A: It prints "Derived" because func(5) promotes to long, and base func(int) is hidden. Use 'using Base::func;' to fix.
Q: What concept is demonstrated in this line? Base *ptr = new Derived(); followed by ptr->method();
A: Polymorphism — using a base class pointer to invoke an overridden function in a derived class.
Q: What is multiple inheritance and what problem can it introduce?
A: It allows a class to inherit from more than one base class, which can lead to ambiguity if both base classes contain members with the same name.