I have an assignment and could use some help with it. I have part of it working, but not all of it.
Here is my assignment.
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:
Set the first name only.
Set the last name only.
Store and set the middle name.
Check whether a given first name is the same as the first name of this person.
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 am not sure how to get these to work.
isFirstName correctly determines if a name is a student's first name.
Error:
Test 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
In file included from /usr/include/gtest/gtest.h:58:0,
from /root/sandboxe269868a/nt-test-9d0bdb40.cpp:1:
/root/sandboxe269868a/nt-test-9d0bdb40.cpp: In member function 'virtual void isFirstName_1_Test::TestBody()':
/root/sandboxe269868a/nt-test-9d0bdb40.cpp:5:25: error: 'class personType' has no member named 'isFirstName'; did you mean 'setFirstName'?
ASSERT_TRUE(student.isFirstName("Robert"));
^
make[2]: *** [CMakeFiles/runTests.dir/nt-test-9d0bdb40.cpp.o] Error 1
make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
make: *** [all] Error 2
TEST(isFirstName, 1) {
personType student("Robert", "Newton", "Ford");
ASSERT_TRUE(student.isFirstName("Robert"));
}
|
Test 2
Error:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
In file included from /usr/include/gtest/gtest.h:58:0,
from /root/sandboxe269868a/nt-test-04500d0a.cpp:1:
/root/sandboxe269868a/nt-test-04500d0a.cpp: In member function 'virtual void isFirstName_2_Test::TestBody()':
/root/sandboxe269868a/nt-test-04500d0a.cpp:5:26: error: 'class personType' has no member named 'isFirstName'; did you mean 'setFirstName'?
ASSERT_FALSE(student.isFirstName("Newton"));
make[2]: *** [CMakeFiles/runTests.dir/nt-test-04500d0a.cpp.o] Error 1
make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
make: *** [all] Error 2
TEST(isFirstName, 2) {
personType student("Robert", "Newton", "Ford");
ASSERT_FALSE(student.isFirstName("Newton"));
}
|
Test 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
In file included from /usr/include/gtest/gtest.h:58:0,
from /root/sandboxe269868a/nt-test-75d7a428.cpp:1:
/root/sandboxe269868a/nt-test-75d7a428.cpp: In member function 'virtual void isFirstName_3_Test::TestBody()':
/root/sandboxe269868a/nt-test-75d7a428.cpp:5:25: error: 'class personType' has no member named 'isFirstName'; did you mean 'setFirstName'?
ASSERT_TRUE(student.isFirstName("John"));
^
make[2]: *** [CMakeFiles/runTests.dir/nt-test-75d7a428.cpp.o] Error 1
make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
make: *** [all] Error 2
TEST(isFirstName, 3) {
personType student("John", "", "Smith");
ASSERT_TRUE(student.isFirstName("John"));
}
|
isLastName correctly determines if a name is a student's first name.
Errors:
Test 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
In file included from /usr/include/gtest/gtest.h:58:0,
from /root/sandbox08f211d4/nt-test-5d3ffe1d.cpp:1:
/root/sandbox08f211d4/nt-test-5d3ffe1d.cpp: In member function 'virtual void isLastName_1_Test::TestBody()':
/root/sandbox08f211d4/nt-test-5d3ffe1d.cpp:5:25: error: 'class personType' has no member named 'isLastName'; did you mean 'setLastName'?
ASSERT_TRUE(student.isLastName("Ford"));
^
make[2]: *** [CMakeFiles/runTests.dir/nt-test-5d3ffe1d.cpp.o] Error 1
make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
make: *** [all] Error 2
TEST(isLastName, 1) {
personType student("Robert", "Newton", "Ford");
ASSERT_TRUE(student.isLastName("Ford"));
|
Test 2
1 2 3 4
|
TEST(isLastName, 2) {
personType student("Robert", "Newton", "Ford");
ASSERT_FALSE(student.isLastName("Newton"));
}
|
Test 3
1 2 3 4 5
|
TEST(isLastName, 3) {
personType student("John", "", "Smith");
ASSERT_FALSE(student.isFirstName("Smith"));
}
|
These ones work:
The program can initialize a personType object without error.
personType getters and setters work as expected.
Here is my code that I have so far.
Main.cpp file
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
|
#include <iostream>
#include "personType.h"
#include <string>
using namespace std;
int main(){
personType student = personType("Robert","Newton","Ford");
//e. Check whether a given last name is the same as the last name of this person.
if(student.isFirstNameSame("Robert")){
cout<<"First name is the same"<<endl;
}else{
cout<<"First name is not same"<<endl;
}
//e. Check whether a given last name is the same as the last name of this person.
if(student.isLastNameSame("Wood")){
cout<<"Last name is not the same"<<endl;
}else{
cout<<"Last name is not same"<<endl;
}
}
|
I am not sure if these would work for the to fix the issue I am having. I tried adding them to the main file but they were giving me errors.
1 2 3 4 5 6
|
void personType::setFirstName(std::string fname){
if (firstName == "")
firstName = fname;
}
|
and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
personType student2 = personType("John");
student2.setFirstName("Oleg"); // doesn't set
if(student2.isFirstNameSame("Jane")){
cout<<"First name is the same "<< student2.getFirstName() <<endl;
}else{
cout<<"First name is not same "<< student2.getFirstName() << endl;
}
student2.setLastName("Smith");
if(student2.isLastNameSame("Smith")){
cout<<"Last name is the same "<< student2.getLastName() <<endl;
}else{
cout<<"Last name is not same "<< student2.getLastName() <<endl;
}
|
personType.h file
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
|
#ifndef PERSONTYPE_H
#define PERSONTYPE_H
#include<string>
class personType
{
public:
personType(std::string fname="",std::string lname="",std::string mname="");
//a. Set the first name only.
void setFirstName(std::string);
//b. Set the last name only.
void setLastName(std::string);
//c. Store and set the middle name.
void setMiddleName(std::string);
bool isFirstNameSame(const std::string &);
bool isLastNameSame(const std::string &);
std::string getFirstName() const;
std::string getLastName() const;
std::string getMiddleName() const;
//Class member variables must be implemented as private.
private:
std::string firstName;
std::string lastName;
//c. Store and set the middle name.
std::string middleName;
};
#endif
|
personTypeimp.cpp file
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
|
#include "personType.h"
#include <string>
using std::string;
personType::personType(std::string fname,std::string lname,std::string mname){
firstName=fname;
lastName=lname;
middleName=mname;
}
void personType::setFirstName(std::string fname){
firstName=fname;
}
void personType::setLastName(std::string lname){
lastName=lname;
}
void personType::setMiddleName(std::string mname){
middleName=mname;
}
//Functions in exercise 9 d. and e. must return a
//boolean indicating whether the specified names are identical or not.
bool personType::isFirstNameSame(const std::string & fname){
return firstName.compare(fname)==0;
}
//Functions in exercise 9 d. and e. must return a
//boolean indicating whether the specified names are identical or not.
bool personType::isLastNameSame(const std::string & lname){
return lastName.compare(lname)==0;
}
std::string personType::getFirstName() const{
return firstName;
}
std::string personType::getLastName() const{
return lastName;
}
std::string personType::getMiddleName() const{
return middleName;
}
|