I'm trying to make my first base class header file and an inherited class from it. I've compared the syntax with at least two other examples that we did in class and they match, yet I'm getting a lot of errors when I try to compile.

This is the code for the base class:

#ifndef SHIP_H
#define SHIP_H
#include<string>
#include<iostream>
using namespace std;

class Ship
{
 private:
  string n;
  string y;
  
 public:

  //parameter constructor 
  Ship(string n, string y)
    {}

  void setName(string n)
  {name = n;}

  void setYear(string y)
  {year = y;}

  string getName()
  {return name;}

  string getYear()
  {return year;}

  virtual void printInfo()
  {
    cout<<"The name of the ship is "<<name<<endl;
    endl;
    cout<<"The year the ship was built is "<<year<<endl; 
  };
#endif

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;

{
 public:
   //parameter constructor
   cargoShip(string n, string y,int c):Ship(n,y)
    {
      capacity = c; 
    }

      void setCapacity(int c)
      {capacity = c;}

      int getCapacity
      {return capacity;}

      //override printInfo()
      printInfo()
      {

	cout<<"The name of the ship is "<<Ship::printInfo()<<endl;
        endl;
        cout<<"The capacity of the ship is "<<capacity<<endl;
      }

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.

cargoShip myCargoShip ("USS Enterprise", "1985", 230456)

printInfo().myCargoShip

Is there something or things I'm doing wrong?

Thanks.

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 the first 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.

Thanks for the suggestions, normally I do that but with the .h files I'm using dev c++ so when I go to compile to isolate the errors to just that file and work on it before going to the next one, it will not compile. In class, we use UNIX so it lets us compile just the .h file by itself and spits back any errors we get.

So I guess what I'm saying is when I do the assignments at home, I get so overwhelmed by the number of errors I get by the time I run the main.cpp program, I feel totally lost.

Besides the previous errors due to your programming environment as it seems u have some other as well:

In the base class u implemented printInfo:

virtual void printInfo()
  {
    cout<<"The name of the ship is "<<name<<endl;
    endl;
    cout<<"The year the ship was built is "<<year<<endl; 
  };

and then in your derived class:

printInfo()
      {

	cout<<"The name of the ship is "<<Ship::printInfo()<<endl;
        endl;
        cout<<"The capacity of the ship is "<<capacity<<endl;
      }

so when u finally manage to compile, call prinfInfo for the cargoShip and run u will get:

The name of the ship is The name of the ship is Titanic
The year the ship was built is 1934
The capacity of the ship is 4000

but i guess would notice this :D

Right, so the correct way should be

printInfo()
{


Ship::printInfo()<<endl;
cout<<"The capacity of the ship is "<<capacity<<endl;
}

That way, it will output whatever was in the original virtual function in the base class, plus the capacity in the inherited class of cargoShip.

Question, when I call printInfo from ship, is there a way to have it just display the name of the ship is xxx and surpress the year it was built in statement if they are both inside the same virtual function?

Thanks.


Question, when I call printInfo from ship, is there a way to have it just display the name of the ship is xxx and surpress the year it was built in statement if they are both inside the same virtual function?

Thanks.

Could u explain what exactly is that u want to succed, because I didnt got it....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.