c++ Lab
CS 144 Computer Science I
Lab 09: Pointers
Goals After completing the lab, students should be able to:
• Divide code into separate header and source files
• Declare classes
• Implement class member functions
• Implement constructors and destructors
• Manage dynamic memory
Resources Use all of the resources available to you. These may include (in no particular order):
• The book
• Videos and slides on Visual Studio (under Home → How-To → Visual Studio)
• The slides on commenting (under Home → Module 1)
• Videos on Doxygen comments (under Home → How-To → Doxygen Comments)
• Videos on Web-CAT (under Home → How-To → Web-CAT)
• Videos and slides on zipping/unzipping files (under Home → How-To → Zipping Files)
Activities In this lab, you will be creating two small classes that will be used in Project 3.
Important Note: Since this code for this lab will be used in Project 3, it is very important that it work
correctly. Test it and make sure that everything works as expected.
Lab Instructions
For this lab, you will be creating two classes, and these contain several member functions. This will be
spread out across multiple files. You will be using main for testing only. The details for each class are
listed below.
Note: Since this lab is creating code for Project 3, the classes will not be doing much on their own.
However, they will form an important part of Project 3.
Comments
You are required to comment all of your code. This includes a Doxygen comment for the file, for each
class and for each function in the code (including main). The information in the comments should be
complete, accurate, and useful. Ensure that all required tags are present. Refer to the notes on
commenting and the guidelines posted under Home -> Module 1 and under Home -> How-To ->
Doxygen Comments for detailed information about what is needed. There is also an example in the
Documenting Classes section below.
CS 144 Computer Science I
Be warned that ~20% of your lab grade is dependent on your comments. While it may take a little time
to do, it is not difficult, so there is no reason for you not to receive those points.
Files
You are required to separate your code into header and source files. The header files should contain the
declarations and should not contain any implementations. Implementation of the functions should be
in the source files.
Important Note: You are required to have a header file called p3.h. This file will #include the other
header files in your lab and nothing else. As such, this file will only be a few lines long. (This is partially
being done for testing purposes. The test file will #include this header to access the class definitions in
your code.)
You may name your other header and source files as you see fit (within reason).
It is strongly suggested that you place each class in its own header and source file.
It is also strongly suggested that you place main in its own source file. (That file would not need to be
submitted to Web-CAT.)
Testing
You are required to test your code. See the individual functions for testing requirements. Some testing
hints are at the end of the document in the Testing section.
Extra and Gift (Lab09)
1. The goal for Project 3 is to create part of a program for a store that specializes in gifts for special
occasions (holidays, birthdays, etc.). The store needs to store information about the gifts that
have been selected and the extras that have been added to each gift (extra flowers, teddy bears,
etc.). This lab will have you build a class to represent each Extra that can be added to a gift and a
class to represent each Gift.
Each Gift will store it own name, id, and cost and up to 6 extras. Each Extra has the extra’s name,
price and whether it is perishable or not.
Important note: Your main function will only be used for testing purposes and will not be
graded.
Important note: cin and cout will only be used for testing purposes. The main function may use
cin and cout but if you use them in any other functions, they should be commented out before
submitting to Web-CAT.
Important note: Only #include the files you need. Each .cpp file should include the
corresponding header file. If other files are needed, include them too. Do not include files
because they are there. For instance, Extra has no need to know about Gift. Do not have an
include for Gift in any of the Extra files.
CS 144 Computer Science I
2. Create a class called Extra. This will be a simple class that stores the extra’s name, price and
whether it is perishable or not. This should be done in a header file.
a. Declare variables for the class. It needs to store the extra name (string), the price
(double) and whether it is perishable (bool). These should be private.
b. Declare a constructor for the class. It should take (in order) the extra’s name, price and
if it is perishable.
c. Declare a getter for the extra’s name called getExtraName. It should take no
parameters and return a string. This function should be const. This will get the extra’s
name.
d. Declare a getter for the price called getPrice. It should take no parameters and return a
double. This function should be const. This will get the extra’s price.
e. Declare a getter for if it is perishable called isPerishable. It should take no parameters
and return a bool. This function should be const. This will return true if the extra is
perishable and false if not.
3. Implement the Extra class. This should be done in a source file.
a. In the constructor, assign the parameters to the class variables. If the extra’s name
parameter is an empty string (i.e., ""), the class’s extra’s name variable should be set
to "Awesomeness". If the price parameter is less than 4.95, the class’s price variable
should be set to 4.95.
b. The getters should just return the appropriate values.
c. Create at least 3 tests for this class. Put these in the Doxygen comments for the class.
You should be able to create an Extra object and then call getExtraName, getPrice and
isPerishable to get the values in the object. See the testing section below for some
examples.
d. (Make sure you include your header file in the source file.)
e. (Make sure the header file is also included in p3.h.)
4. Document your class and files.
5. Create a class called Gift. This will be a slightly more complex class that stores a gift name, an id
and a cost for the gift and up to 6 extras. This should be done in a header file.
a. Declare the variables for the class. It needs to store the gift name (string), the id (int),
the cost (double), the number of extras for this gift (int) and an array of 6 pointers to
extras (Extra pointers). These should be private.
CS 144 Computer Science I
Note: This is an array that stores pointers to Extras. This is not an Extra pointer that
points to an Extra array (i.e., it is not a dynamic array). So, declare an Extra pointer
variable and then make it an array of size 6.
b. Create a default constructor (no parameters).
c. Create a constructor that takes the gift name (string), id (int) and cost (double) as
parameters.
d. Create a destructor.
e. Create a getter called getGiftName that has no parameters and returns a string. This
function should be const. This will get the gift’s name from the gift.
f. Create a getter called getGiftId that has no parameters and returns an int. This function
should be const. This will get the id from the gift.
g. Create a getter called getGiftCost that has no parameters and returns a double. This
function should be const. This will get the cost from the gift.
h. Create a getter called getNumExtras that has no parameters and returns an int. This
function should be const. This will get the number of extras associated with this gift.
i. Create a getter called getExtra that takes an int parameter (the position of the extra to
retrieve) and returns a const Extra pointer. This function should be const. This will get
an extra from the gift. It will return nullptr if it is not given a valid position.
(Since this function is const and has a const return type this function will look like:
const Extra * getExtra(int slot) const;
This means that the function returns a pointer, but you cannot change the value it is
pointing to.)
j. Create a function called addExtra that takes as parameters a string (the extra’s name), a
double (the price of the extra) and a bool (true if it is perishable). It should return a
bool. This will add a new extra to the gift. The new Extra will be added to the first empty
position in the array (if any). The function returns true if the extra was added to the
array and false if the array is full.
k. Create a function called removeExtra that takes an int (the position of the extra to
remove) as a parameter and returns a bool. This will remove the extra from the gift if
there is one at that position. The function returns true if an Extra was removed and false
if the position was invalid.
CS 144 Computer Science I
l. Create a function called getTotalCost that takes no parameters and returns a double.
This function should be const. This will return the cost of the gift with all of the extras
included.
m. Create a function called hasPerishable that takes no parameters and returns a bool.
This function should be const. This returns true if at least one of the extras for the gift is
perishable. It returns false if none of the extras are perishable.
6. Implement the Gift class. This should be done in a source file.
a. For the constructor with a string, an int and a double as parameters, use the
parameters to set the class’s name, id and cost variables. Set the number of extras
associated with the gift to 0.
b. For the default constructor, do the same thing as with the other constructor except set
the gift’s name to "Surprise! Basket", the id to 0 and the cost to 39.95.
When testing, make sure you test this constructor.
c. In the destructor, delete the extras. Loop over the used portion of the array and delete
each Extra. Do not try to delete the array itself (it is an array of pointers and not a
pointer to an array). Do not try to delete the part of the array that has not had values
put in it yet.
d. In the getters, getGiftName, getGiftId, getGiftCost and getNumExtras, return the
appropriate variables.
e. In getExtra, if the parameter is within the used portion of the array, return the Extra
pointer that corresponds with that position.
If the parameter is not within that range, return nullptr.
f. In addExtra, if there is an empty spot in the array, dynamically create a new Extra
object (using the parameters) and put that new extra in the array at the first available
position. Increment your counter for the number of extras in the gift. Return true.
If there are no empty positions in the array, return false.
Create at least 5 tests for this function that add extras to a gift. You may want to
combine these tests with calls to removeExtra (see below) to see what happens as
extras are added and removed. Put them in the Doxygen comments for the function.
See the testing section below for some examples of how to test.
g. In removeExtra, check if the position (the parameter) is valid. If it is, delete the extra in
that spot in the array.
CS 144 Computer Science I
Starting at that position, loop to the second to last position in the array. In the loop,
copy the value in the array one ahead of the current position into the current position
(i.e., copy position 5 into position 4, position 6 into position 5, etc.).
After the loop, decrement the number of extras in the gift.
Return true.
If the position is not valid, return false.
Create at least 3 tests for this function and put them in the Doxygen comments for the
function. Try testing positions that are invalid. See the testing section below for some
examples of how to test.
h. In getTotalCost, you will be calculating the cost of the gift with the cost of the extras
added in.
Loop over the used portion of the array and sum the prices of each extra. Make sure you
add in the cost of the gift and then return the sum.
Create at least 3 tests for this function and put them in the Doxygen comments for the
function. Try testing gifts with and without extras. See the testing section below for
some examples of how to test.
i. In hasPerishable, loop over the used portion of the array. For each Extra pointer check
to see if the Extra is perishable. If it is, return true. If nothing is perishable, return false
(you have to have checked them all).
Create at least 3 tests for this function and put them in the Doxygen comments for the
function. Try gifts that have perishable extras and ones that do not. See the testing
section below for some examples of how to test.
j. (Make sure you include your header file in the source file.)
k. (Make sure the header file is also included in p3.h.)
7. When testing Gift, make sure you use both constructors (the one that takes a string, an int and
a double and the one with no parameters) in the tests. You want to run all of your code.
8. Document your class and files.
9. Format your classes. You can select the text in a file and press Ctrl + K, Ctrl + D. This will do most
of it for you.
10. Using the instructions in the submission section, submit your .cpp and .h files to Web-CAT. Zip
the files together so you can upload them all at once. View the feedback from Web-CAT and
CS 144 Computer Science I
view your score. If you did not receive full points, look at the files that have errors. Make any
corrections you need and resubmit until you get full points.
Testing
You may use the main function to test your code.
Test the classes by creating an object of that class and calling methods on it. For instance, to test if Extra
is working, you could do something like:
// Extra and Gift Tests Lab 9 Extra ex1("Teddy Bear", 14.75, false); // should print Teddy Bear 14.75 0 (0 for false) cout << "Teddy Bear 14.75 0 -> "; cout << ex1.getExtraName() << " " << ex1.getPrice() << " " << ex1.isPerishable() << "\n"; Extra ex2("", 0, true); // should print Awesomeness 4.95 1 (1 for true) cout << "Awesomeness 4.95 1 -> "; cout << ex2.getExtraName() << " " << ex2.getPrice() << " " << ex2.isPerishable() << "\n"; cout << "\n";
This creates two Extras and sees if the constructors and the getters all work.
Testing the Gift class would be similar.
Gift g1("The Proposal Basket", 5932, 159.50); //try get the extra in slot 2 //ex3 needs to start with const since //getExtra returns a const pointer const Extra *ex3 = g1.getExtra(2); //should be nullptr (0) since there are no extras yet cout << "getExtra\n"; cout << "0 (invalid position) -> "<< ex3 << "\n"; //add an item g1.addExtra("Roses", 33.00, false); ex3 = g1.getExtra(0); //Should be Roses 33 0 //note: it3 is a pointer so it uses the -> instead of . cout << "Roses 33 0 -> "; cout << ex3->getExtraName() << " " << ex3->getPrice() << " " << ex3->isPerishable() << "\n"; cout << "\n"; // hasPerishable cout << "hasPerishable\n0 (false) -> "; cout << g1.hasPerishable() << "\n"; g1.addExtra("Candy", 11.50, true); cout << "1 (true after adding Candy) -> "; cout << g1.hasPerishable() << "\n";
CS 144 Computer Science I
// total cost cout << "\nTotal Cost\n"; // should be 204.0 cout << "204 -> " << g1.getTotalCost() << "\n"; //TODO test all of the other functions
Make sure you have all of the necessary testing.
Documenting Classes
The documentation for your classes should go in your header file since that is what other people will be
reading. You will need to document the file (like normal), the class itself, each function (like normal) and
the variables. An example of a documented class is below.
/** * @class Rectangle * * @brief Stores the information about a rectangle in 3d space. * * Stores a rectangle's (x, y, z) position, its * size, and orientation in space. * */ class Rectangle { private: /** x coordinate in 3D space */ int x; /** y coordinate in 3D space */ int y; /** z coordinate in 3D space */ int z; //... public: /** @brief Default constructor for Rectangle Default constructor for Rectangle that sets the location to (0, 0, 0) with a width and length of 0. */ Rectangle(); /** @brief Gets the x coordinate of the upper left corner of the Rectangle Gets the x coordinate of the upper left corner of the Rectangle @return The x coordinate */ int getX();
CS 144 Computer Science I
/** @brief Updates the Rectangle's location in space Updates the Rectangle's location in space @param nx The new x coordinate @param ny The new y coordinate @param nz The new z coordinate */ void setPosition(int nx, int ny, int nz); //... };
Notices, hints, and suggestions
Commenting and Formatting
• Make sure you format your code before you submit. o Ctrl + K, Ctrl + D will do most of it for you automatically in Visual Studio.
• Ensure that your Doxygen comments are there and that all the required tags are there too. o You need a file comment for each file and a function comment for each main function. o The int return value with the main function indicates an error in the program. 0 is
returned if no error occurred. You can describe that with the @return tag in the Doxygen comment. Take a look at the examples in the notes.
• Everything will be uploaded through Web-CAT.
Using the instructions in the next section, submit your .cpp and .h files to Web-CAT. View the feedback
from Web-CAT and view your score. If you did not receive full points, look at the files that have errors.
Make any corrections you need and resubmit until you get full points.
Submission 1. Go to the Windows Explorer window and find your .cpp files and .h files. Zip them together into
a single file so you can upload all of your work at one time. (The resources on zipping files may
be useful here.)
2. Open Web-CAT in your browser window ( https://web-cat.cs.vt.edu/Web-
CAT/WebObjects/Web-CAT.woa ). Log in.
3. Find the Lab09 assignment and submit your files to it. Wait until the grading is completed and
view the results. Make the corrections needed and resubmit.
Grading Total Points: 100 Points from Web-CAT: 65 Compiles correctly/ Produces correct output 50 Meets style guidelines 15 Points from Instructor: 35 Logic 5
CS 144 Computer Science I
Memory management/ other coding requirements 10 Testing 20