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 get tons of errors.
No surprises there. You've given the compiler woefully incorrect code. Compilers are dumb beasts so, even when you feed them incorrect code, they tend to persist with interpreting later code. This causes them to get horribly confused, and emit all sorts of strange error messages.
The basic trick you need to learn is to scroll back through the error messages and find thefirst one emitted by the compiler. Errors tend to cascade and, as the compiler encounters more errors in code, the compiler gets more confused. Looking at the last error message first (which a lot of beginners do) tends to mean looking at error messages caused by the compiler's state of confusion rather than the underlying errors caused by the programmer.