CMPS-2020 Programming II: Data Structures and Algorithms
Lab-3
Additional homework requirement added at bottom.
Overview:
Write this program, from our textbook chapter-15 #12. Name your program 2020/3/boats.cpp Program instructions... Ship, CruiseShip, and CargoShip Classes 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. (See Program 15-14, lines 17 through 22, for an example of how to do this.) The program should then step through the array, calling each object's print function. You must finish this program to receive a lab score of 10. Your lab-3 (boats.cpp) program must compile when you leave the lab session. If you do not finish this during lab, then it will become homework.
Copy your 2020/3/boats.cpp program to 2020/3/boats2.cpp. Modify the boats2.cpp program. Comment-out your current main function, and add the following... int main() { Ship *s[3] = { new Ship("MyYacht", "2018"), new CruiseShip("LoveBoat", "1981", 2000), new CargoShip("DeBarge", "1974", 45000000) }; cout << endl; for (int i=0; i<3; i++) { s[i]->printShip(); delete s[i]; } return 0; } Make your boats2.cpp program work.
From chapter 15, #13 Pure Abstract Base Class Project Name your program: 2020/3/shapes.cpp <-----------LOOK Define a pure abstract base class called BasicShape. The BasicShape class can have public member variables while you are testing, but please make the program work with private member variables. List of member variables anf functions are below. Member Variable: area, a double used to hold the shape's area. Member Functions: getArea. Function will return the value in the member variable area. calcArea. This function will be a pure virtual function. Next, define a class named Circle. It should be derived from the BasicShape class. It should have the following public members: Member Variables: centerX, a long integer that holds an x coordinate. centerY, a long integer that holds a y coordinate. radius, a double used to hold the circle's radius. Member Functions: constructor: Accepts values for centerX, centerY, and radius. Should call the overridden calcArea function described below. getCenterX: returns the value in centerX. getCenterY: returns the value in centerY. calcArea: calculates the area of the circle (area = 3.14159 * radius * radius) and stores the result in the inherited member area. Next, define a class named Rectangle. It should be derived from the BasicShape class. It should have the following members: Member Variables: width, a long integer used to hold the width of the rectangle. length, a long integer used to hold the length of the rectangle. Member Functions: constructor: accepts values for width and length. Should call the overridden calcArea function described below. getWidth: returns the value in width. getLength: returns the value in length. calcArea: calculates the area of the rectangle (area = length * width) and stores the result in the inherited member area. Next, define a class named Triangle. It should be derived from the BasicShape class. It should have the following members: Member Variables: base, a long integer used to hold the triangle base. height, a long integer used to hold the triangle height. Member Functions: constructor: accepts values for base and height. Should call the overridden calcArea function described below. getBase: returns the value in base. getHeight: returns the value in height. calcArea: calculates the area of the triangle (area = (base * height) / 2.0 ) and stores the result in the inherited member area. After you have created these classes, create a driver program that defines a Circle, Rectangle, and Triangle object. Demonstrate that each object properly calculates and reports its area. You may use the main function below. int main() { cout << "\nshapes.cpp program\n\n"; Circle c(20, 30, 100.0); Rectangle r(110, 220); Triangle t(123, 456); BasicShape *s1 = &c; BasicShape *s2 = &r; BasicShape *s3 = &t; cout << "circle area: " << s1->getArea() << endl; cout << "rectangle area: " << s2->getArea() << endl; cout << "triangle area: " << s3->getArea() << endl; cout << endl; return 0; } If this doesn't make sense, go read the problem in the textbook.
Modify the boats.cpp program. In the Ship class, define the ship name and year built to be Mystring types. Use the Mystring class from your lab-2 homework. 1. Put the specification or definition of the Mystring class in an include file named mystring.h. Do not put executable code in the include file. The include file contains only definitions and prototypes. 2. Use include guards to ensure the integrity of your include file. Chapter-13 of our textbook shows how this is done. 3. Define the member function bodies in your boats.cpp file. Please define these functions just below the main function. 4. Define only the functions needed for the boats.cpp program. Do not bring in the entire Mystring class from your homework. Bring in just what you need. (very little) Your Mystring class should work identically to the C++ String class.
Your instructor will find your work out on Odin.