/**
* 15.12 Ship, CruiseShip and CargoShip
*
* 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.
*
* Programming Challenges 967
* 968 Chapter 15 Inheritance, Polymorphism, and Virtual Functions
* 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.
*/
/**
* 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.
*/
#ifndef SHIP_H
#define SHIP_H
//using namespace std;
class Ship
{
public:
Ship();
std::string getName();
void setName(std::string name);
std::string getYearBuilt();
void setYear(std::string yar);
std::string printNameAndYear();
~Ship();
private:
std::string name;
std::string year;
};
#endif // SHIP_H
#include <iostream>
#include "Ship.h"
Ship::Ship()
{
name = "";
year = "";
}
std::string Ship::getName() {
return name;
}
void Ship::setName(std::string nameIn) {
name = nameIn;
}
std::string Ship::getYearBuilt() {
return year;
}
void Ship::setYear(std::string yearIn) {
//std::string yar;
year = yearIn;
}
std::string Ship::printNameAndYear() {
return"The " + name + " was built in " + year + "\n";
}
Ship::~Ship()
{
//dtor
}
and finally my main:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include "Ship.h"
usingnamespace std;
int main()
{
Ship *santaMariaPtr = new Ship;
santaMariaPtr->setName("Santa Maria");
santaMariaPtr->setYear("1763");
cout << santaMariaPtr->printNameAndYear();
cout << "Press and key and return to exit ";
char c;
cin >> c;
return 0;
}
Now my question,
The above code worked fine, but when I added the following CruiseShip files, why did it throw the error message below? ( I have a feeling it has something to do with my use of std::strings but my classmate said they'll work just fine)
||=== Build: Debug in Ship (compiler: GNU GCC Compiler) ===|
include\Ship.h|19|error: 'string' in namespace'std' does not name a type|
include\Ship.h|20|error: 'std::string' has not been declared|
include\Ship.h|21|error: 'string' in namespace'std' does not name a type|
include\Ship.h|22|error: 'std::string' has not been declared|
include\Ship.h|23|error: 'string' in namespace'std' does not name a type|
include\Ship.h|26|error: 'string' in namespace'std' does not name a type|
include\Ship.h|27|error: 'string' in namespace'std' does not name a type|
||=== Build failed: 7 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Nowhere do you include the required <string> header.
A few problems:
- ship.h line 24 The instructions state that the print function should be virtual.
- ship.cpp line 29 - This returns a string. It doesn't print the values as instructed.
- cruiseship.h - This does not have an overloaded print function.
- CruiseShip is missing the required attribute (number_of_passengers), along with getters and setters for it.
The instructions for the Ship class say only that you need a constructor (singular). I would be inclined to also provide a constructor that accepts name and year built.