(Ch. 15) Understanding Inheritance in C++

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/36

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

37 Terms

1
New cards

Inheritance in C++

Inheritance allows a new class to be based on an existing class, inheriting its member variables and functions.

2
New cards

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.

3
New cards

Is a Relationship

It means the derived class is a specialized type of the base class (e.g., a truck is a vehicle).

4
New cards

Inherited Class Members

All member variables and functions, except constructors and destructors.

5
New cards

Constructors Inheritance

No, constructors and destructors are not inherited.

6
New cards

Protected Access Modifier

It allows derived classes to access base class members like private but more permissively.

7
New cards

GradedActivity Class Purpose

It demonstrates a base class holding a numeric score and calculating a letter grade.

8
New cards

FinalExam Inheritance

Using public inheritance: class FinalExam : public GradedActivity.

9
New cards

set() Function in FinalExam

Calculates the score from missed questions and sets it using setScore().

10
New cards

Base Class Access Specification

It determines how inherited members are treated (e.g., public stays public if inherited publicly).

11
New cards

Private Base Class Members

They are inherited but not directly accessible.

12
New cards

Protected Member Purpose

It allows access from derived classes but not from outside the class.

13
New cards

Constructor Chaining

A derived class constructor calls a base class constructor using an initialization list.

14
New cards

Destructor Execution Order

Derived class destructor executes first, followed by the base class destructor.

15
New cards

Redefining Base Class Function

By implementing a function with the same name in the derived class.

16
New cards

Calling Base Class Function

Using scope resolution: BaseClass::functionName().

17
New cards

Overloading vs Redefining

Overloading changes the parameter list; redefining changes the behavior in a derived class.

18
New cards

Abstract Base Classes

Classes with at least one pure virtual function, used to define interfaces.

19
New cards

Pure Virtual Function

A virtual function declared with = 0, requiring derived classes to implement it.

20
New cards

Polymorphism in C++

The ability to use derived class objects through base class pointers or references.

21
New cards

Output Order of Constructors and Destructors

Base constructor → Derived constructor → Derived destructor → Base destructor.

22
New cards

Benefit of Base Class

It avoids duplication and allows easier expansion or modification.

23
New cards

Function Overriding

When a derived class provides a new version of a base class function.

24
New cards

Virtual Member Function

It allows the function to be overridden in a derived class and used polymorphically.

25
New cards

Instantiating Abstract Base Class

No, it can only be used as a base for other classes.

26
New cards

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.

27
New cards

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.

28
New cards

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.

29
New cards

Q: How can you access a hidden base class function in a derived class?

A: Use the scope resolution operator like Base::functionName().

30
New cards

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.

31
New cards

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.

32
New cards

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.

33
New cards

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.

34
New cards

Q: How do you prevent a derived class from overriding a virtual function?

A: Use the final keyword: virtual void display() final;

35
New cards

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.

36
New cards

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.

37
New cards

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.