Don't like this style? Click here to change it! blue.css
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.
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:
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.
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.
DO NOT USE the std::string
class! Build a class which contains an array of char
s
(private).
Store a long current_size
which keeps track of how large your current array of char
s
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.
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.