Educational Objectives. After successfully completing this assignment, the student should be able to accomplish the following:
Operational Objectives: Define and implement the class Product and deliver the code in two files product.h and product.cpp along with a makefile for the supplied test harness.
Deliverables: product.h, product.cpp, makefile
See lecture notes Chapter 4. Classes Part 1, Chapter 5. Pointers, Chapter 6. Classes Part 2, and Chapter 7: String Objects.
Begin as usual by creating your assignment directory and copying the distribution files for the assignment:
cp ~cop3330p/fall14/hw3/* ~/cop3330/hw3/ cp ~cop3330p/fall14/area51/Product*.x ~/cop3330/hw3/
Then a long listing of your assignment directory should look like this:
-rw------- 1 xxxxxxxx CS-Class 505 Sep 14 11:45 hw3submit.sh -rw------- 1 xxxxxxxx CS-Class 16469 Sep 14 11:46 Producttest1.x -rw------- 1 xxxxxxxx CS-Class 13355 Sep 14 11:46 Producttest2.x -rw------- 1 xxxxxxxx CS-Class 12216 Sep 14 11:46 Producttest2ShallowCopy.x -rw------- 1 xxxxxxxx CS-Class 2030 Sep 14 11:45 test1.cpp -rw------- 1 xxxxxxxx CS-Class 1306 Sep 14 11:45 test2.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 hw3submit.sh" to set permissions on the submit script, a long listing should be:
-rwx------ 1 xxxxxxxx CS-Class 505 Sep 14 11:45 hw3submit.sh -rw------- 1 xxxxxxxx CS-Class 2030 Sep 14 11:45 test1.cpp -rw------- 1 xxxxxxxx CS-Class 1306 Sep 14 11:45 test2.cpp
Now continue to the next step:
Create a makefile that builds executables test1.x and test2.x. Look at the #include statements in test1.cpp and test2.cpp to deduce what the intermediate targets and dependencies should be.
Design the class Product, placing the definition in file product.h
Implement the class Product, placing the class implementation in file product.cpp. You can test the code for syntax errors with the command "make product.o" or the command "co3330 product".
Thoroughly test class Product, starting out with the supplied test harnesses in file hw3/test?.cpp using your makefile to build the executables. (Note - you could also use the command line compile scripts "co3330" to create object files and then and "g++ -otest1.x product.o test1.o" to create executables, as in Homework 1.)
Turn in product.h, product.cpp, and makefile using the hw3submit.sh submit script.
Warning: Submit scripts do not work on the program and linprog servers. Use shell.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: | Product |
Services : |
void SetName ( const char* ) // sets the name field void SetBarCode ( uint32_t ) // sets the bar code field void SetCost ( float ) // sets the cost field const char* GetName () const // returns a const pointer to the name field uint32_t GetBarCode () const // returns the bar code by value float GetCost () const // returns cost 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 product name uint32_t code_ // the product bar code float cost_ // the product cost |
The class should be a proper type, to include default constructor, 3-argument constructor (that initializes the three data fields), copy constructor, assignment operator, and destructor. Note that the default constructor should set the name to "#", the bar code to 0x00000000, and the cost to 0.0.
Be sure to use initialization lists for all of the constructors, including the copy constructor.
The output operator operator<< should be overloaded for the type Product. Display the three fields with TAB character between them. (Don't output any newlines.)
Class Product should pass testing with the supplied hw3/test?.cpp with no compile or runtime errors and no compiler warnings when the warning flags -Wall and -Wextra are set. The test compiler is g++47 -std=c++11 on linprog. This compiler and library is as close to compliance with c++11 as we have available.
Building and running the supplied hw3/test?.cpp should result in output identical to the supplied executable area51/Producttest?.x [? = 1 or 2] .
Hints
Note that the product name of p1 has changed (as you would expect after assignment p2 = p1), but the program "crashes" with a message "double free or corruption". What has happened is that the assignment operator made a "shallow copy" of the product name string. As the program terminates, the Product destructor is called for both p2 and p1, which results in a call to delete[] name_. Because of the shallow copy, delete[] is called twice on the same allocation, which generates the error.lacher@linprog2.cs.fsu.edu:~/3330/hw3>test2d.x Products after declaration: p1 = product_1 FEDCBA98 100.00 p2 = product_2 89ABCDEF 200.00 Products after assignment p2 = p1 p1 = product_1 FEDCBA98 100.00 p2 = product_1 FEDCBA98 100.00 *** glibc detected *** test2d.x: double free or corruption (fasttop): 0x0000000004b5e010 *** ======= Backtrace: ========= ...