Lecture 9 - C++ Classes Flashcards

1
Q

What are compound data types in C++?

A

Compound data types group multiple variables under a single identifier. In C++, they are implemented via struct or class, and members can be of different types, accessed by name.

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

What is a struct in C++?

A

A struct in C++ is similar to a struct in C but can also contain member functions. It is a way to group related variables together under a single name, allowing easier data organization.

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

What is the key difference between struct and class in C++?

A

The primary difference is the default access modifier. In a struct, members are public by default, whereas, in a class, members are private by default.

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

What are visibility modifiers in C++?

A

Visibility modifiers control access to class or struct members. They include public, private, and protected, determining whether class members are accessible outside the class.

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

How do constructors work in C++ classes?

A

Constructors are special member functions that initialize objects of a class. They have the same name as the class and do not have a return type. They can be overloaded to provide different ways to initialize objects.

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

What is encapsulation in C++?

A

Encapsulation refers to bundling the data (variables) and methods (functions) operating on that data within a class, and restricting direct access to some of the object’s components using visibility modifiers.

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

What is procedural abstraction in C++?

A

Procedural abstraction in C++ refers to breaking down complex operations into simpler functions or methods. It hides implementation details and exposes only essential functionalities.

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

What are accessor and mutator functions?

A

Accessor functions (getters) retrieve the value of private data members, while mutator functions (setters) modify the value of private data members, providing controlled access to the class’s private data.

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

What is a default constructor in C++?

A

A default constructor is a constructor that takes no parameters. If no constructor is defined for a class, the compiler provides a default constructor.

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

What is constructor overloading in C++?

A

Constructor overloading allows a class to have multiple constructors with different signatures. This provides flexibility in object initialization.

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

What is the purpose of a copy constructor in C++?

A

A copy constructor initializes a new object as a copy of an existing object. It is automatically called when an object is passed by value, returned from a function, or explicitly copied.

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

What is the difference between deep copy and shallow copy?

A

A shallow copy copies an object’s pointer but not the data it points to, leading to shared memory. A deep copy duplicates both the object and the data it points to, creating independent copies.

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

What is the role of the this pointer in C++?

A

The this pointer is an implicit parameter to all non-static member functions. It points to the object that invoked the function and allows access to its members within the class.

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

What are destructors in C++?

A

Destructors are special member functions that clean up when an object is destroyed. They have the same name as the class but are preceded by a tilde (~) and have no return type.

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

What is function overloading in C++?

A

Function overloading allows multiple functions to have the same name but different parameter types or numbers. The appropriate function is selected based on the argument types passed during the call.

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

How do you define a class in C++?

A

A class is defined using the class keyword followed by the class name and its members enclosed in braces. Example: class Fraction { public: int numerator; int denominator; }.

17
Q

What is inheritance in C++?

A

Inheritance allows a new class (derived class) to inherit the properties and behavior (members and methods) of an existing class (base class), promoting code reuse and polymorphism.

18
Q

What is a virtual function in C++?

A

A virtual function is a member function that can be overridden in a derived class. It supports runtime polymorphism, where the derived class’s version of the function is called through a base class pointer or reference.

19
Q

What is polymorphism in C++?

A

Polymorphism allows one interface to be used for a general class of actions. The specific action depends on the type of object that invokes the method, achieved through virtual functions and inheritance.

20
Q

How does public, protected, and private inheritance differ?

A

In public inheritance, public members of the base class remain public in the derived class. In protected inheritance, public members of the base class become protected in the derived class. In private inheritance, all base class members become private in the derived class.