Question

Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design...

Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with)

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 contsructor and appropriate accessors and mutators

- A virtual print function that displays the ship's name and the year it was built (nobody seems to get this part right)

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 contsructor 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 contsructor 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.

********* MY CODE GETTING ERROR- STRAY IN PROGRAM SEVERAL TIMES HELP!! C++ CODE BLOCK

#include <iostream>

using namespace std;


class Ship
{
private:
string Shipname;
string Shipyear;
public:
void setShipName(string);
void setshipyear(string);
string getShipname();
string getShipyear();
virtual void getshipNameAndData();
ship(string name, string year)
{
Shipname = name;
Shipyear = year;
}
};

void Ship::setShipName(string name)
{
Shipname = name;
}
void Ship::setshipyear(string year)
{
Shipyear = year;
}
string Ship::getShipname()
{
return Shipname;
}
string Ship::getShipyear()
{
return Shipyear;
}
virtual void Ship::getshipNameAndData()
{
cout << "The Ship Name: " << getShipname() << endl;
cout << "The Ship Year: " << getShipyear() << endl;
}
class CruiseShip: public Ship
{
private:
int NumberofMembers;
public:
void setNumberofMembers(int);
int getNumberofMembers();
virtual void getshipNameAndData();
CruiseShip(string name, string year, int numberofpeople):Ship(name, year)
{
NumberofMembers = Numberofpeople;
}

};
void CruiseShip::setNumberofMembers(int NumberofPeople)
{
NumberofMembers = NumberofPeople;
}

int CruiseShip::getNumberofMembers()
{
return NumberofMembers;
}
virtual void CruiseShip::getshipNameAndData()
{
cout << "The Ship Name: " << getShipname() << endl;
cout << "The Ship number of members: " << getNumberofMembers() << endl;
}
class CargoShip:public Ship
{
private:
int CargoCapacity;
public:
void setCargoCapacity(int);
int getCargoCapacity();
virtual void getshipNameAndData();
CargoShip(string name, string year, int space):Ship(name,year)
{
CargoCapacity = space;
}

};
void CargoShip::setCargoCapacity(int space)
{
CargoCapacity = space;
}

int CargoShip::getCargoCapacity()
{
return CargoCapacity;
}
virtual void CargoShip::getshipNameAndData()
{
cout << "The Ship Name: " << getShipname() << endl;
cout << "The Ship Capacity: " << getCargoCapacity() << endl;
}

int main()
{

  
Ship * Ships[3] = {new Ship("USS Hornet", 1942)
new CruiseShip("Disney Cruise", 1000)
new CargoShip("MOL Triumph", 20000)}
for(int count = 0; count < 3; count++)
{
ships[count].getshipNameAndData();
}

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

HI, I have fixed all compilation errors.

I t is working fine now.

Please let me know in case of any issue.

#include <iostream>
using namespace std;

class Ship
{
private:
string Shipname;
string Shipyear;
public:
void setShipName(string);
void setshipyear(string);
string getShipname();
string getShipyear();
virtual void getshipNameAndData();
Ship(string name, string year)
{
Shipname = name;
Shipyear = year;
}
};
void Ship::setShipName(string name)
{
Shipname = name;
}
void Ship::setshipyear(string year)
{
Shipyear = year;
}
string Ship::getShipname()
{
return Shipname;
}
string Ship::getShipyear()
{
return Shipyear;
}
void Ship::getshipNameAndData()
{
cout << "The Ship Name: " << getShipname() << endl;
cout << "The Ship Year: " << getShipyear() << endl;
}
class CruiseShip: public Ship
{
private:
int NumberofMembers;
public:
void setNumberofMembers(int);
int getNumberofMembers();
virtual void getshipNameAndData();
CruiseShip(string name, string year, int numberofpeople):Ship(name, year)
{
NumberofMembers = numberofpeople;
}
};
void CruiseShip::setNumberofMembers(int numberofpeople)
{
NumberofMembers = numberofpeople;
}
int CruiseShip::getNumberofMembers()
{
return NumberofMembers;
}
void CruiseShip::getshipNameAndData()
{
cout << "The Ship Name: " << getShipname() << endl;
cout << "The Ship number of members: " << getNumberofMembers() << endl;
}
class CargoShip:public Ship
{
private:
int CargoCapacity;
public:
void setCargoCapacity(int);
int getCargoCapacity();
virtual void getshipNameAndData();
CargoShip(string name, string year, int space):Ship(name,year)
{
CargoCapacity = space;
}
};
void CargoShip::setCargoCapacity(int space)
{
CargoCapacity = space;
}
int CargoShip::getCargoCapacity()
{
return CargoCapacity;
}
void CargoShip::getshipNameAndData()
{
cout << "The Ship Name: " << getShipname() << endl;
cout << "The Ship Capacity: " << getCargoCapacity() << endl;
}
int main()
{
  
Ship * ships[3] = {new Ship("USS Hornet", "1942"),
new CruiseShip("Disney Cruise", "1000", 5),
new CargoShip("MOL Triumph", "20000", 10)
};
for(int count = 0; count < 3; count++)
{
ships[count]->getshipNameAndData();
}

return 0;
}

→ firstweek → firstweek → firstweek g++ Ship.cpp → firstweek ./a . out The Ship Name: USS Hornet The Ship Year: 1942 The Ship

Add a comment
Know the answer?
Add Answer to:
Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • [JAVA] Program: Design a Ship class that the following members: A field for the name of...

    [JAVA] Program: Design a Ship class that the following members: A field for the name of the ship (a string) o A field for the year the the ship was built (a string) o A constructor and appropriate accessors and mutators A toString method that displays the ship's name and the year it was built Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: A field for the maximum number of passengers...

  • Program Challenge 10 Design a Ship class that the following members: ·         A field for the...

    Program Challenge 10 Design a Ship class that the following members: ·         A field for the name of the ship (a string). ·         A field for the year that the ship was built (a string). ·         A constructor and appropriate accessors and mutators. ·         A toString method that displays the ship’s name and the year it was built. Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: ·         A field for the...

  • QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members:...

    QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members: • A private String data field named name for the name of the ship. • A private String data field named yearBuilt for the year that the ship was built. • A constructor that creates a ship with the specified name and the specified year that the ship was built. • Appropriate getter and setter methods. A toString method that overrides the toString method...

  • Please provide a multi lined pseudo code for the program below.....also it is giving me a...

    Please provide a multi lined pseudo code for the program below.....also it is giving me a build error? Could you possibly tell me how to fix the error too? #include <iostream> #include <string> using namespace std; class ship { public: string name; int year; ship(string n, int y) { name = n; year = y; } int getYear() { return year; } string getName() { return name; } void setYear(int y) { year = y; } void setName(string n) {...

  • C++ Could you check my code, it work but professor said that there are some mistakes(check...

    C++ Could you check my code, it work but professor said that there are some mistakes(check virtual functions, prin() and others, according assignment) . Assignment: My code: #include<iostream> #include<string> using namespace std; class BasicShape { //Abstract base class protected: double area; private:    string name; public: BasicShape(double a, string n) { area=a; name=n; } void virtual calcArea()=0;//Pure Virtual Function virtual void print() { cout<<"Area of "<<getName()<<" is: "<<area<<"\n"; } string getName(){    return name; } }; class Circle : public...

  • Use main.cpp, Student.h, Student.cpp (written below) and write classes StudentClub and a non-member function find youngest...

    Use main.cpp, Student.h, Student.cpp (written below) and write classes StudentClub and a non-member function find youngest to make main.cpp compile. Put the declaration and definition of find youngest in StudentClub.h and StudentClub.cpp separately. You may not modify provided files and only submit StudentClub.h and StudentClub.cpp. Non-member function find youngest is declared as follows. It returns the names of students who have the youngest age. Note there may exist more than one students who are youngest. std::vector find_youngest(const std::vector member); StudentClub...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • I keep getting the compilation error: pass the filename from command line arguement... Can you please...

    I keep getting the compilation error: pass the filename from command line arguement... Can you please tell me what im doing wrong? here is the code: import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ShipDemo { public static void main(String[] args) { String companyName; int num_ships, total_passengers=0, total_tonnage=0; Ship[] shipInventory; try { if(args.length == 1) { File file = new File(args[0]); Scanner fileScan = new Scanner(file); companyName = fileScan.nextLine(); num_ships = fileScan.nextInt(); shipInventory = new Ship[num_ships]; int n=0; String type,name;...

  • The code should be written in C++. I included the Class used for this function. Also,...

    The code should be written in C++. I included the Class used for this function. Also, please fix the main function if necessary. Description: This function de-allocates all memory allocated to INV. This will be the last function to be called (automatically by the compiler) //before the program is exited. This function also erased all data in the data file. There are other functions that add, process and alter the file; the file printed after those functions will have new...

  • C++ Language I have a class named movie which allows a movie object to take the...

    C++ Language I have a class named movie which allows a movie object to take the name, movie and rating of a movie that the user inputs. Everything works fine but when the user enters a space in the movie's name, the next function that is called loops infinetly. I can't find the source of the problem. Down below are my .cpp and .h file for the program. #include <iostream> #include "movie.h" using namespace std; movie::movie() { movieName = "Not...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT