SWEN-250

Basic C++ Classes

Overview

This exercise requires you to write C++ code that will provide practice in basic class implementation and inheritance

In this exercise you will use a base class, and add new, derived classes

  • You will use a C++ makefile (starter file provided) to build the executable
  • All output will be to the console
Requirements

  • Review the base class (Animal.cpp)
  • Note that the 'speak' method outputs a generic message to the console, based on parameters provided in the constructor
  • Note that the 'trick' method also outputs a generic message to the console, but there are no parameters used
  • Create a set of new classes, derived from Animal as follows: (Each class should be separate .h and .cpp file, add any #include statement in main.cpp as needed)
    • Dog: The speak method should output 'woof' [The class name must be Dog]
    • Cat: The speak method should output 'meow' [The class name must be Cat]
    • Parrot: The speak method should output 'Polly wants a cracker' [The class name must be Parrot]
    • Giraffe: The speak method should output -- well, look it up :) [The class name must be Giraffe]
    • For each class you add, implement an appropriate trick() method in the new class by overriding the base class method
  • Update the Makefile to add your new files
  • Uncomment the #define statements in main.cpp as you implement each class to enable the test functions; add any #include statements as needed
  • As you implement code in main.cpp, replace any params "??" with something more appropriate!
  • NOTE: We highly recommend doing one class at a time i.e. implement, test one class, then move on to the next one
  • Look at the output from each method call for each Animal (derived) class.
      When is the base class method executed?
      When is the child class method executed?

Instructions
  1. Download the zipfile:
  2. cpp_classes.zip
  3. Unzip the file into your repo, so that the folder name is `cpp_classes`
  4. Build the application `test` using the make file
  5. Test the code by typing ./test . This will run the `test` executable from the command line.
  6. Check the results and the testing source code in main.cpp to understand how it works
    • Pay attention to the commented-out code, since you will need to uncomment those lines as you implement
  7. Implement the code for the classes and functions described in the requirements
    • Uncomment the tests in main.cpp as you make progress

Example output is shown below for the tests in main.cpp

The animal blank slate says silence is golden
Teach me a trick
The animal Rover says Woof
I can roll over
The animal Old Yeller says Bark and Bite
Teach me a trick
Teach me a trick

The animal Selina says Rowr
I play with bats!

The animal Polly says Wanna cracker -- now!
My best friend is Captain Jack Sparrow

The animal Geoff says ToysRMe!
I sold my soul to commercialism :(
Submission
  • Submit your code to a directory named cpp_classes in your git repository.
  • Grading scheme: 10 points

    • Clean build (2)
    • Each new class per requirements AND all test code runs correctly (2) x 4 = 8