everyone. This is my first time using classes in C++. The instructions in the assignment I'm working on seemed a little ambiguous to me, so I'm including them in case someone more experienced thinks I'm not following them properly in my code.
Anyway, I wrote my program, and it works and creates the output I want based on my perception of the assignment. However, when I run it, an error also pops up that says "Invalid Null Pointer." I've done some research but I can't seem to figure out why this is.
Can someone help me fix my code and help a student understand what they did wrong? Thanks! :)
Assignment
Write a Person class that contains the following fields and methods:
• First Name
• Last Name
• ID Number
• Necessary constructors
• Methods to retu last name, first name, full name, and ID number
• Methods to print last name, first name, and ID numberWrite a main program to test your class.
My Program
#include <iostream>
#include <string>
using namespace std;
class Person {
private:
string FirstName;
string LastName;
string FullName;
int IDNumber;
public:
void SetFirstName(string);
void SetLastName(string);
void SetIDNumber(int);
string SetFullName(string first, string last);
string GetFirstName();
string GetLastName();
string Method1();
string Method2();
int GetIDNumber();
};
void Person::SetFirstName(string first) {
FirstName = first;
}
void Person::SetLastName(string last) {
LastName = last;
}
void Person::SetIDNumber(int ID) {
IDNumber = ID;
}
string Person::SetFullName(string first, string last) {
FullName = string(first + " " + last);
retu FullName;
}
string Person::GetFirstName() {
retu FirstName;
}
string Person::GetLastName() {
retu LastName;
}
int Person::GetIDNumber() {
retu IDNumber;
}
string Person::Method1() {
cout << "LAST NAME: " << LastName << endl;
cout << "FIRST NAME: " << FirstName << endl;
cout << "FULL NAME: " << FullName << endl;
cout << "ID NUMBER: " << IDNumber << endl;
retu 0;
}
string Person::Method2() {
cout << "LAST NAME: " << LastName << endl;
cout << "FIRST NAME: " << FirstName << endl;;
cout << "ID NUMBER: " << IDNumber << endl;
retu 0;
}
int main() {
string firstname, lastname;
int id;
char command;
Person Person;
cout << "What is the subject's first name? For example: Bob Smith" << endl;
cin >> firstname >> lastname;
cout << "What is " << firstname << " " << lastname << "'s ID number?" << endl;
cin >> id;
Person.SetFirstName(firstname);
Person.SetLastName(lastname);
string fullname = Person.SetFullName(firstname, lastname);
Person.SetIDNumber(id);
cout << "COMMANDS:" << endl;
cout << "f : Retus first name." << endl << "l : Retus last name." << endl << "i : retus ID number." << endl << "n : retus full name." << endl;
cout << "1 : Retus last name, first name, full name, and ID number." << endl;
cout << "2 : Retus last name, first name, and ID number." << endl << endl;
cout << "Please input the letter of your command: ";
cin >> command;
switch (command) {
case 'f':
case 'F':
cout << "FIRST NAME: " << Person.GetFirstName() << endl;;
break;
case 'l':
case 'L':
cout << "LAST NAME: " << Person.GetLastName() << endl;
break;
case 'i':
case 'I':
cout << "ID NUMBER: " << Person.GetIDNumber() << endl;
break;
case 'n':
case 'N':
cout << "FULL NAME: " << fullname << endl;
break;
case '1':
Person.Method1();
cout << endl;
break;
case '2':
Person.Method2();
cout << endl;
break;
default:
cout << "That is not a valid command." << endl;
}
}
