C++ Classes Flashcards
(10 cards)
What are the key principles of object-oriented programming in C++?
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
What are constructors and destructors in C++?
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 do parameterized constructors work in C++?
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
What is the scope resolution operator (::) in C++?
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 does C++ memory management differ from C?
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
What are references in C++?
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 does pass by reference work in C++?
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
What are static member variables in C++?
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
What is a copy constructor in C++?
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
What is the this pointer in C++?
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;
}
};