Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a getter method for the private member age.
C++
class Person { private: int age; public: int getAge() const { return [1]; } };
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using parentheses after the variable name like a function call.
Using incorrect capitalization.
β Incorrect
The getter method should return the private member variable
age directly.2fill in blank
mediumComplete the code to declare a setter method for the private member age.
C++
class Person { private: int age; public: void setAge(int [1]) { age = value; } };
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using a parameter name different from the one used in the assignment.
Using the same name as the member variable without
this->.β Incorrect
The setter method parameter should be named
value to match the assignment inside the method.3fill in blank
hardFix the error in the setter method to correctly assign the parameter to the member variable.
C++
class Person { private: int age; public: void setAge(int value) { [1] = value; } };
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Assigning the parameter to itself.
Using the member variable name without
this-> when parameter name is the same.β Incorrect
To avoid ambiguity, use
this->age to refer to the member variable inside the setter.4fill in blank
hardFill both blanks to create a getter and setter for the private member score.
C++
class Game { private: int score; public: int [1]() const { return score; } void [2](int value) { score = value; } };
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using the same name for both getter and setter.
Using incorrect method names that don't follow conventions.
β Incorrect
The getter is conventionally named
getScore and the setter setScore.5fill in blank
hardFill all three blanks to create a class with private member level and its getter and setter methods.
C++
class Player { private: int [1]; public: int [2]() const { return [1]; } void [3](int value) { [1] = value; } };
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using inconsistent variable names.
Not matching getter and setter names with the variable.
β Incorrect
The private member is
level, the getter is getLevel, and the setter is setLevel.