UMBC CMSC 202 Computer Science II

Lab4: C++ Aggregation



Source: Flickr

Objective

In this lab you will learn how to aggregate classes. Aggregation is when one class has objects of other class(es) as data members.

Assignment: DogSledTeam

Your assignment is to write a DogSledTeam class. The new class will use the Dog class from Lab 3, as well as the C++ string class.

Step 1: Define the DogSledTeam Class

  1. Create a file "DogSledTeam.h" for the class definition of DogSledTeam. Remember to use "#ifndef", "#define" and "#endif" to guard the header file. You will need to #include the string class and the Dog class.

  2. We will only consider dog sled teams with 6 dogs: 2 leaders, 2 swingers and 2 wheelers. We'll have a leader, a swinger and a wheeler on the left side of the "gangline" and the same on the right side.

    (See the Wikipedia entry on "Mushing" for further information on dog sledding.)

    Decide on names for the 6 data members of type Dog for these 6 positions. (See the Dog class defined by Dog.h and implemented in Dog.cpp)

  3. Should these Dog data members be public or private?

  4. Define a constructor for DogSledTeam: DogSledTeam(const string& teamName, const Dog& leader1, const Dog& leader2, const Dog& swinger1, const Dog& swinger2, const Dog& wheeler1, const Dog& wheeler2) ; Note that the parameters are passed as const references. What does this mean?

  5. Should DogSledTeam have a default constructor?

  6. The team must have a name. Define a data member for the team's name. Once a dog joins the team, its owner should be set to the team name.

  7. Define an accessor and a mutator for the team name: GetTeamName() and SetTeamName().

  8. Define a Print() method for the team, that prints out the team name and the position, name, current age and owner of each dog on the team. (Each dog's owner should be just the team's name, but we want to make sure that this is set correctly.)

Step 2: Implement the DogSledTeam Class

  1. Create a file DogSledTeam.cpp for the implementation of the DogSledTeam class you defined.

  2. Remember to #include DogSledTeam.h and iostream, and of course you'll need to use the 'std' namespace.

  3. Every function of DogSledTeam must be implemented here. Look in Dog.cpp for examples of the syntax for implementing constructors and member functions. You will need DogSledTeam:: before the name of each member function implemented.

Step 3: Use the DogSledTeam Class

  1. Compile your implementation with the main program provided in SledTest.cpp. A makefile has also been provided: Makefile.

  2. Debug.

  3. The output should look something like: Team Name: The Six Stooges Left Leader: Larry 11 The Six Stooges Right Leader: Groucho 10 The Six Stooges Left Swinger: Moe 9 The Six Stooges Right Swinger: Chico 12 The Six Stooges Left Wheeler: Curly 7 The Six Stooges Right Wheeler: Harpo 11 The Six Stooges Changing team name from The Six Stooges to The Marx Mushers Team Name: The Marx Mushers Left Leader: Larry 11 The Marx Mushers Right Leader: Groucho 10 The Marx Mushers Left Swinger: Moe 9 The Marx Mushers Right Swinger: Chico 12 The Marx Mushers Left Wheeler: Curly 7 The Marx Mushers Right Wheeler: Harpo 11 The Marx Mushers

Step 4: Extra Credit (3 points)

For extra credit, implement some additional methods for the DogSledTeam class.

  1. Implement a TeamAbuse() method and a TeamPat() method that abuses or pats each dog in the team. Make up a pattern so dogs in some positions are abused or patted more than others.

  2. Implement a GetTeamHappiness() method that checks if the majority of the team is "happy", "sad" or "ok".

  3. Implement a main function that defines two teams. Use TeamAbuse() and TeamPat() on the teams a few times. Report the "happier" team as the winning team.