Having a problem with virtual methods

Started by
4 comments, last by Jeff D 22 years, 6 months ago
I got this code here that is giving me an unexpected end of file found error I cant seem to find a reason why. I am gonna use the source tags for the first time so I hope it works:
  

#include <iostream.h>
#include <stdlib.h>

class mammal
{

   public:
      mammal() { cout << "Mammal constructor \n";
                 setage(); };
      mammal(int age) { cout << "Mammal constructor \n";
                    setage(age); }; 
      virtual ~mammal() { cout << "Mammal destructor \n"; };
      virtual void speak() const { cout << "Mammal speak! \n"; };
      void move() { cout << "Mammal Moves! \n"; };
      void setage() { itsage = 1; };
      void setage(int age) {itsage = age; };
      void getage() { cout << itsage << endl; };
   
   protected:
      int itsage;

};

class dog : public mammal
{

   public:
      dog() { cout << "Dog constructor \n"; }; 
      virtual ~dog() { cout << "Dog destructor \n"; ); 
      void speak() { cout << "Woof! \n"; };
      void move() { cout << "dog moves! \n"; };

};

int main()
{
   mammal* fido = new dog;
   fido -> getage();
   fido -> speak();
   fido -> move();
   system("PAUSE");
   return 0;

}

  
I hope that works. I know that the program is quite juvenile but Im trying my hardest to create a progrm in a book without looking at it to seem if I actually can get the syntax right. But the book looks like this to and I still get this error. Thx, Jeff Desrosiers
Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton
Advertisement
the 2 includes are actually on different lines not sure why that happened.


Thx,

Jeff Desrosiers
Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton
Check out this line:

virtual ~dog() { cout << "Dog destructor \n"; );

See anything wrong with it?

--Jeff

LOL thx a lot man.

Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton
I might add that you have a bunch of redundant statement terminators (normal people call them "semicolons" ). When you do:
int setage() {itsage = 1;}; 

the last semicolon is unnecessary. (Just information.)
Ah I see I tried this and it still worked thx a lot.



Thx,

Jeff Desrosiers
Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton

This topic is closed to new replies.

Advertisement