The problem is your coding errors.
#ifndef SHIP_H #define SHIP_H #include<string> #include<iostream> using namespace std; class Ship { // stuff snipped in interest of brevity virtual void printInfo() { cout<<"The name of the ship is "<<name<<endl; endl; cout<<"The year the ship was built is "<<year<<endl; }; #endif
"endl;" on a line by itself will trigger an error: std::endl is a stream manipulator, not something that works in its own. You have also not matched braces ({ and }). The }; on the last line ends the function body, but there is no closing of the class declaration. In subsequent code (typically within a source file that #include's this header) the compiler will get confused because things appear out of context it expects.
This is the code for the inherited class...
#include "ship.h" #ifndef CARGOSHIP_H #define CARGOSHIP_H //inherits from ship.h class cargoShip : public Ship private: int c; {
The declaration of class members need to occur in the body of the class i.e. after the brace.
public: // more stuff snipped for brevity //override printInfo() printInfo() { cout<<"The name of the ship is "<<Ship::printInfo()<<endl; endl; cout<<"The capacity of the ship is "<<capacity<<endl; }
You have the same coding error here as you had in your base class.
I know that I can't just call the Ship class by itself because it includes a virtual function but for some reason, when I use these two lines in my main.cpp to call the inherited cargoShip class, I …