The purpose of this lab is to review C++ classes and get
some practice with object oriented design and incremental
program development. As you follow the steps below, you will
create a program to store and manipulate information describing
a music CD. We will not put iTunes out of business, but
hopefully you will have some fun.
- Design the Music Object
Your first task is to design the Music object we will be
implementing today. When someone asks you to "tell me everything
about your favorite CD" you will probably start with the
artist name and the album name. Then you will probably look
at the back of the CD and start reading off other interesting
Your first step in designing a Music object is to think
of meaningful names for all of these attributes, and decide
what data types would be appropriate to store these values.
For example, a CAR object could have a Model attribute of
type string, a Year attribute of type int,
and a Mileage attribute of type float.
Think up FIVE Music attributes and decide what C++ variables
are appropriate for storing each attribute. Too keep things
simple for now, use just the basic types (int, float, string)
and fixed size arrays of basic types. Type in your C++
declarations for your Music attributes in the space below:
- Create NetBeans Project
Go through the following steps to create a NetBeans project and
to create an empty main program.
- Open the NetBeans application.
- Next, create a NetBeans project and an empty C++ program.
- In the "File" menu, choose "New Project".
- In Categories choose "C/C++", in Projects choose "C/C++ Application".
- Click "Next".
- Enter project name as "lab2".
- Enter project location as "your-home-directory/csce2014".
- The project folder will be automatically written as
- Check option "Create Main File".
- Click "Finish".
- The IDE will now create a project called "lab2" in the left panel.
- Inside the "Source Files" section there is a file called "main.cpp".
- Edit "main.cpp" to print out your name and UAID number.
- To compile and run this program click the green arrow button on the top bar.
- When you have this working, you are ready for the next step.
- Create Music Class
Go through the following steps to create a C++ class called "Music"
inside the NetBeans project.
- In the tab "Projects" in the left window, expand the "Source Files" section.
- Right click on "Source Files," hover over "New," and click "C++ Class."
- Type in "Music" for the class name, and click on "Finish".
- You should now see "Music.cpp" and "Music.h" in the "Source Files" section.
- Compile and run your program. It should still output your name and UAID number.
- Declaring Attributes and Implementing Constructors
Now we can start adding information to the "Music.h" and "Music.cpp" files
to declare private variables and to declare and implement constructor function.
- First, copy/paste the C++ declarations for your Music attributes above into
the private section of the Music class in "Music.h".
- Then, edit "Music.h" and add the following statements above the class definition.
using namespace std;
- Next, edit "Music.cpp" and add the following statements to the appropriate methods.
cout << "In constructor\n";
cout << "In copy constructor\n";
cout << "In destructor\n";
- Next, add several lines of code to the constructor function to initialize your
Music attributes to their default value (zeros, empty string, etc).
- Next, add code to implement the copy constructor, copying Music attributes
from the input parameter into the private variables.
- Finally, edit "main.cpp" to include "Music.h" and declare two objects of the Music class.
- Compile and run the program.
- You should get some output showing how the Music methods are called.
Copy and paste the contents of your "Music.h" file below:
Copy and paste the contents of your "Music.cpp" file below:
- Declaring Methods
So far we have ignored the operations that
"do something" to our Music object. Frankly, there
is not much you can "do" to objects this simple
without a copy of the music or artwork. Since we
are not going to re-implement iTunes in two hours,
we will limit ourselves to operations that input/output
the attributes, and print the information we have stored.
Typically, direct access from outside to variables of a class is not
recommended. The variables are usually made private, and read/write access
is done through the getVar and setVar methods. For example, with the CAR object
above, the methods to read/write the Model attribute would be getModel/setModel.
Add public methods to "get" and "set" each of your Music
attributes. Start by typing THREE method prototypes into your class
definition in "Music.h". Then, add calls to these methods in "main.cpp".
When you compile the program, you will probably get some error
message. Cut and paste these error messages below.
- Implementing Methods
The reason you got error messages was because the methods
were not implemented. Your next task is to create EMPTY methods
for each of your new methods in the "Music.cpp" file. Be careful
to use the same parameters and return type as the method definition
in "Music.h". Every public method has the following syntax:
RETURN_TYPE CLASS_NAME::CLASS_METHOD( PARAMETER_DECLARATIONS )
cout << "In CLASS_METHOD method\n";
// PUT REST OF IMPLEMENTATION HERE
After you have implemented the THREE methods you created in
"Music.h", add calls to these methods in "main.cpp" and compile
and run your program. Cut and paste your program output below:
- Adding More Methods
Continue the process of adding methods to the class
declaration "Music.h", the class implementation "Music.cpp",
and add calls to these methods in the main program until you
have all FIVE "get" and "set" methods completed.
Next, add TWO different print methods to the Music class,
one that prints all of the music attributes, and another
that prints an interesting subset of the music attributes.
Cut and paste your new and improved "Music.h" below:
- Completing and Testing Methods
The next step is to complete the implementations for
ALL of your methods above, one method at a time, and test
the program as you go. Each time you run your program
it should print out more information as more of the
methods do actual work. Cut and paste your final version
of "Music.cpp" below:
Cut and paste your final version of "main.cpp" below:
Cut and paste your final program output below:
- Submit Work
When you have completed all of the steps to this lab: