Lab3: Creating a Simple Class

Let’s use our Star Wars name generator code and build a simple class to manage the information needed to generate these names.

Part 1: Create a basic Class

Create a class called Person that will store the information needed to generate a Star Wars name for a person. Refer to Example C++ Program for the code to generate that name.

Here is the header file you should use for this class:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef PERSON_H
#define PERSON_H

#include <string>
using namespace std;

class Person {
    private:
        string first_name;
        string last_name;
        string maiden_name;
        string birth_town;

    public:
        void set_name(string, string);  // store the first and last name strings
        void set_maiden(string);        // store maiden name staring
        void set_birth(string);         // store birth town string

        string gen_star_wars(void);     // return the star wars name from loaded data
};

#endif

You will need to create two additional files for this project:

  • Person.cpp

  • main.cpp

Build your program so the main function (in main.cpp) creates one object of type Person. Using that object load the required data using the methods defined in the class, then generate your Star Wars name to show that your class works properly. You can hand code your data for a sample person for this part.

Once that is working, move on to step 2

Part 2: Load your data from a file

Modify your program so it reads the four input strings needed for a person from standard input (using cin). Test your new program to make sure it still works as before, only now reading the four names from user input.

Next, create a test file named name.data containing this data:

Fred Flintstone Neanderthal Bedrock

That is all on one line.

Then, assuming your program executable is named lab3.exe, open up a command prompt and navigate to the project directory. Make sure your program file and data file are both in that directory, then do this:

$ lab3 < names.dat

This is called command redirection, and it makes the operating system feed the contents of a file to your program, which still thinks a human is tying things in. The difference is that this human types really fast!

YOu should see your Star Wars name for this person.

Part 3: Read more names

Now modify your program so it creates an array of Person objects. This is as simple as this:

Person people[5];

Now, create a loop around the part of your code that reads in the data and loads it in the object for a person. This time, we will load one of the objects in the people array. Assuming your re using a for-loop with i as the counter variable, your code would look something like this:

cin >> first;
cin >> last;
people[i].set_name(first, last)

Next, add another method to the Person class called get_name that returns a string that looks like this: “first last”.

With this method in place generate a final display of names and associated start wars names that looks like this:

Fred Flintstone: Flifr Nebed
...

You should modify your name.dat file so it contains exactly five sets of data for your run. Use the same command used in Part 2 to see your output.