0% found this document useful (0 votes)
17 views5 pages

OOP Inheritance

The document outlines a series of programming exercises focused on object-oriented design, requiring the creation of various classes and their relationships. Tasks include implementing classes for sales transactions, weather reports, pet store items, paintings, employees, graded activities, automobiles, investments, restaurant meals, ships, horses, candles, cabin rentals, packages, and a college system. Each exercise emphasizes the use of constructors, accessors, mutators, and friend functions, along with user interaction for data input and output.

Uploaded by

hemanthvasu822
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

OOP Inheritance

The document outlines a series of programming exercises focused on object-oriented design, requiring the creation of various classes and their relationships. Tasks include implementing classes for sales transactions, weather reports, pet store items, paintings, employees, graded activities, automobiles, investments, restaurant meals, ships, horses, candles, cabin rentals, packages, and a college system. Each exercise emphasizes the use of constructors, accessors, mutators, and friend functions, along with user interaction for data input and output.

Uploaded by

hemanthvasu822
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PSG COLLEGE OF TECHNOLOGY, Coimbatore-4

Department Of Applied Mathematics And Computational Sciences


23XD26 Object Computing Lab
Problem Sheet

Inheritance
1. Create two classes. The first, named Sale, holds data for a sales transaction. Its private data members
include the day of the month, amount of the sale, and the salesperson’s ID number. The second class,
named Salesperson, holds data for a salesperson, and its private data members include each
salesperson’s ID number and last name. Each class includes a constructor to which you can pass the
field values. Create a friend function named display()that is a friend of both classes and displays the
date of sale, the amount, and the salesperson ID and name. Write. Add a function to both the Sale and
Salesperson classes that returns the private salesperson ID number. Write a main()function that
contains an array of five Salesperson objects and store appropriate data in it. Then, continue to prompt
the user for Sale data until the user enters an appropriate sentinel value. For each Sale transaction
entered, determine whether the salesperson’s ID number is valid. Either display an error message, or
use the friend display()function to display all the data.

2. Create a class named WeatherReport that holds a daily weather report with data members such as
dayOfMonth, highTemp, lowTemp, amountRain, and amountSnow. The constructor initializes the
fields with default values: 99 for dayOfMonth, -9999 for highTemp, 9999 for lowTemp, and 0 for
amountRain and amountSnow. Include a function that prompts the user and sets values for each field
so that you can override the default values. Instantiate 30 WeatherReport objects and, in a loop, prompt
the user for a month’s data. At the end of the month, a month-end WeatherReport object is created.
Initialize the object with default values; then use a friend function to store the high temperature, low
temperature, and rain and snow totals for the month in the object. The friend function takes two
WeatherReport objects—the summary object and one day’s object—and it returns an updated
summary object. If the day’s high temperature is higher than the summary object’s high, then replace
the summary high. If the day’s low temperature is lower than the summary object’s low, then replace
the summary low. Accumulate rain and snow in the summary object. Write a main()function that
creates a month-end weather report from the 30 daily reports

3. Consider a class developed for all items sold by a pet store. PetStoreItem class might contain data
members for stock number and price. Derive a class PerStoreAnimal from PetStoreItem. The
PetStoreAnimal should have a field for Age. Include the required constructors for both the classes.

4. Create a Painting class that holds the painting title, artist name, and value. All Paintings are valued at
$400 unless they are FamousPaintings. Include a display function that displays all fields. The
FamousPainting subclass overrides the Painting value and sets each Painting’s value to $25,000. Write
a main()function that declares an array of 10 Painting objects. Prompt the user to enter the title and
artist for each of the 10 Paintings. Consider the Painting to be a FamousPainting if the artist is one of
the following: Degas, Monet, Picasso, or Rembrandt. Display the 10 Paintings.

5. Design a class named Employee . The class should keep the following information in
• Employee name
• Employee number
• Hire date
Write one or more constructors and the appropriate accessor and mutator functions for the class. Next,
write a class named ProductionWorker that is derived from the Employee class.The ProductionWorker
class should have member variables to hold the following information:
• Shift (an integer)
• Hourly pay rate (a double )
The workday is divided into two shifts: day and night. The shift variable will hold an integer value
representing the shift that the employee works. The day shift is shift 1, and the night shift is shift 2.
Write one or more constructors and the appropriate accessor and mutator functions for the class.
Demonstrate the classes by writing a program that uses a ProductionWorker object.

6. Most teachers assign various graded activities for their students to complete. A graded activity can
receive a numeric score such as 70, 85, 90, and so on, and a letter grade such as A, B, C, D, or F.
Design a GradedActivity class to hold the numeric score and letter grade of a graded activity. When a
numeric score is stored by the class, it automatically determines the letter grade. FinalExam class,
which is derived from the GradedActivity class. It has member variables for the number of questions
on the exam ( numQuestions ), the number of points each question is worth ( pointsEach ), and the
number of questions missed by the student ( numMissed ). Create ‘n’ objects for the FinalExam class
and calculate their grades.

7. A car dealership company wants to develop software to manage its inventory of used cars. The
dealership’s inventory includes three types of automobiles: cars, pickup trucks, and sport-utility
vehicles (SUVs). Regardless of the type, the dealership keeps the following data about each
automobile:
• Make
• Year model
• Mileage
• Price
Each type of vehicle that is kept in inventory has these general characteristics, plus its own
specialized characteristics.
For cars, the dealership keeps the following additional data:
• Number of doors (2 or 4)
For pickup trucks, the dealership keeps the following additional data:
• Drive type (two-wheel drive or four-wheel drive)
And, for SUVs, the dealership keeps the following additional data:
• Passenger capacity
Create the appropriate base and derived classes. Write a main() and create objects and test
their functionality.
8. Create an Investment class that contains fields to hold the initial value of an investment, the current
value, the profit (calculated as the difference between current value and initial value), and the percent
profit (the profit divided by the initial value). Include a constructor that requires initial and current
values and a display function. Create a House class that includes fields for street address and square
feet, a constructor that requires values for both fields, and a display function. Create a
HouseThatIsAnInvestment class that inherits from Investment and House. It includes a constructor and
a display function that calls the display functions of the parents. Write a main()function that declares
a HouseThatIsAnInvestment and displays its values.
9. Create a RestaurantMeal class that holds the name and price of a food item served by a restaurant. Its
constructor requires arguments for each field. Create a HotelService class that holds the name of the
service, the service fee, and the room number to which the service was supplied. Its constructor also
requires arguments for each field. Create a RoomServiceMeal class that inherits from both
RestaurantMeal and HotelService. Whenever you create a RoomServiceMeal object, the constructor
assigns the string “room service” to the name of the service field, and $4.00 is assigned to the service
fee inherited from HotelService. Include a RoomServiceMeal function that displays all of the fields in
a RoomServiceMeal by calling display functions from the two parent classes. Additionally, the display
function should display the total of the meals plus the room service fee. In a main() function, instantiate
a RoomServiceMeal object that inherits from both classes. For example, a “steak dinner” costing
$19.99 is a “room service” provided to room 1202 for a $4.00 fee.

10. Design a Ship class that has the following members:


• A member variable for the name of the ship (a string)
• A member variable for the year that the ship was built (a string)
• A constructor and appropriate accessors and mutators
• A virtual print function that displays the ship’s name and the year it was built.
Design a CruiseShip class that is derived from the Ship class. The CruiseShip class should have the
following members:
• A member variable for the maximum number of passengers (an int )
• A constructor and appropriate accessors and mutators
• A print function that overrides the print function in the base class.
The CruiseShip class’s print function should display only the ship’s name and the maximum number
of passengers.
Design a CargoShip class that is derived from the Ship class. The CargoShip class should have the
following members:
• A member variable for the cargo capacity in tonnage (an int ).
• A constructor and appropriate accessors and mutators.
• A print function that overrides the print function in the base class.
The CargoShip class’s print function should display only the ship’s name and the ship’s cargo capacity.
Demonstrate the classes in a program that has an array of Ship pointers. The array elements should be
initialized with the addresses of dynamically allocated Ship, CruiseShip, and CargoShip objects. The
program should then step through the array, calling each object’s print function.

11. Create a class named Horse that contains data fields for the name, color, and birth year. Include methods for
these fields. Next, create a subclass named RaceHorse, which contains an additional field that holds the
number of races in which the horse has competed. Write an application that demonstrates using objects of
each class.
12. Mick’s Wicks makes candles in various sizes. Create a class for the business named Candle that contains data
fields for color, height, and price. Create get methods for all three fields. Create set methods for color and
height, but not for price. Instead, when height is set, determine the price as $2 per inch. Create a child class
named ScentedCandle that contains an additional data field named scent and methods to get and set it. In the
child class, override the parent’s setHeight() method to set the price of a ScentedCandle object at $3 per inch.
Write an application that instantiates an object of each type and displays the details.

13. Every summer, Leeland Lakeside resort rents cabins by the week. Create a class named CabinRental that
includes an integer field for the cabin number and a double field for the weekly rental rate. Include get
methods for these fields and a constructor that requires an integer argument representing the cabin number.
The constructor sets the weekly rate based on the cabin number; cabins numbered 1, 2, and 3 are $950 per
week, and others are $1,100 per week. Create an extended class named HolidayCabinRental that is used for
rentals during weeks that include summer holiday weekends. The constructor for this class requires a room
number and adds a $150 surcharge to the regular rental rate. Write an application named DemoCabinRental
that creates an object of each class, and demonstrate that all the methods work correctly.

14. Create a class named Package with data fields for weight in ounces, shipping method, and shipping cost. The
shipping method is a character: A for air, T for truck, or M for mail. The Package class contains a constructor
that requires arguments for weight and shipping method. The constructor calls a calculateCost() method that
determines the shipping cost, based on the following table:

The Package
class also contains a display() method that displays the values in all four fields. Create a subclass named
InsuredPackage that adds an insurance cost to the shipping cost, based on the following table:
Write an application named UsePackage that instantiates at least three objects of each type (Package and

InsuredPackage) using a variety of weights and shipping method codes. Display the results for each Package
and InsuredPackage.

15. Develop a set of classes for a college to use in various student service and personnel applications. Classes you
need to design include the following: l Person—A Person contains a first name, last name, street address, zip
code, and phone number. The class also includes a method that sets each data field, using a series of dialog
boxes and a display method that displays all of a Person’s information on a single line at the command line on
the screen.
a. CollegeEmployee—CollegeEmployee descends from Person. A CollegeEmployee also includes a Social
Security number, an annual salary, and a department name, as well as methods that override the
Person methods to accept and display all CollegeEmployee data.
b. Faculty descends from CollegeEmployee. This class also includes a Boolean field that indicates whether
the Faculty member is tenured, as well as methods that override the CollegeEmployee methods to
accept and display this additional piece of information.
c. Student—Student descends from Person. In addition to the fields available in Person, a Student
contains a major field of study and a grade point average as well as methods that override the Person
methods to accept and display these additional facts.

Write an application named CollegeList that declares an array of four “regular” CollegeEmployees,
three Faculty, and seven Students. Prompt the user to specify which type of person’s data will be entered (C,
F, or S), or allow the user to quit (Q). While the user chooses to continue (that is, does not quit), accept data
entry for the appropriate type of Person. If the user attempts to enter data for more than four
CollegeEmployees, three Faculty, or seven Students, display an error message. When the user quits, display a
report on the screen listing each group of Persons under the appropriate heading of College Employees,
Faculty, or Students. If the user has not entered data for one or more types of Persons during a session, display
an appropriate message under the appropriate heading.

You might also like