i need help to 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.
this is what i have
#include <string>
#include <iostream>
using namespace std;
void printName()
{
cout << name << " " << lastname;
cout << name << " " << name;
}
private:
string name;
string lastname;
};
int main()
{
PersonType newPerson();
cout << "What would you like his/her first name to be? " << endl;
string n;
cin >> newPerson.setName();
cout << "What would you like his lastname to be? " << endl;
cin >> lastname.setName;
cout << "Your first name is " << name.getName << endl;
cout << "Your last name is " << lastname.getLastname << endl
The line PersonType newPerson(); should be PersonType newPerson;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//declare a string first
//declare string for last
//getline of string first
//newPerson.setName(first)
cout << "What would you like his lastname to be? " << endl;
//getline of string last
//newPerson.setLastname(last)
cout << "Your first name is " << newPerson.getName() << endl;
cout << "Your last name is " << newPerson.getLastname() << endl
//objects (newPerson) contain the variables
//they require function calls to be accessed
//for example, name.getName is missing the () so it isn't a function call
//also name is a private member variable and has to be accessed from newPerson
Yes, currently you have PersonType(string name, string lastname). This a constructor for the class, but not a default. You would need to use default parameters or a member initialization list.
1 2 3 4
//Here is an example of both
class A{
A(int x = 1): x(x) {}
}
so i rewrote it to this with help only issue i have now is in public getName() is not working not allowing for my constructor to work
#include <string>
#include <iostream>
using namespace std;
class personType
{
string firstName;
string lastName;
public:personType();
personType(string, string);
void setName();
void printName();
getName();
};
personType::personType()//Default constructor
{
firstName = "Joe"; lastName = "Smith";
cout << "The default constructor defaults to this data -";
}
personType::personType(string first, string last)
{
firstName = first;
lastName = last;
cout << "Custom constructor called! This is the input data: " << firstName << " " << lastName << "\n";
} void personType::setName()
{
cout << "Enter First Name: ";
cin >> firstName;
cout << "Enter Last Name: ";
cin >> lastName;
}
string personType::getName()
{
return firstName + " " + lastName;
}
void personType::printName()
{
cout << "\n" << "The name in the record is: " << firstName << " " << lastName << "\n\n";
} int main()
{
string first;
string last;
personType rec1;
cout << rec1.getName() << " -Though the data can be overwritten.\n";
cout << "Program will now use a custom constructor and request custom data to be entered. \n"
<< "Enter First Name: ";
cin >> first;
cout << "Enter Last Name: ";
cin >> last; personType rec2(first, last);
rec2.getName();
rec2.printName();
return 0;
system("pause");
}