Complete the code to declare a private member variable in a class.
class MyClass { private: int [1]; };
The private member variable is declared with a name like data. Keywords like public, void, or class are not valid variable names.
Complete the code to provide a public method that accesses the private variable.
class MyClass { private: int data; public: int [1]() { return data; } };
The method to access a private variable is usually called a getter, commonly named getData. setData is for setting values, and data or private are not method names here.
Fix the error in the code to correctly set the private variable using a public method.
class MyClass { private: int data; public: void setData(int value) { [1] = value; } };
The private variable data should be assigned the value passed to the method. Using data = value; correctly sets the private member.
Fill both blanks to create a class with a private variable and a public getter method.
class Person { private: [1] age; public: [2] getAge() { return age; } };
void as the return type for a getter method.float when it should be int.The private variable age should be of type int. The getter method returns an int value, so its return type is also int.
Fill all three blanks to define a class with a private string variable, a setter, and a getter.
class Book { private: [1] title; public: void [2]([3] t) { title = t; } std::string getTitle() { return title; } };
int instead of std::string for the variable or parameter.The private variable title is of type std::string. The setter method is named setTitle and takes a parameter of type std::string.