View the step-by-step solution to:


This exercise will give you practice in using classes that have a relationship we call aggregation and composition

- aka the "has-a" relationship. In the diagram below, the dark diamond shape indicates composition and the clear diamond shape indicates aggregation. The difference is that with composition, the object contained in the other object disappears when the containing object disappears, for example, if the Bank is closed down, the lobby is gone. But with aggregation, the contained object can exist by itself, for example, an ATM can be in the lobby but it can exist independently of the lobby as well.

We will have three classes: A Bank has-a Lobby; the  Lobby has-a ATM:

Here are the UML class diagrams for your three classes.


-manufacturer : string

-model : string


+ATM(string, string)                                           

+setManufacturer(string) : void

+getManufacturer() : string

+setModel(string) : void

+getModel() : string

+displayATM() : void                                            


-length : double  

-width : double             

-height : double            

-atm1 : Copier


+Lobby(double, double, double, ATM)                  

+setLength(double) : void

+getLength() : double

+setWidth(double) : void

+getWidth() : double

+setHeight(double) : void

+getHeight() : double

+setATM(ATM) : void

+getATM() : ATM

+displayLobby() : void                                     


-name : string

-address : string                                     

-lobby : Lobby


+Bank(string, string, Lobby)

+setName(string) : void

+getName() : string

+setAddress(string) : void

+getAddress() : string

+setLobby(Lobby) : void

+getLobby () : Lobby

+displayBank() : void                              

STEP 1 - Create New Project

Name your project: CIS247_Exercise4_YourLastName. For this exercise, it is required to use separate header files for each of the three classes and all member function code placed in separate code files. Thus there will be seven files making up this project, including the one with the main function.

The easiest way to create the header and class code files is to use the Add Class selection from the Project menu. Enter the following names for your three classes: ATM, Lobby, Bank.

Add a seventh file to the Source Files folder for the main function - name this file as you wish.

STEP 2 - Documentation

Add this documentation to the source file for the main function:


Program: Using Aggregation/Composition

Programmer: Your Name


Purpose: Demonstrate classes related by composition and aggregation; We will code a Bank class that contains an object of the Lobby class which contains an object of the ATM class.


Step 3 - Coding the ATM Class

In the ATM.h file, place the class definition except for any member function code; put only the private data members and the public member function prototypes. If not already there, add this statement to the top of your header file:

              #pragma once

This prevents redefinition errors. Include the <iostream> and <string> libraries.

In the ATM.cpp file, add a #include for the "ATM.h" file (unless it is already added). Then add code for the two constructors, the setters and getters, and the displayATM() function. That function should display something like:

 "ATM Information: <insert manufacturer> <insert model>"

Remember you need to include the class name and scope resolution operator in front of all member function names. Example:

              void ATM::setManufacturer(string manu) {manufacturer = manu;}

Step 4 - Coding the Lobby Class

Use the UML diagram to add the class definition for the Lobby class to Lobby.h. Be sure the #pragma once statement is at the top of your header file. Also add a #include for the ATM.h file:

#include "ATM.h"

You would think we need to include iostream and string libraries, but when we include ATM.h, we automatically get those. As we did for the ATM class, include the private data members and the member function prototypes (no function code.) The Lobby class has 4 private data members, one of which is an object named, atm1, of datatype "ATM."

In the Lobby.cpp file, be sure to #include the "Lobby.h" file which will pass along the other libraries we need. Add code for the two constructors, for the setters and getters and for the displayLobby() function. The displayLobby() function should print something like this:

"The lobby is ____ cubic feet.

It has an ATM: ATM Information: <manufacturer> <model>

First of all, we need to calculate the cubic feet by multiplying the length times the width times the height of the lobby. Notice that the last part, ATM Information etc., can be printed by the ATM class. So we want to call:


We could just recode this in displayLobby(), but we want to make use of OOP. In this way, if somebody changes the display in the ATM class to make it better, we automatically take advantage of that in our Lobby class by using the ATM display function. Does that make sense? We want to avoid redundant code that can get out of sync over time.

Step 5 - Coding the Bank Class

By now, you should see the pattern of what to do next. We want to put the Bank definition in the Bank.h file with only private data members and public function prototypes. Remember the #pragma once at the top. Now notice that the Bank has an object of type Lobby. We know that an office has a ATM. Do we need to include "ATM.h" here?

The answer is "no". If we put:

              #include "Lobby.h"

then we automatically get ATM.h and iostream and string libraries. We could repeat them but we don't need to. Avoid code that isn't needed!

As before, in the Bank.cpp file, put the code for all the member functions with the class name and scope resolution operator in front of the function names.

The displayBank() function should print something like this:

"This is information on <insert name> <insert address>.

The lobby is ____ cubic feet.

It has an ATM: ATM Information: <manufacturer> <model>"

So our function should print the first two lines. Is there an easy way to print the last two lines? What function can we call to do that for us? What object name should be used? See if you can figure this part out on your own.

Step 6 - The Main Function

If your classes are coded correctly, this main function will be short and sweet. It can be done with four lines of code, if you use the constructors that accept parameters.

1.     Declare an ATM object with a name of your own choice and pass in a manufacturer and type of your choice (these are text strings so use double quotes).

2.     Declare a Lobby object with a name of your own choice, passing numbers for the length, width and height (feet is assumed) and pass the name of the ATM object you created in step 1.

3.     Declare a Bank object with a name of your own choice. Pass text strings containing the name and address and pass the name of the lobby object from step 2. Remember: the lobby already contains the ATM!

4.     Now just call displayBank() with the name of your Bank object from step 3. That should display all information!

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes