COSC 1437 Programming Fundamentals IIBy FirstName LastName
15
Inheritance and Polymorphism
Exercise 1:
Assume the existence of a Phone class.
Define a derived class, CameraPhone that contains two member variables:
▪ An int named imageSize that contains the number of megabytes that a picture uses
on the phone
▪ An int named memorySize that contains the number of megabytes in the camera’s
memory
Write the following member functions in the CameraPhone class:
▪
▪
A constructor that accepts two int arguments. The first argument should be assigned to
the imageSize member, and the second argument should be assigned to the
memorySize argument.
A member function named numPictures that returns (as an int) the number of
pictures the camera’s memory can hold.
Exercise 2:
In this exercise you will write the code for two classes.
Person Class
Write a class named Person with the following member variables:
•
•
•
•
lastName, a string
firstName, a string
email, a string
phone, a string
Write a constructor that accepts no arguments and initializes all the member variables to an
empty string.
Write a second constructor that accepts arguments that will be used to initialize the member
variables. The arguments must be accepted in this order: lastName, firstName, email,
and phone.
Write mutator functions named setLastName, setFirstName, setEmail, and
setPhone.
Write accessor functions named getLastName, getFirstName, getEmail, and
getPhone.
Customer Class
Next, write a class named Customer, which is derived from the Person class.
The Customer class should inherit the Person class’s member variables and member
functions. In addition, the Customer class should have the following member variables of its own:
•
•
customerNumber, an int
emailList, a bool
The customerNumber member variable will be used to hold a unique integer for each customer.
The emailList member variable will be set to true if the customer wishes to be on an email list,
or false if the customer does not wish to be on an email list.
Write a constructor that accepts no arguments and initializes customerNumber to 0 and
emailList to false. This constructor should also call the base class’s constructor that
accepts no arguments.
Write a second constructor that accepts arguments that will be used to initialize the member
variables. The arguments must be accepted in this order: lastName, firstName, email,
phone, customerNumber, and emailList. This constructor should call the base class
constructor to initialize the inherited member variables.
Write mutator functions named setCustomerNumber and setEmailList.
Write accessor functions named getCustomerNumber and getEmailList.
For this exercise, do not write a complete program. Write the classes and their member functions
only.