Question: C++ Assignment In visual studio Create file ShipClass.h which defines ShipClass that has the following members: A member variable for the name of the ship
C++ Assignment
In visual studio Create file ShipClass.h which defines ShipClass 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 (an int)
A constructor and appropriate set and get functions
A print function that displays the ships name and the year it was built.
Create file CruiseShip.h which defines CruiseShipClass that is derived from ShipClass. The CruiseShipClass should have the following members:
A member variable for the maximum number of passengers (an int)
A constructor and appropriate set and get functions
A print function that overrides the print function in the base class. The CruiseShipClasss print function should invoke the ShipClasss print function then display the maximum number of passengers.
Create file CargoShipClass.h which defines CargoShipClass that is derived from ShipClass. The CargoShipClass should have the following members:
A member variable for the cargo capacity in tonnage (an int).
A constructor and appropriate set and get functions.
A print function that overrides the print function in the base class. The CargoShipClasss print function should invoke the ShipClasss print function then display the ships cargo capacity.
Write a complete program that demonstrates these classes by asking the user to:
1. Enter values and create a ShipClass Object
2. Print the contents of the ShipClass object
3. Enter values and create a CruiseShipClass Object
4. Print the contents of the CrusieShipClass object
5. Enter values and create a CargoShipClass Object
6. Print the contents of the CagoShipClass object
Here is my code along with the error messages I'm getting next to it.
ShipClass.h
#ifndef SHIPCLASS_H
#define SHIPCLASS_H
class ShipClass
{
public:
ShipClass(string name, string year);
string getName();
void setName(string name);
string getYear();
void setYear(string year);
void print();
private:
string name;
string year;
};
#endif
ShipClass.cpp
#include
using namespace std;
#include "ShipClass.h"
ShipClass::ShipClass(string name, string year)
{
this->name = name;
this->year = year;
}
string ShipClass::getName()
{
return name;
}
void ShipClass::setName(string name)
{
this->name = name;
}
string ShipClass::getYear()
{
return year;
}
void ShipClass::setYear(string year)
{
this->year = year;
}
void ShipClass::print()
{
cout << "Ship Name :" << getName() << endl; \\binary << no operator found which takes a right hand operand of type std::string
cout << "Built Year :" << getYear() << endl << endl; \\same as line above with no operator << matches these operands
}
CargoShipClass.h
#ifndef CARGOSHIPCLASS_H
#define CARGOSHIPCLASS_H
#include "ShipClass.h"
class CargoShipClass :public ShipClass
{
public:
CargoShipClass(string name, string year, int capacity_tonnage);
int getCapacity_tonnage();
void setCapacity_tonnage(int capacity_tonnage);
void print();
private:
int capacity_tonnage;
};
#endif
CargoShipClass.cpp
#include
using namespace std;
#include "CargoShipClass.h"
#include "ShipClass.h"
CargoShipClass::CargoShipClass(string name, string year, int capacity_tonnage) : ShipClass(name, year)
{
this->capacity_tonnage = capacity_tonnage;
}
int CargoShipClass::getCapacity_tonnage()
{
return capacity_tonnage;
}
void CargoShipClass::setCapacity_tonnage(int capacity_tonnage)
{
this->capacity_tonnage = capacity_tonnage;
}
void CargoShipClass::print()
{
cout << "Ship Name :" << getName() << endl; \\ no operator << matches these operands
cout << "Capacity :" << getCapacity_tonnage() << endl << endl;
}
CruiseShipClass.h
#ifndef CRUISESHIPCLASS_H
#define CRUISESHIPCLASS_H
#include "ShipClass.h"
class CruiseShipClass :public ShipClass
{
public:
CruiseShipClass(string name, string year, int max_no_of_passengers);
int getMax_no_of_passengers();
void setMax_no_of_passengers(int max_no_of_passengers);
void print();
private:
int max_no_of_passengers;
};
#endif
CruiseShipClass.cpp
#include
using namespace std;
#include "CruiseShipClass.h"
#include "ShipClass.h"
CruiseShipClass::CruiseShipClass(string name, string year, int max_no_of_passengers) :ShipClass(name, year)
{
this->max_no_of_passengers = max_no_of_passengers;
}
int CruiseShipClass::getMax_no_of_passengers()
{
return max_no_of_passengers;
}
void CruiseShipClass::setMax_no_of_passengers(int max_no_of_passengers)
{
this->max_no_of_passengers = max_no_of_passengers;
}
void CruiseShipClass::print()
{
cout << "Ship Name :" << getName() << endl; \ o operator << matches these operands
cout << "Max No Passengers :" << getMax_no_of_passengers() << endl << endl;
}
Main.cpp
#include
#include
using namespace std;
#include "ShipClass.h"
#include "CargoShipClass.h"
#include "CruiseShipClass.h"
int main()
{
string name;
string year;
int max_no_of_passengers;
int capacity_tonnage;
cout << " Enter the Ship Name :";
getline(cin, name); \\getline identifier not found
cout << "Enter the Built Year :";
cin >> year; \\binary >> no operator found which takes a right hand operand of type std::string
ShipClass s(name, year);
cin.ignore();
cout << " Enter the Cruise Ship Name :";
getline(cin, name); \\ getline identifier not found
cout << "Enter the Built Year :";
cin >> year; \ o operator found which takes a right hand operand of type std::string
cout << "Enter Capacity Tonnage :";
cin >> capacity_tonnage;
CargoShipClass cs(name, year, capacity_tonnage);
cin.ignore();
cout << " Enter the Cruise Ship Name :";
getline(cin, name); \\ getline identifier not found
cout << "Enter the Built Year :";
cin >> year; \\binary >> no operator found which takes a right hand operand of type std::string
cout << "Enter no of Passengers :";
cin >> max_no_of_passengers;
CruiseShipClass csc(name, year, max_no_of_passengers);
cout << " Displaying Ship Info :" << endl;
s.print();
cout << " Displaying Cargo Ship Info :" << endl;
cs.print();
cout << " Displaying Cruise Ship Info :" << endl;
csc.print();
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
