0
0
C++programming~10 mins

Getter and setter methods in C++ - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aage
Bthis->age()
CgetAge
DAge
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using parentheses after the variable name like a function call.
Using incorrect capitalization.
2fill in blank
medium

Complete 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'
AnewAge
Bage
Cvalue
DageValue
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->.
3fill in blank
hard

Fix 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'
Aage
Bvalue
CsetAge
Dthis->age
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Assigning the parameter to itself.
Using the member variable name without this-> when parameter name is the same.
4fill in blank
hard

Fill 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'
AgetScore
BsetScore
Cscore
DupdateScore
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using the same name for both getter and setter.
Using incorrect method names that don't follow conventions.
5fill in blank
hard

Fill 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'
Alevel
BgetLevel
CsetLevel
Dscore
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using inconsistent variable names.
Not matching getter and setter names with the variable.