98-361 · Question #10
You need to allow a consumer of a class to modify a private data member. What should you do?
The correct answer is C. Provide a public function that assigns a value to the data member. In this example (see below), the Employee class contains two private data members, name and salary. As private members, they cannot be accessed except by member methods. Public methods named GetName and Salary are added to allow controlled access to the private members. The…
Question
You need to allow a consumer of a class to modify a private data member. What should you do?
Options
- AAssign a value directly to the data member.
- BProvide a private function that assigns a value to the data member.
- CProvide a public function that assigns a value to the data member.
- DCreate global variables in the class.
How the community answered
(31 responses)- A10% (3)
- B3% (1)
- C74% (23)
- D13% (4)
Explanation
In this example (see below), the Employee class contains two private data members, name and salary. As private members, they cannot be accessed except by member methods. Public methods named GetName and Salary are added to allow controlled access to the private members. The name member is accessed by way of a public method, and the salary member is accessed by way of a public read-only property. Note: The private keyword is a member access modifier. Private access is the least permissive access level. Private members are accessible only within the body of the class or the struct in which they are declared private string name = "FirstName, LastName"; private double salary = 100.0; public string GetName() public double Salary get { return salary; }
Topics
Community Discussion
No community discussion yet for this question.