ICS212: Assignments

Assignment 09

Details

A09.pdf   (Due: 8am, 04 May)

Clarifications and Suggestions

Grading (10 points)

FAQs

What is the output supposed to look like for this?
The assignment asks you to create 3 fruit plants. Each time you do this, the fruit plant constructor will create a random number of new fruits, and those new fruits will each print a line from their constructors as they are created.

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)
If Fruitplant is templated, doesn't that mean we should be able to make an instance of it with any type, even ints or strings?
Theoretically, yes. However, if you try it, you'll find that you get a compile error because an int or string doesn't have a print() function, which your Fruitplant should be calling on each of its contents. C++ templates are sort of an example of compile-time duck typing in this way: they don't require you give it a certain type, but the given type must still have all the behaviors required by the template.

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.



Assignment 08

Details

A08.pdf   (Due: 8am, 27 Apr)

Clarifications and Suggestions

Grading (10 points)



Assignment 07

Details

A07.pdf   (Due: 8am, 13 Apr)

Grading (10 points)



Assignment 06

Details

A06.pdf   (Due: 8am, 23 Mar)

Grading (10 points)



Assignment 05

Details

A05.pdf   (Due: 8am, 14 Mar)

Grading (10 points)

Names of typedefs, structs, functions, and modules/files are up to you.

Clarifications and Suggestions

FAQs

How many .h header files should I create?
It's up to you. What do you think would be clearer in the long run? You've got a range of options:

So weigh your options and see what seems like the best choice. (See also this FAQ, and the four links from there.)

I'm getting compiler errors when I #include one header file from another.
Then don't do that. :) If you do, it's very easy to have a header included more than once, so that some struct gets defined twice when you compile.

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.)



Assignment 04

Details

A04.pdf   (Due: 8am, 29 Feb)

Grading (10 points)

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.

Clarifications and Suggestions



Assignment 03

Details

A03.pdf   (Due: 8am, 22 Feb)

Grading (10 points)

Clarifications and Suggestions



Assignment 02

Details

A02.pdf   (Due: 8am, 15 Feb)

Grading (10 points)

Clarifications and Suggestions



Assignment 01

Details

A01.pdf   (Due: 8am, 08 Feb)

Grading (10 points)

Tips, Hints, and FAQs

How to create a ZIP file on uhunix
This is for those of you who work on uhunix or who don't have a zip program on their home machine. Here is an example of how to create a zip file:
zip usernameA01.zip GumBallMachine.h GumBallMachine.c main.c PartyDesign.jpg
zip 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.

I'm trying to compile on uhunix using gcc, but I'm getting a linker error.
You can recognize a linker error because it looks something like this:
  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