question archive The Name Class An Empty String (before we begin) In this part of the workshop, we refer to a Cstring as empty if it has no characters or it has only spaces
Subject:Computer SciencePrice: Bought3
The Name Class
An Empty String (before we begin)
In this part of the workshop, we refer to a Cstring as empty if it has no characters or it has only spaces. So "" and " " are both considered empty strings it this senario.
Name
Create class encapsulating a Name.
A Name object encapsulates three parts; First name, Middle name, and last name. The lengths of all three parts are unknown and should be kept dynamically.
construction
A Name object can be created in four different ways: 1- No argument. In this case, the Name is in a safe empty state. 2- One argument (Cstring). Only the first name is set. 3- Two arguments (Cstring), First name and last name are set. 4- Three arguments (Cstring), First, Middle and Last names are set.
For the latest three, if any of the parts of the name provided are null or empty, the name object will be set into an empty state.
Rule of three
A name can be safely copied or assigned to another name and its destruction will cause no memory leak.
Short form.
The name must have a modifier method called "setShort" that accepts a boolean argument. If the argument passed is true, the middle name will be abbreviated when printed. (See Insertion to ostream for more detail)
Adding a Cstring
A Cstring can be added to a Name object using a += operator, returning the reference of the object as follows.
When a Cstring is added to the name, it will be used to set the next available part of the name:
Extraction from istream
Using operator>>, a Name can be extracted from the istream. A name entry is space separated ending with a newline. A valid name can be:
1. FirstName<NEW LINE>
2. FirstName<SPACE>LastName<NEW LINE>
3. FirstName<SPACE>MiddleName<SPACE>LastName<NEW LINE>
If after the third part a new line is not immediately following the last name the object is set to an invalid state:
FirstName<SPACE>MiddleName<SPACE>LastName<SPACE>Whatever
Hint: use the standard C++ string class to read the name parts from the console before dynamic memory allocation. See the example under the "String Class" section in Input and Operators Chapter. Note that this is the only case in which you are allowed to use the standard C++ string class.
Insertion to ostream
Using operator<<, a Name can be inserted into ostream.
When a name is inserted into ostream and all three parts are not null, they will be printed separated by a space:
{"Homer", "Jay", "Simpson"}
is printed as
Homer Jay Simpson
If the Name is set to be displayed in Short format the middle name will be abbreviated:
{"Homer", "Jay", "Simpson"}
is printed as
Homer J. Simpson
If the middle name is null, then the first and last name is printed separated by a space:
{"Bart", nullptr, "Simpson"}
is printed as
Bard Simpson
If the middle name and last name are null, then the name is printed:
{"Lisa", nullptr, nullptr}
is printed as
Lisa
If the name is invalid Bad Name is printed.
State of validity (type conversion)
If a Name object is casted to the Boolean type it should return false if it is in an invalid empty state otherwise it should return true; (A Name is valid if at least the first name is not null)
--------------------------------------------------------------------------------------------------
OUTPUT
Tester Output
Names;
First (Middle) Last
Homer
Homer Simpson
Homer Jay Simpson
Bad Name
Enter the follwing:
> Homer Jay Simpson Adams
> Homer Jay Simpson Adams
Bad Name entry!!!
Enter the follwing:
> Homer Jay Simpson
> Homer Jay Simpson
Name: Homer Jay Simpson
Short: Homer J. Simpson
Enter the follwing:
> Homer Simpson
> Homer Simpson
Name: Homer Simpson
Short: Homer Simpson
Enter the follwing:
> Homer
> Homer
Name: Homer
Short: Homer
Milhouse+= "": Milhouse
Milhouse+= " ": Milhouse
Milhouse Van+= Van: Milhouse Van
Milhouse Van Houten+= Houten: Milhouse Van Houten
Bad Name+= Adams: Bad Name
Fred+= Fred: Fred
An assigned name: Homer Jay Simpson
A self assigned name: Homer Jay Simpson
Names in the file in short form:
-------------------------------
Abraham Simpson
Agnes Skinner
Akira Kurosawa
Alice Glick
Allison Taylor
Apu Nahasapeemapetilon
Artie Ziff
Baby Gerald
Barney Gumble
Bart Simpson
Bernice Hibbert
Brandine Spuckler
Bumblebee Man
Carl Carlson
Chazz Busby
Cletus Spuckler
Comic B. Guy
Dewey Largo
Disco Stu
Dolph Starbeam
Drederick Tatum
Edna Krabappel
--------------Bad record!!!
Gloria Jailbird
Grounds k. Willie
Hans Moleman
Helen Lovejoy
Herman Hermann
Homer J. Simpson
Jacqueline Bouvier
--------------Bad record!!!
Jessica Lovejoy
Jimbo Jones
Johnny Tightlips
Judge R. Snyder
Kearney Zzyzwicz
Kent Brockman
Kirk V. Houten
Krusty T. Clown
Kumiko Albertson
Lenny Leonard
Lindsey Naegle
Ling Bouvier
Lionel Hutz
Lisa Simpson
Luann V. Houten
Luigi D. Risotto
Lunchlady Doris
Maggie Simpson
Manjula Nahasapeemapetilon
Marge Simpson
Martin Prince
Maude Flanders[D]
Mayor J. Quimby
Milhouse V. Houten
Miss Springfield
Moe Szyslak
Mona Simpson
Ned Flanders
Nelson Muntz
Old J. Man
Patty Bouvier
Rabbi H. Krustofsky
Rainier Wolfcastle
Ralph Wiggum
Rod Flanders
Roger M. Jr.
Ruth Powers
Sarah Wiggum
Sea Captain
Selma Bouvier
Seymour Skinner
Shauna Chalmers
Sideshow Mel
Snake Jailbird
Squeaky V. Teen
Surly Duff
The R. Texan
Todd Flanders
Troy McClure
Uter Zorker
Waylon Smithers
Wendell Borton
Wise Guy
----> Files which are included
/--main.cpp---/
#include <iostream>
#include <fstream>
#include "Name.h"
using namespace sdds;
using namespace std;
void printNames(const Name* n, int size=1) {
cout << "Names;" << endl << "First (Middle) Last" << endl;
for (int i = 0; i < size; i++) {
cout << n[i] << endl;
}
}
// get a copy of the Name
void printName(Name N, bool shortOnly= false) {
if (!shortOnly) {
N.setShort(false);
cout << "Name: " << N << endl;
}
N.setShort(true);
if (!shortOnly) cout << "Short: ";
cout << N << endl;
}
void nameTester(const char* prompt) {
Name N;
cout << prompt;
cin >> N;
if (N) {
printName(N);
}
else {
cout << N << " entry!!!" << endl;
cin.ignore(1000, 'n');
}
}
int main() {
Name A[4] = { {"Homer"},{"Homer", "Simpson"}, {"Homer", "Jay", "Simpson"} };
Name N = "Milhouse";
printNames(A, 4);
nameTester("Enter the follwing:n> Homer Jay Simpson Adamsn> ");
nameTester("Enter the follwing:n> Homer Jay Simpsonn> ");
nameTester("Enter the follwing:n> Homer Simpsonn> ");
nameTester("Enter the follwing:n> Homern> ");
cout << N << "+= "": " << (N += "") << endl;
cout << N << "+= " ": " << (N += " ") << endl;
cout << N << "+= Van: " << (N += "Van") << endl;
cout << N << "+= Houten: " << (N += "Houten") << endl;
cout << N << "+= Adams: " << (N += "Adams") << endl;
cout << A[3] << "+= Fred: " << (A[3] += "Fred") << endl;
A[1] = A[2];
cout << "An assigned name: " << A[1] << endl;
A[2] = A[2];
cout << "A self assigned name: " << A[1] << endl;
ifstream names("names.txt");
cout << endl << "Names in the file in short form:" << endl
<< "-------------------------------" << endl;
while (names) {
// this will call operator>> since ifstream is an istream but for files
names >> N;
if (names) {
if (N) {
printName(N, true);
}
else {
cout << "--------------Bad record!!!" << endl;
names.clear();
names.ignore(1000, 'n');
}
}
}
return 0;
}
//----- FILE
1) Name.cpp
2) Name.h
3) main.cpp //already attached no implamentation needed
) TEXT FILE
Name.txt
Abraham Simpson
Agnes Skinner
Akira Kurosawa
Alice Glick
Allison Taylor
Apu Nahasapeemapetilon
Artie Ziff
Baby Gerald
Barney Gumble
Bart Simpson
Bernice Hibbert
Brandine Spuckler
Bumblebee Man
Carl Carlson
Chazz Busby
Cletus Spuckler
Comic Book Guy
Dewey Largo
Disco Stu
Dolph Starbeam
Drederick Tatum
Edna Krabappel
Elizabeth Hoover Gil Gunderson
Gloria Jailbird
Grounds keeper Willie
Hans Moleman
Helen Lovejoy
Herman Hermann
Homer Jay Simpson
Jacqueline Bouvier
Janey Powell Jasper Beardly
Jessica Lovejoy
Jimbo Jones
Johnny Tightlips
Judge Roy Snyder
Kearney Zzyzwicz
Kent Brockman
Kirk Van Houten
Krusty The Clown
Kumiko Albertson
Lenny Leonard
Lindsey Naegle
Ling Bouvier
Lionel Hutz
Lisa Simpson
Luann Van Houten
Luigi Don Risotto
Lunchlady Doris
Maggie Simpson
Manjula Nahasapeemapetilon
Marge Simpson
Martin Prince
Maude Flanders[D]
Mayor Joe Quimby
Milhouse Van Houten
Miss Springfield
Moe Szyslak
Mona Simpson
Ned Flanders
Nelson Muntz
Old Jewish Man
Patty Bouvier
Rabbi Hyman Krustofsky
Rainier Wolfcastle
Ralph Wiggum
Rod Flanders
Roger Meyers Jr.
Ruth Powers
Sarah Wiggum
Sea Captain
Selma Bouvier
Seymour Skinner
Shauna Chalmers
Sideshow Mel
Snake Jailbird
Squeaky Voiced Teen
Surly Duff
The Rich Texan
Todd Flanders
Troy McClure
Uter Zorker
Waylon Smithers
Wendell Borton
Wise Guy