Problems with a problem

I have been asked 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.

Here is what I have so far:


#ifndef Person_H
#define Person_H
#include <string>
#include <iostream>
using namespace std;

struct PersonType
{
public:

PersonType(string name, string lastname)
{
setName(name);
setLastname(lastname);
}
void setName(string name)
{
name = name;
}
void setLastname(string lastname)
{
lastname = lastname;
}
string getName()
{
return name;
}

string getLastname()
{
return lastname;
}

void printName()
{
cout << name << " " << lastname;
}

private:

string name;
string lastname;
};
#endif

#include <iostream>
#include <string>
#include <cstdlib>

int main()
{

cout << endl << "Let's create a person!!! " << endl;
cout << "What would you like his/her first name to be? " << endl;
cin >> name.setName;
cout << "What would you like his/her lastname to be? " << endl;
cin >> lastname.setName;
cout << "Your person's first name is " << name.getName << endl
cout << "Your person's last name is " << lastname.getLastname << endl


cin.get();
return 0;
}


It seems that all the functions in the class PersonType are not recognized in the main. I keeps telling me that getname and setname are not declared inthis scope. Does anybody have any suggestions?
First of all, welcome to the forums:)

From the assignment description, it says you are supposed to create a class, but you are created a struct here. They're similar but not the same. Secondly, there are some OOP-related problems here. In order to use your class like you intend to you need to create an instance of it in main().

PersonType newPerson

Then, in order to set the variables, you need to do something along the lines of:

1
2
3
string n;
cin >> n
newPerson.setName(n);



Also, you should use code-tags when posting code, it makes it much easier to read:)
Last edited on
Topic archived. No new replies allowed.