Create a person class to represent a person. (You may call the class personType.
ID: 3660966 • Letter: C
Question
Create a person class to represent a person. (You may call the class personType.) To simplify things, have the class have 2 variable members for the person's first and last name. Include 2 constructors. One should be a default constructor and the other should be one with parameters. Include respective functions for: setting the name, getting the name, and printing the name on the screen. Have your main program call these functions to demonstrate how they work. Explain how you can replace both constructors with one constructor by using a single constructor with default parameters.Explanation / Answer
/*100% working code*/
#include <iostream>
using namespace std;
class personType
{
string firstName;
string lastName;
public:
personType()
{
setFirstName("no firstname");
setLastName("no lastname");
}
personType(string fname, string lname)
{
setFirstName(fname);
setLastName(lname);
}
string getFirstName() {
return firstName;
}
void setFirstName(string fName) {
firstName = fName;
}
string getLastName() {
return lastName;
}
void setLastName(string lName) {
lastName = lName;
}
void print()
{
cout<<" Name is :"<<getFirstName()<<" "<<getLastName();
}
};
int main()
{
cout<<" ********Demonstration-1*******";
personType p1("John","Marvel");
p1.print();
cout<<" ********Demonstration-2*******";
personType p2;
p2.print();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.