Don't like this style? Click here to change it! blue.css

Preamble: Bit quiz

Some loose ends

This is the stage where, while we've had fun in lecture, the work begins to get harder. If you have not been doing a good job at 10 hours a week at home then it is time to buckle down.

If you have not started your first project, do so, immediately. If you have not found a team, we can spend some time now coordinating that.

malloc/free vs new/delete with arrays

Hopefully, you are now confident asking for chunks of memory. You can do it with malloc/free or new/delete and you know how they are different (constructors/destructors).

Here is a snippet which should hammer this home:

Overloading methods/functions

You can have many functions with the same name as long as their inputs and/or outputs are different. C++ will figure out which version you want.

Class 6: Putting together a useful class

I would like you to work in groups of 2-4ish today. We're going to build our own "string" class. Take a look at the specs, discuss with your group how you might break up the tasks, and see if you can knock it out during class.

This mini-project is a starter for doing dynamic arrays. Which, at this stage, are containers that scale when you need more space by allocating more memory and copying the contents.

MyString base specs:

DO NOT USE the std::string class! Build a class which contains an array of chars (private).

Store a long current_size which keeps track of how large your current array of chars is (private).

Provide a public method concat which consumes a const char * (normal input text, e.g. "hi there") and extends your string.

If you run out of space (check for this when appending) reallocate twice as much space and copy your characters over. (Free the old space after copying if that applies.)

If the size of your string gets too large consider using the heap.

Extra Credit:

Overload << so you can cout the contents.

Make a constructor which takes in a const char*.

Make a destructor which frees and memory you've allocated.

Make a public "clear" method which gives back space and empties the characters.

Helpful Samples

More details here.