A getter method is a function that returns the value of a private class member variable. It allows controlled read access to the variable.
Click to reveal answer
beginner
What is a setter method in C++?
A setter method is a function that sets or updates the value of a private class member variable. It allows controlled write access to the variable.
Click to reveal answer
intermediate
Why do we use getter and setter methods instead of making variables public?
Getter and setter methods protect the data by controlling how variables are accessed or changed. This helps keep data safe and allows validation before changing values.
Click to reveal answer
beginner
Show a simple example of a getter and setter for a private int variable named 'age'.
class Person {
private:
int age;
public:
int getAge() const { return age; }
void setAge(int a) { if(a >= 0) age = a; }
};
Click to reveal answer
intermediate
What happens if you try to set a negative age using the setter in the example?
The setter checks if the age is negative. If it is, it does not change the value, so the age stays valid (non-negative).
Click to reveal answer
What is the main purpose of a getter method?
ATo read the value of a private variable
BTo write a value to a private variable
CTo delete a variable
DTo create a new variable
✗ Incorrect
A getter method allows reading the value of a private variable safely.
Which keyword usually describes variables accessed by getters and setters?
Astatic
Bpublic
Cprivate
Dconst
✗ Incorrect
Variables are usually private to protect them and accessed via getters and setters.
What can a setter method do before changing a variable?
AValidate the new value
BPrint the variable
CDelete the variable
DConvert the variable to string
✗ Incorrect
Setters often check if the new value is valid before updating the variable.
Which of these is a correct setter signature for an int variable 'score'?
Avoid getScore();
Bvoid setScore(int s);
Cint getScore(int s);
Dint setScore();
✗ Incorrect
Setters usually return void and take the new value as a parameter.
If a class has a private variable, how can code outside the class access it safely?
ADirectly accessing the variable
BBy copying the class
CUsing global variables
DUsing getter and setter methods
✗ Incorrect
Getters and setters provide controlled access to private variables.
Explain what getter and setter methods are and why they are useful in C++ classes.
Think about how to safely read and change private variables.
You got /4 concepts.
Write a simple C++ class with a private string variable and add getter and setter methods for it.
Remember getters return the variable, setters take a parameter to update it.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of getter and setter methods in C++ classes?
easy
A. To control access to private variables safely
B. To make variables public automatically
C. To delete variables from the class
D. To print variable values directly
Solution
Step 1: Understand encapsulation in classes
Getter and setter methods are used to access and modify private variables safely without exposing them directly.
Step 2: Identify the role of getters and setters
Getters return the value, setters allow controlled modification, often with validation.
Final Answer:
To control access to private variables safely -> Option A
Quick Check:
Getter and setter = control access [OK]
Hint: Getters and setters protect private data access [OK]
Common Mistakes:
Thinking getters/setters make variables public
Confusing setters with printing functions
Assuming getters modify data
2. Which of the following is the correct syntax for a setter method in C++ to set a private integer variable age?
easy
A. int setAge() { return age; }
B. void setAge(int a) { age = a; }
C. void getAge(int a) { age = a; }
D. int age(int a) { age = a; }
Solution
Step 1: Identify setter method signature
A setter usually returns void and takes a parameter to set the private variable.
Step 2: Match correct syntax
void setAge(int a) { age = a; } matches: void return type, parameter int a, and sets age = a.
Final Answer:
void setAge(int a) { age = a; } -> Option B
Quick Check:
Setter = void + parameter + assign [OK]
Hint: Setter methods return void and take a parameter [OK]
Common Mistakes:
Using return type int for setter
Naming setter as getAge
Setter returning a value
3. What will be the output of the following code?
#include <iostream>
class Person {
private:
int age;
public:
void setAge(int a) { if(a >= 0) age = a; else age = 0; }
int getAge() { return age; }
};
int main() {
Person p;
p.setAge(-5);
std::cout << p.getAge();
return 0;
}
medium
A. Garbage value
B. -5
C. 0
D. Compilation error
Solution
Step 1: Analyze setter behavior with negative input
The setter checks if the input is >= 0; if not, it sets age to 0.
Step 2: Check what happens with -5
Since -5 < 0, age is set to 0.
Final Answer:
0 -> Option C
Quick Check:
Negative input sets age to 0 [OK]
Hint: Setter validates input before setting value [OK]
Common Mistakes:
Assuming negative value is stored
Expecting garbage value from uninitialized variable
Thinking code won't compile
4. Identify the error in the following setter method and choose the correct fix:
class Box {
private:
int length;
public:
void setLength() { length = l; }
};
medium
A. Remove the method entirely
B. Change return type to int
C. Make length public instead
D. Add parameter int l to setLength method
Solution
Step 1: Check setter method parameters
The setter tries to assign 'l' but has no parameter 'l' defined.
Step 2: Fix by adding parameter
Adding 'int l' as a parameter allows passing the value to set length.
Final Answer:
Add parameter int l to setLength method -> Option D
Quick Check:
Setter needs parameter to assign value [OK]
Hint: Setter must have parameter to set value [OK]
Common Mistakes:
Forgetting to add parameter
Changing return type unnecessarily
Making variable public instead of fixing setter
5. You want to create a class Temperature that stores temperature in Celsius but only allows values between -273 and 1000. Which setter method correctly enforces this rule?
hard
A. void setTemp(int t) { if(t >= -273 && t <= 1000) temp = t; else temp = -273; }
B. void setTemp(int t) { temp = t; }
C. int setTemp(int t) { if(t >= -273) temp = t; return temp; }
D. void setTemp() { temp = 0; }
Solution
Step 1: Understand the value range requirement
Temperature must be between -273 and 1000 inclusive.
Step 2: Check which setter enforces this range
void setTemp(int t) { if(t >= -273 && t <= 1000) temp = t; else temp = -273; } checks the range and sets temp accordingly; others do not enforce limits properly.