92.7k views
3 votes
Define the Person class's SetName() mutator that sets data member name to personName and the SetHeight() mutator that sets data member height to personHeight.

Ex: If the input is Ina 3.0, then the output is:

Person: Ina
Height: 3.0 feet
in C++ please!

code:

#include
#include
#include
using namespace std;

class Person {
public:
void SetName(string personName);
void SetHeight(double personHeight);
void Print() const;
private:
string name;
double height;
};

/* Your code goes here */

void Person::Print() const {
cout << "Person: " << name << endl;
cout << "Height: " << fixed << setprecision(1) << height << " feet" << endl;
}

int main() {
Person person;
string inputName;
double inputHeight;

cin >> inputName;
cin >> inputHeight;
person.SetName(inputName);
person.SetHeight(inputHeight);

person.Print();

return 0;
}

User Sheril
by
8.0k points

1 Answer

2 votes

Final answer:

The SetName() mutator sets the Person class's name member, while the SetHeight() mutator sets the height member. These functions allow the encapsulated fields to be modified by external code while maintaining object integrity.

Step-by-step explanation:

To define the SetName() and SetHeight() mutator functions for the Person class in C++, you need to implement these functions to set the private data members' name and height to the values provided by the parameters personName and personHeight, respectively.

The definition of the SetName() mutator function inside the Person class will look like this:

void Person::SetName(string personName) {
name = personName;
}

The definition of the SetHeight() mutator function inside the Person class will look like this:

void Person::SetHeight(double personHeight) {
height = personHeight;
}

These functions allow modification of the Person object's private data members from outside the class, adhering to the principles of encapsulation in object-oriented programming.

User GlS
by
8.5k points