Lab 4.1: Defining and Using Classes with UML Notation and Exchange Class Implementation, Lab Reports of Computer Science

A lab exercise for defining and using classes in c++ with unified modeling language (uml) notation. The exercise involves creating a uml diagram and implementing a class named exchange with private and public members, member functions for user input, validation, conversion, and display of different currencies. The document also includes instructions for creating a driver program to test the exchange class.

Typology: Lab Reports

Pre 2010

Uploaded on 08/18/2009

koofers-user-37b
koofers-user-37b 🇺🇸

9 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Name:
Sec: 11/01/2005
Lab 4.1 Defining and Using Classes
Recall that two structured data types are arrays, which have
elements of the same type, and structures, which have elements of
differing types. Another structured data type is a class, which is
specifically designed to combine data and functions in a single unit. A
class is a collection of a fixed number of components. The
components of a class are called the members of the class. If a
member of a class is a variable, you define it just like any other
variable, but you cannot initialize it at definition. If a member of a
class is a function, you typically use the function prototype to define
that member. Member functions can directly access any data
member of the class without passing that data member as an
argument.
No memory is allocated in a class definition. Instead, memory is
allocated when the class is instantiated (an object is created).
Additionally, the semicolon (;) is part of the syntax. The members of a
class are classified into three categories: private, public, and
protected. By default, all members of a class are private. Private
members cannot be accessed outside of the class. A public member
is accessible outside the class. Protected members will be discussed
in Chapter 13.
A class and its members can be described graphically using Unified
Modeling Language (UML) notation. A UML diagram contains three
boxes stacked vertically. The top box contains the name of the class;
the middle box contains the data members and their data types; and
the bottom box contains the member method names, parameter list,
and return types. The + (plus) sign indicates that it is a public
member; the - (minus) sign indicates that it is a private member. The
# (pound) symbol indicates that it is a protected member.
Objectives
In this lab, you define a class and declare objects.
After completing this lab, you will be able to:
Use basic UML notation to design and specify a class.
Write a driver program that contains a class definition.
Instantiate an object of a class.
Practice using coding projects and working with a program
defined across multiple files.
Describing Classes Using the Unified Modeling Language (UML)
pf3
pf4
pf5

Partial preview of the text

Download Lab 4.1: Defining and Using Classes with UML Notation and Exchange Class Implementation and more Lab Reports Computer Science in PDF only on Docsity!

Name:

Sec: 11/01/

Lab 4.1 Defining and Using Classes

Recall that two structured data types are arrays, which have elements of the same type, and structures, which have elements of differing types. Another structured data type is a class, which is specifically designed to combine data and functions in a single unit. A class is a collection of a fixed number of components. The components of a class are called the members of the class. If a member of a class is a variable, you define it just like any other variable, but you cannot initialize it at definition. If a member of a class is a function, you typically use the function prototype to define that member. Member functions can directly access any data member of the class without passing that data member as an argument. No memory is allocated in a class definition. Instead, memory is allocated when the class is instantiated (an object is created). Additionally, the semicolon (;) is part of the syntax. The members of a class are classified into three categories: private, public, and protected. By default, all members of a class are private. Private members cannot be accessed outside of the class. A public member is accessible outside the class. Protected members will be discussed in Chapter 13. A class and its members can be described graphically using Unified Modeling Language (UML) notation. A UML diagram contains three boxes stacked vertically. The top box contains the name of the class; the middle box contains the data members and their data types; and the bottom box contains the member method names, parameter list, and return types. The + (plus) sign indicates that it is a public member; the - (minus) sign indicates that it is a private member. The

(pound) symbol indicates that it is a protected member.

Objectives

In this lab, you define a class and declare objects. After completing this lab, you will be able to: Use basic UML notation to design and specify a class. Write a driver program that contains a class definition. Instantiate an object of a class. Practice using coding projects and working with a program defined across multiple files.

Describing Classes Using the Unified Modeling Language (UML)

Notation and Declaring Objects 1a. Create a UML diagram for a struct named Money that will contain data members. Your struct (or class) should contain a char data member named type and a double data member named amount as public members. (See Ch #12, pg. 599 for a quick discussion and example of UML diagrams). 1b. Implement the struct in your diagram in C++ and save your code in a file named Money.h. 2a. Create a UML diagram for a class named Exchange that will contain data members and member functions to allow a user to input, validate, and convert pesos, Euro dollars, and Swiss francs to U. S. dollars. Your class should meet the criteria in the following list:_ The class has 2 data members, US and entered , which will be of the type Money that you defined in Exercise 7, and should be private members. The Boolean function getData and the void functions convert and displayExchange have no formal parameters and should be public members. The void function outputType has no formal parameters and is a private member.

object and uses the getData() member. The loop should continue until the returned value from getData() is false. After calling getData(), within the loop you should then call the member functions convert() and displayExchange(). 3b. Enter, compile, link, and execute TestExchange.cpp. The following is a copy of the screen results that might appear after running your program, depending on the data entered. The input entered by the user is shown in bold. This program prompts the user to enter a character to designate the type of currency to be exchanged and the amount of money to be exchanged. Enter a character for currency type and an amount. (P)esos, Swiss (f)rancs, (E)uro dollars or (q)uit: x 123. You entered an invalid type, please re-enter. p 123. You entered 123.54 in Pesos, which is $12.59 in U.S. dollars. Enter a character for currency type and an amount. (P)esos, Swiss (f)francs, (E)uro dollars or (q)uit: f 500 You entered 500.00 in Swiss francs, which is $355.77 in U.S. dollars. Enter a character for currency type and an amount. (P)esos, Swiss (f)francs, (E)uro dollars or (q)uit: e 987 You entered 987.00 in Euro dollars, which is $1033.18 in U.S. dollars. Enter a character for currency type and an amount. (P)esos, Swiss (f)francs, (E)uro dollars or (q)uit: q

Lab 4 Finished

You have now completed Lab 4. Turn in the previous pages to the instructor and make sure you have uploaded your TestExchange.cpp, Exchange.h, Exchange.cpp and Money.h in a new Lab4 folder in your online student account. Attached at the end of this lab is a description of your third programming assignment, which will be due next Friday Oct 21. After completing this lab, now is a good time to begin thinking about how you will implement the second programming assignment.