O b j e c t O r i e n t e d P r o g r a m m i n g L a b
Lab 10 Marks 05
Instructions
➢ Work in this lab individually.
➢ You can use your books, notes, handouts etc. but you are not allowed to borrow anything from your peer student.
➢ Make sure to follow the best coding practices.
➢ Include comments to explain the logic where necessary.
➢ You are strictly NOT ALLOWED to include any additional data-members/functions/constructors in your class.
➢ Test your program thoroughly with various inputs to ensure proper functionality and error handling.
➢ Show your work to the instructor before leaving the lab to get some or full credit.
Ship Inheritance Hierarchy
Implement the following class hierarchy. The inheritance access level should be public for all derived classes.
Ship Class Details
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)
• An appropriate constructor
• Appropriate accessors and mutators
• A virtual print function that displays the ship’s name and the year it was built.
CruiseShip Class Details
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)
• An appropriate constructor
• Appropriate accessors and mutators
• A print function that overrides the print function in the base class. The CruiseShip class’s print function should display the
ship’s name, year of build, and the maximum number of passengers.
CargoShip Class Details
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)
• An appropriate constructor
• 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.
BattleShip Class Details
Design a BattleShip class that is derived from the Ship class. The BattleShip class should have the following members:
• A member variable for the total number of missiles (an int)
• An appropriate constructor
• Appropriate accessors and mutators
• A print function that overrides the print function in the base class. The BattleShip class’s print function should display only
the ship’s name and the missile capacity.
Main Program Details
Create an array of 4 Ship pointers and initialize its elements with the addresses of dynamically allocated objects of the classes Ship,
CruiseShip, CargoShip, and BattleShip. Then, iterate through the array and call the print function of each object to demonstrate
the classes. Finally, release any occupied memory resources.
Umair Babar, FCIT – PU. Lahore. Page 1 of 1