Lab 5: Defining and Using Classes in C++ - Money and Exchange, Lab Reports of Computer Science

Instructions for creating a simple money structure and a full-blown exchange class in c++. It covers creating files, defining structures and classes, implementing member functions, and testing the code. The objective is to learn how to define and use classes, instantiate objects, and perform data conversions.

Typology: Lab Reports

Pre 2010

Uploaded on 08/19/2009

koofers-user-xsv-1
koofers-user-xsv-1 🇺🇸

9 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Name:
Sec: 07/11/2006
Lab 5.1 Defining and Using Classes
Recall that the two structured data types we have studied so far are arrays, which have
elements all 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 at the end of the class definition. 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.
Objectives
In this lab, you define a class and declare objects.
After completing this lab, you will be able to:
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.
Instructions
1. We will first create a simple structure called Money. We will be using
multiple files in this lab to build a program. So, you should start off by
creating a project to hold all of your files. Name the project Lab5.
2. Create a new file called Money.h and add it to your Lab5 project. Define a
struct, called Money, in this file. The Money structure should have only 2
member items. One member is a char data type and should be called type.
The other is of type double and should be called amount. Since this is a
structure, both of these members are publically accessible by default. The
type member will hold the type of the monetary value being represented,
for example Mexican Pesos, or Russian Rubbles, or U.S. Dollars, using a
pf3
pf4

Partial preview of the text

Download Lab 5: Defining and Using Classes in C++ - Money and Exchange and more Lab Reports Computer Science in PDF only on Docsity!

Name: Sec: 07/11/

Lab 5.1 Defining and Using Classes

Recall that the two structured data types we have studied so far are arrays, which have elements all 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 at the end of the class definition. 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.

Objectives

In this lab, you define a class and declare objects. After completing this lab, you will be able to:

  • 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.

Instructions

  1. We will first create a simple structure called Money. We will be using multiple files in this lab to build a program. So, you should start off by creating a project to hold all of your files. Name the project Lab5.
  2. Create a new file called Money.h and add it to your Lab5 project. Define a struct, called Money, in this file. The Money structure should have only 2 member items. One member is a char data type and should be called type. The other is of type double and should be called amount. Since this is a structure, both of these members are publically accessible by default. The type member will hold the type of the monetary value being represented, for example Mexican Pesos, or Russian Rubbles, or U.S. Dollars, using a

single character to represent the monatary type. The amount will be, as suggested by the name, the amount of Pesos, Rubles, Dollars, etc respectively being saved in the structure.

  1. Create a new source file and name it TestExchange.cpp. Add a single main() function to this file. Include your Money.h header file by doing a: #include “Money.h”. You need to use the parentheses (instead of the <>) for a header file that you define. Test your Money class by creating a few variables of type Money. For example, do the following, and make sure your TextExchange.cpp and Money.h files compile correctly: Money dollars; Money pesos; Money francs; dollars.type='d'; dollars.amount=55.23; pesos.type='p'; pesos.amount=1000.00; francs.type='f', francs.amount=15.35;
  2. Now we will implement a full blown class, that has both data members as well as function members. Create a new file called Exchange.h and add it to your project. Inside of this file we will create a class called Exchange. The purpose of this class it to calculate an exchange of Money from one type of currency into U.S. dollars, for example from Pesos to U.S. dollars.
  3. Add 2 private data members to your Exchange class. Both of these members will be of type Money (defined in step 3), so you will need to #include “Money.h” at the top of your “Exchange.h” header file. Make sure the members are private by using the private: keyword before the definition of the member items.
  4. We will add 3 public function members to the Exchange class next. One function should be called getData() and it should return a bool as its result. Another function should be called displayExchange(), and it returns no result so it is a void function. The third function is called convert() , and it is also a void function that takes no parameters as input.
  5. At this point you have defined a class called Exchange in the Exchange.h header file. We now need to implement the 3 member functions that you declared as being members of the Exchange class. We will code the implementations of these member functions in a file called Exchange.cpp. Create this file and add it to your project. At the top of the file you will
  1. As a last step, use this main loop to test your Exchange and Money classes/structures: int main() { Exchange e; while (e.getData()) { e.convert(); e.display(); } } This will keep asking the user for a monetary amount and currency type, then converting it and displaying the conversion. Note that you need to return false from your getData() member function if the user enters 'q' so that the program knows that the user is done entering monetary amounts.

Lab 5 Finished

You have now completed Lab 5. If your program compiles and runs correctly and you have successfully uploaded your source file to the eCollege online submission site, then you are done.