Revision dated 08/21/18
Educational Objectives. After successfully completing this assignment, the student should be able to accomplish the following:
Operational Objectives: Define and implement the class ID and deliver the code in two files id.h and id.cpp along with a makefile for the supplied test harness.
Deliverables: id.h, id.cpp, makefile, log.txt
============================================= student build: [2 pts] [0..2]: x assess build: [10 pts] id.o [0..2]: x idtest1.o [0..2]: x idtest2.o [0..2]: x idtest1.x [0..2]: x idtest2.x [0..2]: x tests: 20 points [20 pts] IDtest1.x [0..10]: xx IDtest2.x [0..10]: xx code: [8 pts] constructor 0 0/1: x init list 0/1: x constructor 2 0/1: x init list 0/1: x copy constructor 0/1: x init list 0/1: x destructor 0/1: x assignment operator 0/1: x engineering etc: [10 pts] requirements [0..4]: x coding standard [0..4]: x log.txt [0..2]: x dated submission deduction [2 pts per]: (xx) -- total [0..50]: xx =============================================
See lecture notes Chapter 4. Classes Part 1, Chapter 5. Pointers, Chapter 6. Classes Part 2, and Chapter 7: C-Strings.
See also the COP3014 Notes on C-Strings.
Begin as usual by creating your assignment directory and copying the distribution files for the assignment:
cp ~cop3330p/LIB/proj2/* ~/cop3330/proj2/ cp ~cop3330p/LIB/area51/id*.x ~/cop3330/proj2/
Then a long listing of your assignment directory should look like this:
-rw------- 1 xxxxxxxx CS-Class 505 Jan 14 11:45 deliverables.sh -rw------- 1 xxxxxxxx CS-Class 16469 Jan 14 11:46 idtest1_i.x -rw------- 1 xxxxxxxx CS-Class 13355 Jan 14 11:46 idtest2_i.x -rw------- 1 xxxxxxxx CS-Class 12216 Jan 14 11:46 idtest2ShallowCopy_i.x -rw------- 1 xxxxxxxx CS-Class 2030 Jan 14 11:45 idtest1.cpp -rw------- 1 xxxxxxxx CS-Class 1306 Jan 14 11:45 idtest2.cpp
The executables are for demonstration purposes. You can erase these and get them back by copying again. After invoking "clean ." to declutter the directory and "chmod 700 submit.sh" to set permissions on the submit script, a long listing should be:
-rw------- 1 xxxxxxxx CS-Class 505 Jan 14 11:45 deliverables.sh -rw------- 1 xxxxxxxx CS-Class 2030 Jan 14 11:45 idtest1.cpp -rw------- 1 xxxxxxxx CS-Class 1306 Jan 14 11:45 idtest2.cpp
Now continue to the next step:
Begin your log file named log.txt. (See Assignments for details.)
Create a makefile that builds executables idtest1.x and idtest2.x. Look at the #include statements in idtest1.cpp and idtest2.cpp to deduce what the intermediate targets and dependencies should be.
Design the class ID, placing the definition in file id.h
Implement the class ID, placing the class implementation in file id.cpp. You can test the code for syntax errors with the command "make id.o" or the command "co3330 id".
Thoroughly test class ID, starting out with the supplied test harnesses in file proj2/idtest?.cpp using your makefile to build the executables. (Note - you could also use the command line compile script "co3330" to create object files and then and create an executable at the command line.)
Behavior of your executables should be identical to those of the area51 benchmark executables.
Turn in id.h, id.cpp, and makefile using the submit system.
Warning: Submit scripts do not work on the program and linprog servers. Use shell.cs.fsu.edu or quake.cs.fsu.edu to submit projects. If you do not receive the second confirmation with the contents of your project, there has been a malfunction.
The class should implement the following diagram:
Class Name: | ID |
Services : |
void SetName ( const char* ) // sets the name field void SetAge ( int ) // sets the age field const char* GetName () const // returns a const pointer to the name field int GetAge () const // returns the age field by value |
Properties : |
Constructable: objects can be declared as ordinary variables Assignable: objects can be assigned one to another Passable: objects can be passed by value to and returned as values from functions |
Private variables: |
char * name_ // the name field int age_ // the age field |
The class should be a proper type, to include default constructor, 2-argument constructor (that initializes the two data fields), copy constructor, assignment operator, and destructor. Note that the default constructor should set the name to "#" and the age to -1.
The output operator operator<< should be overloaded for the type ID.
Class ID should pass testing with the supplied proj2/idtest?.cpp with no compile or runtime errors and no compiler warnings when the warning flags -Wall, -Wextra are set.
Building and running the supplied proj2/idtest?.cpp should result in output identical to the supplied executable area51/idtest_?.x [? = i or s] .
Hints
The "resource allocation" aspects of this assignment are where most mistakes are made - not just by students doing this assignment, but also throughout the professional world: failure to correctly and safely manage C strings has been at the root of many security leaks and system intrusions over the years.
The distributed "idtest2ShallowCopy.x" shows one of the things that can go wacky if the programming isn't up to muster. What you see on the screen is:
IDs after declaration: p1 = Chris Lacher 99 p2 = Tony Harris 17 IDs after client program changes myName and myAge: p1 = Tony Harris 99 p2 = Dalton Bohning 17
Note that the internal info in ID objects is changed by an action taken by a client program on its own variables myName and tonyName. These are C strings owned by the test program, which is a client of class ID. The problem is that the ID objects are pointing to the "name" C strings instead of owning a copy of them.
These issues will arise repeatedly through this course and many others that have it as pre-requisit. You can save yourself a lot of pain and improve your grades if you stick with this issue until you have learned it well enough not to forget it, ever.