C++ Classes Flashcards

(10 cards)

1
Q

What are the key principles of object-oriented programming in C++?

A

Modularity: Group data and operations for an object together

Abstraction: Hide complex implementation details

Encapsulation: Hide object implementation from users

Extensibility: Reuse and extend functionality

Polymorphism: Use same code for different object types

Inheritance: Allow code reuse and extensibility

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

What are constructors and destructors in C++?

A

Constructors:
- Special member functions called when an object is created
- Have the same name as the class
- No return type
- Can be overloaded
- Used to initialize object’s state

Destructors:
- Special member function called when an object is destroyed
- Prefixed with ~ (tilde)
- No return type
- Used to clean up resources (free memory, release locks)
- Only one destructor per class

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

How do parameterized constructors work in C++?

A

Allow creating objects with initial values

Can be overloaded with different parameter lists

Support default parameter values

Example:
class NewClass {
public:
NewClass(int i, int j = 0); // Constructor with default value
NewClass(int i); // Overloaded constructor
};

Enables flexible object initialization

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

What is the scope resolution operator (::) in C++?

A

Used to define member functions outside the class declaration

Specifies the scope of:
- Member functions
- Classes
- Namespaces

Allows separating class declaration (in header) from function definition (in source file)

Example:
void MyClass::myFunction() {
// Function definition
}

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

How does C++ memory management differ from C?

A

C Way:
int *p = (int )malloc(sizeof(int)25);
free(p);

C++ Way:
int *p = new int; // Single uninitialised integer
int *p = new int(43); // Single initialised integer
int *q = new int[25]; // Array of 25 integers

delete p; // Free single value
delete[] q; // Free array

Key Differences:
- new calls constructor
- delete calls destructor
- Type-safe memory allocation
- Automatic constructor/destructor invocation

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

What are references in C++?

A

An alias for an existing variable

Implicitly dereferenced

Cannot be null

Cannot be reassigned to reference another variable

Syntax: int &y = x;

Compared to Pointers:
References don’t need * to access value
Pointers can be reassigned
References must be initialized when declared

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

How does pass by reference work in C++?

A

Pass by Value:
void addTen(int x) {
x += 10; // No effect on original variable
}

Pass by Reference:
void addTen(int &x) {
x += 10; // Modifies original variable
}

Key Differences:
- Pass by reference allows function to modify original variable
- Avoids copying large objects
- More efficient for complex types

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

What are static member variables in C++?

A

Shared by all objects of the same class

Can be accessed without creating an object

Declared in class definition

Initialized outside the class

Example:
class MyClass {
public:
static int counter;
};

int MyClass::counter = 0; // Initialization

Useful for tracking class-wide information

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

What is a copy constructor in C++?

A

Initializes an object using another object of the same class

Declaration: MyClass(const MyClass &)

Used when:
- Creating a new object as a copy of existing object
- Passing object by value
- Returning object by value

Default Copy Constructor:
- Performs member-wise copy
- Can be problematic with dynamically allocated memory
- Custom implementation often needed for deep copying

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

What is the this pointer in C++?

A

Pointer to the current object instance

Used to:
- Resolve naming conflicts between member variables and parameters
- Return a reference to the current object
Pass the current object to a function

Example:
class MyClass {
public:
MyClass(int a, int b) {
this->a = a; // Disambiguate member variable
this->b = b;
}
};

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