Help woth assignment - Class person, with two constructors

I need help with this assignment. I need a lot more time to read the textbook and understand it better, but I have a deadline to submit it.
Here it is:
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #ifndef PERSONTYPE_H
#define PERSONTYPE_H

class personType
{
public:
personType();
personType(string, string);

void setName(string);
string getFName() const;
string getLName() const;
void printName() const;

private:
string first;
string last;
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  #include <iostream>
#include <string>
#include <iomanip>
#include "personType.h"

using namespace std;

personType::personType();
{
	first = "";
	last = "";
}
personType::personType( fname, lname)
{
setName( fname, lname);
}

void personType::setName(string first, string last)
{
first = fname;
last = last;	
}

srting personType::getFName () const
{
cout << "First name: "
	cin >> personType.first;
return first;
}

string personType::getLName() const
{
	cout << "Last name: "
	cin >> peronType.last;
return last;	
}

void personType::printName() const
{
cout << "The name of the person is: " <<first << " " << last << endl;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include <iostream>
#include <string>
#include <iomanip>
#include "personType.h"

int main()
{
	personType Person("My", "Name");
	
	cout << "The first and last name of the person are " << Person.first << " " << Person.last << endl;
	
	cout << "Please, enter the first and last name of another person" << endl;
	Person.getFName();
	Person.getLName();
	
	Person.printName();
	
	}



The errors I get are:

17:class string personType::first is private
within the context
18:class string personType::last is private
within the context
Last edited on
Hey, a few comments -

About the getFunctions. They're just supposed to return the name, nothing else.

1
2
3
4
5
6
string personType::getLName() const
{
	cout << "Last name: "
	cin >> peronType.last;
return last;	
}


I'm not sure what you're trying to do here. Say I create a person in main, I will use the setPerson function to set my name, then I can use this get function to get my name whenever I want. For example -

1
2
3
4
5
PersonType person;

person.setName("Tarik", "Neaj");

cout << "My Name is: " << person.getFName() << " " << person.getLName() << endl;


So your function should simply look like this -

1
2
3
4
string personType::getLName() const
{
	return last;	
}


(I would suggest you name your variable something more descriptive, like lastName and firstName)
Last edited on
My problem is that I don't understand how I can have 2 constructors and why.
I am also not sure where I should display the message
1
2
3
4
cout << "Enter a first name";
cin << Person.getFName;
cout << "Enter alast name";
cin << Person.getLName;


and I don't know if it is correct.
Constructor are used to initialize variables. You can have as many as you want, as long as they are different. The default constructor doesnt do anything, the assignment says a constructor with 2 paramters, they probably mean a constructor that takes a first and last name as paramter and initializes your class variables to them.

1
2
3
4
5
personType::personType(string firstName, string lastName)
{
    first = firstName;
    last = lastName;
}


Now, you can create a person with a first and lastname, without having to use the setFunctions.

personType person("Tarik", "Neaj");

Now you can do like earlier - cout << "My Name is: " << person.getFName() << " " << person.getLName() << endl;

don't forget, when you're calling a function, you need to use the brackets. Person.getFNameis wrong, it's Person.getFName()

setFunctions are useful because it allows you to change name whenever you want.
Last edited on
I need to have 2 constructors, one default and one with parameters.
I made some changes, but I still get errors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef PERSONTYPE_H
#define PERSONTYPE_H

class personType
{
public:

personType(string, string);

void setName(string);
string getFName() const;
string getLName() const;
void printName() const;

private:
string firstName;
string lastName;
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <string>
#include <iomanip>
#include "personType.h"

using namespace std;


personType::personType( fname, lname)
{
firstName = fname;
lastName = lname;
}

void personType::setName(string fname, string lname)
{
firstName = fname;
lastName = lname;	
}

srting personType::getFName () const
{
return firstName;
}

string personType::getLName() const
{
return lastName;	
}

void personType::printName() const
{
cout << "The name of the person is: " <<firstName << " " << lastName << endl;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <iomanip>
#include "personType.h"

int main()
{
	personType Person("My", "Name");
	
	
	
	cout << "Enter the first name:" << Person.getFName();
	cout << "Enter the last name:" << Person.getLName();
	
	Person.printName();



Errors:
8: undefined reference to personType::personType
13:undefined reference to personType::getFName
14:undefined reference to personType::getLName
16:undefined reference to personType::printName
personType::personType( fname, lname)
What is fname and lname, are they bananas or waterfalls? C++ doesnt know, you need to specify.

personType::personType( string fname, string name)

In the .h file. setName takes 1 paramter, in your definition of setName in the .cpp file it takes 2.

srting personType::getFName () const // should be string not srting


string is part of the standard library, in your .h file you need to either put using namespace std; or std::string everywhere. Also include string.

Last edited on
Thank you for your help. I changed it. Now it doesn't show any errors but it does not run as well. Nothing is happening. I also still don't know how to have two constructors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef PERSONTYPE_H
#define PERSONTYPE_H
using namespace std;
class personType
{
public:

personType(string, string);

void setName(string, string);
string getFName() const;
string getLName() const;
void printName() const;

private:
string firstName;
string lastName;
};

#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include "personType.h"

using namespace std;


personType::personType( string fname, string lname)
{
firstName = fname;
lastName = lname;
}

void personType::setName(string fname, string lname)
{
firstName = fname;
lastName = lname;	
}

string personType::getFName () const
{
return firstName;
}

string personType::getLName() const
{
return lastName;	
}

void personType::printName() const
{
cout << "The name of the person is: " <<firstName << " " << lastName << endl;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <iomanip>
#include "personType.h"

int main()
{
	personType Person("My", "Name");
		
	cout << "Enter the first name:" << Person.getFName();
	cout << "Enter the last name:" << Person.getLName();
	
	Person.printName();
	
	
}
Works for me, it's also printing exactly what it's supposed to.

1
2
cout << "Enter the first name:" << Person.getFName();
cout << "Enter the last name:" << Person.getLName();

these are "get" functions, you're not entering anything. This is pointless.

You're giving the person a name here - personType Person("My", "Name");

About the constructors, you just have 2 constructors like you would with any function.

1
2
3
4
5
6
7
class personType
{
public:

	personType(string, string); // user defined constructor
        personType(); // default constructor
	void setName(string, string);


1
2
3
4
5
6
7
8
9
10
personType::personType(string fname, string lname) // userdefined Constructor
{
	firstName = fname;
	lastName = lname;
}

personType::personType() // default constructor
{

}
I think my problem is that I don't understand if the assignment requires the user to enter a Name or, I have to just define it.
Also, I don't understand exactly the role of the get functions.
The print() prints the name, and the get(), gets the name from set(). Am I right?
The print() prints the name, and the get(), gets the name from set(). Am I right?
No, set() sets the nam, get() gets the name, thats it.

Say I use the constructor to create my first and last name. But later decide, my name sucks, I want to change. That is why we need the set(). So we can change name.

1
2
3
4
5
6
7
8
9
10
11
// Create person - 
personType myself("Tarik", "Neaj");

//You can now use the get function to print out your name.
cout << "My name is " << myself.getFName() << " " << myself.getLName() << endl; // outputs Tarik Neaj

// 2 years later, name sucks gotta change name.
myself.setName("Potato", "Tomato"); // new cool name

//Well, I forgot what my name is... maybe I should take a look?
cout << "My name is " << myself.getFName() << " " << myself.getLName() << endl; // Outputs Potato Tomato 
Last edited on
How do I use the print function. It is part of my assignment?
I changed it a little bit:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef PERSONTYPE_H
#define PERSONTYPE_H
using namespace std;
class personType
{
public:

personType(string, string);
personType();
void setName(string, string);
string getFName() const;
string getLName() const;
void printName() const;

private:
string firstName;
string lastName;
};

#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
#include "personType.h"
using namespace std;

personType::personType()
{
}
personType::personType( string fname, string lname)
{
firstName = fname;
lastName = lname;
}

void personType::setName(string fname, string lname)
{
firstName = fname;
lastName = lname;	
}

string personType::getFName () const
{
return firstName;
}

string personType::getLName() const
{
return lastName;	
}

void personType::printName() const
{
cout << "The name of the person is: " << getFName() << " " << getLName() << endl;
}

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
#include <iomanip>
#include "personType.h"

int main()
{
	personType Person("My","Name");
		
	Person.printName();
}


Now I get errors:
undefined reference to personType::personType
undefined reference to personType::printName
Topic archived. No new replies allowed.