Answer to Question #225524 in C++ for Matete

Question #225524
Create a class called Person and save it in person.cpp 2. This class must have the following data members: A name > An age And gender, for example M for Male and F for Female 3. This class must have the following methods: • A public default constructor that sets the data members to appropriate default values A copy constructor A setPerson method that will receive and assign person data. Ensure that the name is not an empty string, that the age is not negative and gender is only M/m/F/f. getName method that returns a name with the 1st letter uppercased. A getGender method that returns "Male" or "Female" depending on the person's gender • A getAge method that returns a person's age
1
Expert's answer
2021-08-12T16:24:26-0400
#include <iostream>
#include <string>


using namespace std;

class Person{
private:
	string name;
	int age;
	string gender;
public:
	// A public default constructor that sets the data members to appropriate default values 
	Person(){
		this->name="";
		this->age=1;
		this->gender="";
	}
	//A copy constructor 
	Person(Person &person){
		this->name=person.name;
		this->age=person.age;
		this->gender=person.gender;
	}
	//A setPerson method that will receive and assign person data.
	void setName(string name){
		this->name=name;
	}
	void setGender(string gender){
		this->gender=gender;
	}
	void setAge(int age){
		this->age=age;
	}
	//getName method that returns a name with the 1st letter uppercased. 
	string getName(){
		return this->name;
	}
	//A getGender method that returns "Male" or "Female" depending on the person's gender 
	string getGender(){
		if(this->gender=="F" ||this->gender=="f"){
			return "Female";
		}
		return "Male";
	}
	
	//A getAge method that returns a person's age
	int getAge(){
		return this->age;
	}


};

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS