My C++ teacher gave me some part of the code because many of us were stuck.
Here was the assigned project.
Example 10- 10 defined a class personType to store the name of a person. The member functions that we included merely print the name and set the name of a person. Redefine the class personType so that, in addition to what the existing class does, you can:
a. Set the first name only.
b. Set the last name only.
c. Store and set the middle name.
d. Check whether a given first name is the same as the first name of this person.
e. Check whether a given last name is the same as the last name of this person. Write the definitions of the member functions to implement the operations for this class. Also, write a program to test various operations on this class.
I think I have a b and c down.
D and E are my main problems.
If you could maybe show me some better ways to code.
OR if you could show me how to fix my project so I know how to do it in the future.
Thank you.
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
// The personType.h header file
#include <string>
using namespace std;
class personType
{
public:
void print() const;
void setName(string first, string last, string middle);
string setFirstName(string first);
string setMiddleName(string middle);
string setLastName(string last);
string getFirstName() const;
string getLastName() const;
string getMiddleName() const;
personType(string first = "", string last = "", string middle = "");
private:
string firstName;
string lastName;
string MiddleName;
};
//
//
//
// The behind the scene .cpp file
#include <string>
#include "personType.h"
#include <iostream>
using namespace std;
void personType::print() const
{
cout << firstName << " " << MiddleName << " " << lastName;
}
void personType::setName(string first, string middle, string last)
{
firstName = first;
MiddleName = middle;
lastName = last;
}
// Figure out why setFirst,Middle, last isnt working.
// void personType::setFirstName(string first)
//{
// firstName = first;
//}
//void personType::setMiddleName(string middle)
//{
// MiddleName = middle
//}
// void personType::setLastName(string last)
//{
// lastName = last;
//}
string personType::getFirstName() const
{
return firstName;
}
string personType::getLastName() const
{
return lastName;
}
string personType::getMiddleName() const
{
return MiddleName;
}
personType::personType(string first, string middle, string last)
{
firstName = first;
lastName = last;
MiddleName = middle;
}
// int main()
#include <iostream>
#include "personType.h"
using namespace std;
int main()
{
personType user;
personType student;
string firstN;
string middleN;
string lastN;
personType student("Oliver", "Benjamin", "Sue");
student.print();
cout << endl;
if (student.getLastName("Sue"))
cout << "Student\'s last name is Sue" << endl;
else
cout << "Student\'s name is not Sue" << endl;
system("pause");
return 0;
}
|