Classes, Inheritance, and Object-Oriented Concepts in C++ Flashcards

(53 cards)

1
Q

What command is used to link a C++ object file?

A

g++ MyClass.o -o MyProgram

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

What is the significance of the C++ Standard version 2020?

A

Introduced concepts and ranges

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

What feature does setw provide in C++?

A

Sets the width of the next output field

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

What does the using namespace std; statement do?

A

Allows access to the standard namespace

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

What is the principle of modularity in C++?

A

Code organized into classes/files

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

Define abstraction in the context of C++.

A

Hide unnecessary details

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

What does encapsulation mean in C++?

A

Private data, accessed only via class

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

What is extensibility in C++?

A

Reuse and build on top of existing code

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

Define polymorphism in C++.

A

Same interface, different behavior

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

What is inheritance in C++?

A

Child class inherits from parent class

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

What is the syntax to define a class in C++?

A

class ClassName { /* members */ };

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

What does a constructor do in C++?

A

Called automatically when an object is created

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

What is the role of a destructor in C++?

A

Called automatically when an object goes out of scope

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

How do you declare a parameterized constructor?

A

ClassName(int i, int j)

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

Fill in the blank: A parameterized constructor creates objects with _______.

A

Initial values

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

What is the rule for default parameters in C++?

A

Once a default is used, all following parameters must have one too!

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

What does the scope resolution operator :: do?

A

Defines class methods outside the class

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

True or False: You should use malloc() and free() in C++.

A

False

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

What is the correct way to allocate memory for an integer in C++?

A

int* p = new int(42);

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

What is the difference between a reference and a pointer in C++?

A

Reference: Implicit dereference; Pointer: * required

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

What is the syntax for passing by reference?

A

void function(int& x)

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

Fill in the blank: The this pointer is used to refer to the _______.

A

Current object inside its method

23
Q

What is a copy constructor in C++?

A

Creates a new object from another existing one

24
Q

Why is a custom copy constructor necessary?

A

Default copy = shallow copy (bad for pointers!)

25
What is a static member in a C++ class?
Value is shared across all instances
26
How do you access a static member in C++?
ClassName::memberName
27
What are the two main types of constructors in C++?
* Default constructor * Parameterized constructor
28
What happens when you use new in C++?
Calls constructor
29
What happens when you use delete in C++?
Calls destructor
30
How do you define a member function outside the class?
ReturnType ClassName::functionName() { /* body */ }
31
Fill in the blank: In C++, the this pointer allows method _______.
Chaining
32
What is Inheritance?
Inheritance lets a class acquire properties (variables & methods) from another class.
33
What is the analogy used to explain Inheritance?
* Product → base class (general) * PackagedProduct → derived class (adds packaging) * LabelledProduct → derived class (adds barcode, label)
34
What is the syntax for defining a derived class in C++?
class Derived : access Base { // new or extended features };
35
Provide an example of a derived class in C++.
class MySpecialString : public Stringy { char type; };
36
How are base class members inherited with respect to access specifiers?
* public: accessible * protected: accessible * private: not accessible
37
What should you use for most inheritance scenarios?
Use public inheritance unless intentionally hiding things.
38
What are the access levels for members in inheritance?
* public: accessible * protected: accessible * private: not accessible
39
What is the order of execution for constructors and destructors?
Constructors run top-down (base → derived), Destructors run bottom-up (derived → base).
40
What is the output order of constructors and destructors in the provided example?
Base, Derived, Bye Derived, Bye Base
41
How do you pass parameters to a base class constructor?
Use the initializer list in the derived class constructor.
42
What is chain inheritance?
class base { ... }; class derived1 : public base { ... }; class derived2 : public derived1 { ... };
43
What is the order of construction and destruction in chain inheritance?
* Construct: base → derived1 → derived2 * Destruct: derived2 → derived1 → base
44
What is multiple inheritance?
class derived : public base1, public base2 { ... };
45
What issue may arise from multiple inheritance?
Ambiguity if both base classes have similar members.
46
What is the Diamond Problem in inheritance?
Derived class gets two copies of the base class member.
47
How can you resolve the Diamond Problem?
* Use scope resolution * Use virtual base classes
48
What are virtual base classes?
Ensure only one copy of the base is inherited.
49
What is the purpose of virtual member functions?
Enables polymorphism.
50
What is the result of dynamic dispatch using virtual functions?
Function is bound at runtime, not compile time.
51
What is an abstract class?
A class that has at least one pure virtual method.
52
What happens if you try to instantiate an abstract class?
It results in a compilation error.
53
Summarize the key ideas of inheritance.
* Inheritance: Let one class use another's members * Access Levels: public / protected / private * Constructor Order: Base → Derived * Multiple Inheritance: Can cause conflicts – use virtual * Virtual Function: Enables dynamic dispatch * Abstract Class: Has at least one pure virtual method * Diamond Problem: Fixed using virtual base classes