Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Laboratory 11 Report - Inheritance and Composition | CPSC 220, Lab Reports of Computer Science

Material Type: Lab; Class: Computer Science I; Subject: Computer Science; University: University of Mary Washington; Term: Unknown 1989;

Typology: Lab Reports

Pre 2010

Uploaded on 08/18/2009

koofers-user-x8k
koofers-user-x8k 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lab 11
Inheritance and Composition
In today’s lab, you’ll explore relationships that can exist between related classes.
The directory of files you’ll need for today’s lab is ~ernie/220/lab11
Inheritance
One benefit of object-oriented programming is the ease with which program components can be
modified. Code reuse is the ability to use pieces of code (as they are or adapted slightly) in a
new context (either a new program or in a new way in an existing program).
Suppose that you have already written a class that encapsulates the data and operations
associated with a person. The class contains information about the person’s first name, last name
and social security number. The operations associated with the person allow for the data
members of the class to be given values and for the user to access these values. This class is
called PersonType and is stored in the files person.h and person.cpp. You may find it useful to
refer to these files as you read through the discussion below.
Now suppose that a new software development request is made (by your professor or work
supervisor). The new request is for a piece of software that stores information about students at a
college. The program is going to store information about each student on campus: the person’s
first name, last name, social security number, current number of credits completed, and current
GPA. You know that your company has already written and tested the PersonType class that
stores most of this information. Now you want to be able to enhance the PersonType class to
create your new required data type: StudentType that will store information about one student.
C++ allows you to modify the existing class through inheritance. By using inheritance, one
class acquires the properties of another class. The original class (the class that you are
enhancing) is called the base class. The new class that you are creating is called the derived
class. The derived class inherits all of the data and functions from the base class. This means
that an object of the derived class will contain space to store each data member of the base class.
An object of the derived class can also call any function of the base class.
Question 1: In the example above, which class will be the base class? Which class will be the
derived class?
In addition to using information/operations associated with the base class, the derived class can
also contain new data members and new functions that were NOT associated with the base class.
This allows us to specialize the general base class to apply to our particular situation.
Question 2: In the example scenario above (using StudentType and PersonType), what new
data members should be included in the StudentType class that were not included in the
PersonType class?
Question 3: Draw a diagram (circle containing data members/functions as done in the book Pg
752) showing what data will be associated with each class.
Because you will be extending the base class to contain new data members, you must also
provide new operations in your derived class so that the user will be able to set the value of the
1
pf3

Partial preview of the text

Download Laboratory 11 Report - Inheritance and Composition | CPSC 220 and more Lab Reports Computer Science in PDF only on Docsity!

Lab 11 Inheritance and Composition In today’s lab, you’ll explore relationships that can exist between related classes. The directory of files you’ll need for today’s lab is ~ernie/220/lab Inheritance One benefit of object-oriented programming is the ease with which program components can be modified. Code reuse is the ability to use pieces of code (as they are or adapted slightly) in a new context (either a new program or in a new way in an existing program). Suppose that you have already written a class that encapsulates the data and operations associated with a person. The class contains information about the person’s first name, last name and social security number. The operations associated with the person allow for the data members of the class to be given values and for the user to access these values. This class is called PersonType and is stored in the files person.h and person.cpp. You may find it useful to refer to these files as you read through the discussion below. Now suppose that a new software development request is made (by your professor or work supervisor). The new request is for a piece of software that stores information about students at a college. The program is going to store information about each student on campus: the person’s first name, last name, social security number, current number of credits completed, and current GPA. You know that your company has already written and tested the PersonType class that stores most of this information. Now you want to be able to enhance the PersonType class to create your new required data type: StudentType that will store information about one student. C++ allows you to modify the existing class through inheritance. By using inheritance, one class acquires the properties of another class. The original class (the class that you are enhancing) is called the base class. The new class that you are creating is called the derived class. The derived class inherits all of the data and functions from the base class. This means that an object of the derived class will contain space to store each data member of the base class. An object of the derived class can also call any function of the base class. Question 1: In the example above, which class will be the base class? Which class will be the derived class? In addition to using information/operations associated with the base class, the derived class can also contain new data members and new functions that were NOT associated with the base class. This allows us to specialize the general base class to apply to our particular situation. Question 2 : In the example scenario above (using StudentType and PersonType ), what new data members should be included in the StudentType class that were not included in the PersonType class? Question 3: Draw a diagram (circle containing data members/functions as done in the book Pg

  1. showing what data will be associated with each class. Because you will be extending the base class to contain new data members, you must also provide new operations in your derived class so that the user will be able to set the value of the

new data members and will be able to request that the value of each new data member be returned. Logical choices for function names would be setCredits( ), setGPA( ), getCredits( ) and getGPA( ). In addition to creating new functions, object-oriented programming allows us to modify a base class through polymorphism. Using function overloading, you can provide an implementation for a function that applies to the derived class. When the function is called with an object of the derived class, the new version of the function will be executed. When the function is called with an object of the base class, the version of the function associated with the original class will be executed. For example, in the PersonType class there is a function called print( ) which prints out the value of each data member associated with the PersonType class (the first name, last name and social security number). In the StudentType class that you derive from the PersonType class, you may also want to include a function to print the values of the data members (first name, last name, social security number, credits earned and GPA). You can re-use the function name print( ) which will make it easy for the user to know what your function is used for. The compiler will automatically pick the correct implementation of the function to use at the time at which the program is executed. This is called dynamic binding. If you have not already done so, open the files person.h and person.cpp. These files contain an implementation of the PersonType class. Look through the implementation and ask questions about anything that confuses you. Now use inheritance to extend the PersonType class to create a class StudentType. Store your implementation in files student.h and student.cpp. Make sure that you  Include 2 new data members in the StudentType class: credits and GPA  Include 4 new functions:  void setCredits(int totalCredits);void setGPA(float currentGPA);int getCredits( );float getGPA( );  Use polymorphism to provide a new implementation of the function print( ). In other words write an implementation for the print( ) function that will work for the StudentType class.  Provide both a default and non-default constructor for the StudentType class. Use the syntax to call the original constructor for the PersonType class to initialize the majority of the data members in the StudentType class (see page 784) The file testStudent.cpp contains code that uses an object of type StudentType. Run this program to verify that the functionality of the StudentType class has been correctly implemented.