CS 222/420 Winter 1999, Instructor: Jeffrey Horn
HOMEWORK 2: (program
1) INTRODUCTION TO C++ for programmers
| Handed out/Assigned: | Monday, Feb. 1, 1999 |
NOTICE (Feb. 10, 1999): My apologies to all for the problems in handing out this assignment! It is now ready to go, official and all that. I.e., the code WORKS and the assignment is do-able by new C++ programmers. Trust me! Check out the NEW code here, and example executable ("example.prog"), and example output.
OVERALL: Get the code running, and in the process get used to our programming environment, C syntax, and definition and use of C++ classes.
WHAT TO HAND IN: Send me the ascii text file by email to jhorn@nmu.edu. And DO comment! (I always find it easier to go back and add comments later, but whatever works for you. See my style of commenting. At the very least, comment modules such as classes, methods, and functions, that you write or modify.)
PURPOSE: Sort of an advanced "hello world" to get started!
SPECIFICS: I have written most pieces for you. In particular, I have written the classes Individual and ArrayOfPeople, and also a main that should test it. The code below works; that is, it compiles under the gnu C++ compiler on GA and on Euclid, and runs. Use "g++ main.cc". If no compiler errors are generated (warnings should not stop the compilation, and you might get some of those!), the compiler will leave the executable in a file called "a.out" in the same directory in which you ran g++. You should then be able to run your executable with "a.out", or if you have a different shell setting than I have, you might have to type "./a.out" to force your Unix shell to look in the current directory for a.out!
The code is split up among three files: main.cc, individual.cc, and arrayofpeople.cc.
THE CODE: (you can go directly to the files by clicking here)
Here is the definition for an individual, in
"individual.cc":
Note how an individual contains not only the data but also methods to print and initialize.
// INDIVIDUAL.CCclass Individual
{
public:int age;
long employee_id;
float weight;
float data[1000];
Individual(){}; // Default constructor. Don't need to do anything.
// Constructor for initializing data fields with values GIVEN as
// input parameters.Individual(long input_employee_id, int input_age, float input_weight)
{
employee_id = input_employee_id;
age = input_age;
weight = input_weight;
}Init(int tempint) // Initialize the data fields based on some input value "tempint".
// This method is just a quick and dirty way to get different
// individuals. Values are pretty meaningless!
{
employee_id = tempint;
weight = 10*tempint+5;
age= 100*tempint+9;
}
Print() // Print to console all three data field values of this individual,
// all on the same line.
{
cout << "\n EMPLOYEE ID: " << employee_id << " AGE: " << age << " WEIGHT: "<< weight;
};
};
WHAT TO DO:
(0) Download all files into your own directory.
(I) Add a "Print_All_Employees()" class to the ArrayOfPeople class. Test it!
(II) Add code to implement the array reversal method in array of people.
(III) Test and comment your code!
(IV) Send in your source!