Back to notes
What is the difference between a class constructor and a destructor in C++?
Press to flip
A constructor is a method called when an object is instantiated and is used for initialization: `MyClass() { /*...*/ }`. A destructor is called when an object is destroyed and is used for cleanup: `~MyClass() { /*...*/ }`.
How do you define a derived class `Car` that inherits from a base class `Vehicle` and adds an attribute `model`?
```cpp class Car : public Vehicle { std::string model; // Additional members and methods }; ```
How would you implement a getter and a setter for a private member `speed` in a class `Vehicle`?
```cpp class Vehicle { private: int speed; public: int getSpeed() { return speed; } void setSpeed(int spd) { speed = spd; } }; ```
Describe how arrays and pointers are related in C++.
Arrays are essentially pointers to their first element. Therefore, an array declaration like `int arr[5]` can be accessed using a pointer `int* ptr = arr;`.
What is encapsulation and how is it implemented in C++?
Encapsulation is restricting direct access to certain components of an object and is implemented using private and public access specifiers. Use `private` for sensitive data and implement getters and setters for accessing private members.
Explain how to use method overriding with an example involving a base class `Vehicle` and a derived class `Car`.
In the base class `Vehicle`, define a method as virtual: ```cpp class Vehicle { virtual void getClassType() { std::cout << "Vehicle"; } }; ``` In the derived class `Car`, override the base class method: ```cpp class Car : public Vehicle { void getClassType() override { std::cout << "Car"; } }; ```
How can pointers be used to print the memory address and the value stored in the address of an integer variable?
If `int* ptr = &var;`: To print value: `std::cout << *ptr;` To print address: `std::cout << ptr;`
How do you declare a pointer that stores the address of an integer variable `var`?
int* ptr = &var;
How does one create an instance of the `Car` structure and assign the number of wheels to 4?
Car myCar; myCar.wheels = 4;
How would you access the first element of an array `arr` using a pointer?
int* ptr = arr; int firstElement = *ptr;
What is a virtual method and how is it defined in C++?
A virtual method is a function that can be overridden in a derived class to provide specific behavior. It is defined using the `virtual` keyword: `virtual void myMethod();`
Describe method overloading and provide a basic example.
Method overloading is when multiple methods have the same name but different parameters. Example: ```cpp void func(); void func(int a); void func(int a, float b); ```
What is the purpose of the `virtual` keyword in a method declaration?
The `virtual` keyword allows a method to be overridden in a derived class, enabling polymorphism.
How would you declare a structure named `Car` with attributes `wheels`, `brand`, `color`, `maxSpeed`, and a method `printProperties`?
```cpp struct Car { int wheels; std::string brand; std::string color; int maxSpeed; void printProperties(); }; ```
What is the difference in syntax between passing a variable by value and passing a variable by reference to a function in C++?
Passing by value: `void func(int a);` Passing by reference: `void func(int& a);`
Previous
Next