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.
#include <iostream>
#include <string>
#include <iomanip>
#include "personType.h"
usingnamespace 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
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;
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.
#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
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.
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