1

I'm trying to writing some code for my c++ class. I'm using eclipse. I'm having a hard time trying to understand some of the instructions in the problem.

I've created a base class called Ship and then used inheritance for my CruiseShip class and CargoShip class.

For the CruiseShip class, I'm instructed to create

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.

And similarly for the CargoShip class

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.

I'm not sure what it means to "override" the print function in the base class.

It also instructs me to

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.

#include <iostream>
#include <string>
using namespace std;

class Ship
{
 protected:
    string ship_name;
    int year_built;
public:
    Ship()
    {
        ship_name="";
        year_built=0;
    }
    void set_ship_name(string str)
    {
        ship_name=str;
    }
    void set_year(int y)
    {
        year_built=y;
    }
    int get_year()
    {
            return year_built;
    }
    string get_ship_name()
    {
            return ship_name;
    }

    void print(string, int)
    {
            cout<<"Ship name is "<<ship_name<<" and it was built in the year "<<year_built<<endl;
    }
};

class CruiseShip: public Ship
{
private:
    int max_passengers;
public:
    CruiseShip()// :Ship(str,year)
    {
    max_passengers=0;
    }
    void set_passengers(int pass)
    {
            max_passengers=pass;
    }
    int get_passengers()
    {
            return max_passengers;
    }
    void print1(string, int)
    {
            cout<<"Ship name is "<<get_ship_name()<<" and max number of passengers are "<<max_passengers<<endl;
    }

};

class CargoShip: public Ship
{
private:
    int cargo_capacity_in_tons;
public:
    CargoShip()//:Ship (str,year)
    {
        cargo_capacity_in_tons=0;
    }
    void set_capacity(int pass)
    {
        cargo_capacity_in_tons=pass;
    }
    int get_capacity()
    {
            return cargo_capacity_in_tons;
    }
    void print2(string, int)
    {
            cout<<"Ship name is "<<get_ship_name()<<" and its capacity is "<<cargo_capacity_in_tons<<" Tons."<<endl;
    }
};

int main(){
CruiseShip ship1;
CargoShip ship2;

string ship_name1;
string ship_name2;
int year_built1;
int year_built2;
int max_passengers;
int cargo_capacity_in_tons;

cout<<"What is the name of the cruise ship?"<<endl;
cin>>ship_name1;
ship1.set_ship_name(ship_name1);

cout<<"What year was "<<ship_name1<<" built in?"<<endl;
cin>>year_built1;
ship1.set_year(year_built1);


cout<<"What is the maximum capacity of "<<ship_name1<<"?"<<endl;
cin>>max_passengers;
ship1.set_passengers(max_passengers);

//ship1.print(ship_name1, year_built1);
ship1.print1(ship_name1, max_passengers);

cout<<"What is the name of the cargo ship?"<<endl;
cin>>ship_name2;
ship2.set_ship_name(ship_name2);

cout<<"What year was "<<ship_name2<<" built in?"<<endl;
cin>>year_built2;
ship2.set_year(year_built2);

cout<<"What is the maximum capacity of "<<ship_name2<<" in tons?"<<endl;
cin>>cargo_capacity_in_tons;
ship2.set_capacity(cargo_capacity_in_tons);

ship2.print2(ship_name2, cargo_capacity_in_tons);


return 0;
}
8
  • 2
    "I'm using eclipse as my compiler." Eclipse is an IDE, not a compiler. Commented Dec 7, 2015 at 22:59
  • 3
    What @MrEricSir meant to say is that you should google "virtual functions c++" and also "base class pointer polymorphism c++" Most people won't help you here b/c you've shown no effort to research the problem. Commented Dec 7, 2015 at 23:04
  • I didn't know that. I'll edit my question... @MrEricSir Commented Dec 7, 2015 at 23:05
  • 1
    @TriHard8 Thanks for pointing me in the right direction. Commented Dec 7, 2015 at 23:12
  • Easy on the setters! You are never going to need set_year as the build year is never going to change. set_capacity is likewise extremely unlikely to be needed. Commented Dec 7, 2015 at 23:25

1 Answer 1

3

Let´s say you have the following classes:

class Animal
{
private:
  int x;
  int y;
public:
   virtual string sound() {return "Animal";}
   void move() {x += 1; y+=1;}
};

class Cow
{
   string sound() {return "Muh"} //this is overriding
   string sound(string soundYouWant) {return soundYouWant;} //this is not overriding as string sound(string soundYouWant) is not the same as string sound()
   void move() {x += 1; y+=1;} //this is also not overriding as move() in Animal has no virtual
};

So to summarize, overriding means you have a virtual method in the base class and you re-declare it in the derived class. This way, you are able to re-define it for every derived class (the method-body can be different for the base class and each of its derived classes).

Now to dynamic allocated arrays:

int size;
std::cin >> size;
int *array = new int[size]; //the array is stored on the heap
delete[] array; //deallocates the array and so frees the memory

If you create an array on the stack (without new), you either have to hardcode its size using literals (0, 1, 2, ...) or using a const int variableName. This way, the compiler knows the array size during compile time. So you have to know the array size while writing your program. Consequently, the compiler wouldn´t allow you to do this: std::cin >> size;.

Using new (dynamical arrays) you are allowed to specify the array size during compile time. So it is legal to let your program calculate the array size or take it as an user input. With dynamic arrays you also have a lot, lot, lot more memory than using the small stack (stackoverflow).

int *array: obviously the memory content is interpreted as integers. *array points to the first element of the array. int *array does NOT know the SIZE of the array. You have to keep track of that yourself.

new int[size]: You are reserving space for size * integers on the heap.

You might know that C++ does not have a garbage collector. This is when delete[] array; comes into play. When you don´t need array anymore (this includes other pointers pointing to array) you should call delete to free the memory. With small, short running programs, forgetting it won´t matter as the OS (operation system) will free the memory after your program has terminated. Nevertheless, you should use delete as not using it is very bad still and will lead to trouble with bigger programs. You should place delete in the destructor of a class (~clasname()) if you use array within a class.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.