• Welcome to Valhalla Legends Archive.
 

Polymorphism?

Started by Eli_1, April 26, 2004, 02:01 PM

Previous topic - Next topic

Eli_1

Trying to move on to polymorphism now.  :-\

I'm trying to make a hierarchy of abstract data types (I think it's called). I get 11 errors trying to use my class though.  :-X


// plytst.h = Polytest.h
// Testing polymorphism??
// I use question marks because I'm
// not even sure if this is true polymorphism...

#include <stdio.h>

class transportation {
public:
};

class automobile : public transportation {
public:
   virtual void start() = 0;
   virtual void move() = 0;
   virtual void stop() = 0;
   virtual void turnoff() = 0;
   virtual void addgas() = 0;
   virtual void explode() = 0;
}

class car : public automobile {
public:
   virtual void start() { printf("Brrroooom! (car started)\n"); }
   virtual void move() { printf("Brroooom! (car is moving)\n"); }
   virtual void stop() { printf("Screchh! (car stopped moving)\n"); }
   virtual void turnoff() { printf("Putt Puttt! (car stopped)\n"); }
   virtual void addgas() { printf("Guzzle! (car gassed\n"); }
   virtual void explode() { printf("Boooom! (car exploded)\n"); }
}

class bus : public automobile {
public:
   virtual void start() { printf("Brrrooom! (bus started)\n"); }
   virtual void move() { printf("Brrooom! (bus is moving)\n"); }
   virtual void stop() { printf("Screchh! (bus stopped moving)\n"); }
   virtual void turnoff() { printf("Putt Puttt! (bus stopped)\n"); }
   virtual void addgas() { printf("Guzzle! (bus gassed)\n"); }
   virtual void explode() { printf("Booom! (bus exploded)\n"); }
}


My usage:

// main.cpp
// polymorphism test
// work damnit! ><

#include <plytst.h> // My transportation classes
transportation *cTrans = 0;

int main() {
   cTrans = new car;
   car->start;
   car->move;
   car->stop;
  car->turnoff;
   car->addgas;
   car->start;
   car->stop;
   car->explode;
   delete cTrans;
   return 0;
}


All the errors are in main.cpp. I get invailed syntax with transportation *cTrans = 0. And a majority of the rest are improper use of car typedef.

K

#1
You're missing semicolons at the end of the class definitions.


class automobile {

}; // semicolon here


Edit: second problem:


cTrans = new car;
car->go();


car is the class, not the object  ;)


cTrans = new car;
cTrans->start();



Edit! Third problem:

you need to put parentheses on all function calls.

cTrans->start();
cTrans->move();


Edit Again!

your transportation class doesn't have any member functions like start() or move(), so you can't create a transportation object and expect to call those functions.  make cTrans of type automobile.

Eli_1

#2
Nevermind, those were all simple retarded mistakes, I got it working. Thanks a lot K, I actually think I understand this.  ;D

Now say I wanted to add grayhound buses to the ADT bus (I changed it some so now car and bus are adts). How could I have the buses have a unloadpassenger function and a loadpassenger function for just buses and not cars?

Only way I can think of is doing
car *cCars;
// and
bus *cBuses;

and then doing it like that, but that's ugly.  :-[