#include <iostream> #include <cstring> using namespace std; class Ship { protect
ID: 3671107 • Letter: #
Question
#include <iostream>
#include <cstring>
using namespace std;
class Ship {
protected:
string name;
string year;
public:
Ship(string n, string y)
{
name = n;
year = y;
}
virtual void printShip()
{
cout << "Ship name: " << name << endl;
cout << "Year built: " << year << endl;
}
};
class CruiseShip : public Ship {
private:
int passengers;
public:
//overload constructor that has inherited varibles
CruiseShip(string n, string y, int p) : Ship(n,y)
{
passengers = p;
}
void printShip()
{
cout << "Cruise Ship: " << name << endl;
cout << "Number of passengers: " << passengers << endl;
}
};
class CargoShip : public Ship {
private:
int capacity;
public:
CargoShip(string n, string y, int t) : Ship(n,y)
{
capacity = t;
}
void printShip()
{
cout << "Cargo Ship: " << name << endl;
cout << "Capacity: " << capacity << endl;
}
};
so i got the program working using these classes. but i still get this warning when i compile using "-Wall"
ship.cpp: In function 'int main()':
ship.cpp:64:19: warning: deleting object of polymorphic class type 'Ship' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]
Explanation / Answer
#include <iostream>
#include <cstring>
using namespace std;
class Ship // main class
{
public:
string name;
string year;
public:
Ship(string n, string y)
{
name = n;
year = y;
}
virtual void printShip() // virtual which makes that function existing in appearance only but, you can't call that function
{
cout << "Ship name: " << name << endl;
cout << "Year built: " << year << endl;
}
};
class CruiseShip : public Ship // sub class under MAIN (Ship )
{
private:
int passengers;
public:
//overload constructor that has inherited varibles
CruiseShip(string n, string y, int p) : Ship(n,y)
{
passengers = p;
}
void printShip()
{
cout << "Cruise Ship: " << name << endl;
cout << "Number of passengers: " << passengers << endl;
}
};
class CargoShip : public Ship
{
private:
int capacity;
public:
CargoShip(string n, string y, int t) : Ship(n,y)
{
capacity = t;
}
void printShip()
{
cout << "Cargo Ship: " << name << endl;
cout << "Capacity: " << capacity << endl;
}
};
int main( void)
{
//call the function anything you want to display as output here
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.