A09.pdf (Due: 8am, 04 May)
cin.get()
(or, if you must, getchar()
) trick to hold the output window open, main has not ended yet. To force your destructors to run where you can see them, you can wrap the contents of main before the call to cin.get()
in an anonymous {block}
. This way, your destructors will get called when execution leaves that block but before reaching cin.get()
. See the lecture slides for examples of this.
Once you're created the the fruit plants, you can call print on each of them. Each plant will then call print on all of their contained fruit.
Finally, you'll see everything getting destroyed.
Adding some print statement to the fruit plant constructor, print function, and destructor is optional. I did that, as well as printing a blank line at the end of each. So, with no printing done in main, my output looks like this:
new FruitPlant (size: 4) new Apple(112 grams) new Apple(147 grams) new Apple(167 grams) new Apple(102 grams) new FruitPlant (size: 5) new Banana(63 grams) new Banana(103 grams) new Banana(131 grams) new Banana(63 grams) new Banana(61 grams) new FruitPlant (size: 3) new Cherry(4 grams) new Cherry(5 grams) new Cherry(4 grams) printing FruitPlant (size: 4) an Apple(112 grams) an Apple(147 grams) an Apple(167 grams) an Apple(102 grams) printing FruitPlant (size: 5) a Banana(63 grams) a Banana(103 grams) a Banana(131 grams) a Banana(63 grams) a Banana(61 grams) printing FruitPlant (size: 3) a Cherry(4 grams) a Cherry(5 grams) a Cherry(4 grams) destroying FruitPlant (size: 3) destroy Cherry(4 grams) destroy Cherry(5 grams) destroy Cherry(4 grams) destroying FruitPlant (size: 5) destroy Banana(61 grams) destroy Banana(63 grams) destroy Banana(131 grams) destroy Banana(103 grams) destroy Banana(63 grams) destroying FruitPlant (size: 4) destroy Apple(102 grams) destroy Apple(167 grams) destroy Apple(147 grams) destroy Apple(112 grams)
Fruitplant
is templated, doesn't that mean we should be able to make an instance of it with any type, even int
s or string
s?
For this assignment, this is fine. You can assume that whatever goes into your Fruitplant will have the necessary print() behaviors of a fruit.
However, for the curious, there is a way to make your template more robust: The standard way to print in C++ is to use the << operator. You can override this behavior for each of your fruits. Most C++ classes do this. It's a lot like overriding toString()
in Java.
The first step is to change your fruit's print() method to take the stream to print to as a parameter:
void print(std::ostream& out = std::cout);
In the definition of this new print function, write to out
rather than to std::cout
. Note that I'm using a default argument here. This way, you can still simply call print() to print to the screen, but you could also pass a file stream instead to print to a file if you wanted to.
Then declare the following function, renaming Apple to your particular fruit:
std::ostream& operator<<(std::ostream& out, const Apple& f);
The definition of this function:
std::ostream& operator<<(std::ostream& out, const Apple& f) { f.print(out); return out; }
Finally, use cout << fruits[i];
instead of fruits[i].print();
in your template class. Now your template class will work with anything that can be printed using the << operator, and your fruits will also work with the << operation in other situations.
Again, this is all optional.
A08.pdf (Due: 8am, 27 Apr)
Tree
typedef. You can just change this back to char
so that you'll have char*
instead of Tree*
).
A07.pdf (Due: 8am, 13 Apr)
A06.pdf (Due: 8am, 23 Mar)
A05.pdf (Due: 8am, 14 Mar)
Names of typedefs, structs, functions, and modules/files are up to you.
So weigh your options and see what seems like the best choice. (See also this FAQ, and the four links from there.)
Many people argue you should never include one header file from another. #includes should only go in a .c file. Remember that all the code in a .h file will be copied into a .c file by the preprocessor before the .c file gets compiled. If you order your #includes in the .c file correctly, this will work out fine, even if code in one .h requires a definition from another .h.
Other people use a neat preprocessor trick called an include guard. These 3 lines go in the .h file around its contents and prevents it from being copied twice into the same .c file.
See here for more info on all of this. (I'll talk more about this in lab.)
A04.pdf (Due: 8am, 29 Feb)
Each item listed here is worth 1 point. However, failures on some items (particularly those marked with a [*]) may cost you more points on other dependent items. For example, you will likely also lose points on gameplay if you have no print function.
A03.pdf (Due: 8am, 22 Feb)
getchar()
for this (though that will return their choice as a digit/character) or else try scanf()
.
PrintGumBallMachineStatus
function lying around. If you dust this off a bit, it can still be useful!
A02.pdf (Due: 8am, 15 Feb)
main
so that your program prints only A02 content. (You will still be extending A01 because your GumBallMachine.c file will still contain the two functions you wrote in A01. Although you won't use them in your finished A02, the functions may come in handy when testing your A02 program.)
OperateGumballMachine()
are not explictly stated. I recommend that it take no parameters (but you can vary this if you really want to).
OperateGumballMachine()
only needs to sell the user one gumball and then end. (So 'x' is just so they can quit early without putting in enough to buy a gumball.) However, your functions should still work correctly if main is later changed to keep using the gumball machine until it's empty.
static
. But if you make it static, how can you initialize it from main? (Hint: Use a function... preferably one of the functions that you already have to write anyway...)
switch
in DeliverGumBall() if a different control structure makes more sense for your algorithm--for example, if you're picking a random gumball across all colors rather than picking a random color.
A01.pdf (Due: 8am, 08 Feb)
zip usernameA01.zip GumBallMachine.h GumBallMachine.c main.c PartyDesign.jpgzip is the name of the program. The first argument is the name of the zip file you want to create or add to; the other arguments are a list of files to add to the zip. All the files you are trying to zip need to be in your current uhunix working directory. (That is, type
ls
first to make sure they are all there.)
You don't have to create your zip file on uhunix if you can create one on your home machine. Note that Tamarin does require a .ZIP file though--no RARs, GZs, TARs, etc.
Undefined first referenced symbol in file main /usr/local/lib/gcc/sparc-sun-solaris2.10/3.4.6/crt1.o ld: fatal: Symbol referencing errors. No output written to a.out collect2: ld returned 1 exit status
You can recognize a linker error because 1) it's complaining about a .o file and 2) it doesn't give you a code line number.
Here, its complaining that the symbol main
is not defined. That means it can't find the definition of your main function. (Your error message might be about a different function name.)
Generally, the reason the linker can't find a function definition is because you forgot to include the appropriate .c file when you compiled. You would get the error above if you forgot to mention your main.c file when compiling. Remember that you have to specify all the .c files you are compiling to produce the single executable. (You don't need to mention any .h files when compiling because those should already be #included from the .c files.)
So your compile command should look something like this:
gcc main.c GumBallMachine.c
Or, if you want to specify the name of the output file (rather than the a.out default):
gcc -o gum.exe main.c GumBallMachine.c