Valhalla Legends Forums Archive | C/C++ Programming | Polymorphism?

AuthorMessageTime
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

[code]
// 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"); }
}[/code]

My usage:
[code]
// 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;
}[/code]

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.
April 26, 2004, 7:01 PM
K
You're missing semicolons at the end of the class definitions.

[code]
class automobile {

}; // semicolon here
[/code]

Edit: second problem:

[code]
cTrans = new car;
car->go();
[/code]

car is the class, not the object ;)

[code]
cTrans = new car;
cTrans->start();
[/code]


Edit! Third problem:

you need to put parentheses on all function calls.
[code]
cTrans->start();
cTrans->move();
[/code]

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.
April 26, 2004, 7:32 PM
Eli_1
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 [code]
car *cCars;
// and
bus *cBuses;[/code]
and then doing it like that, but that's ugly. :-[
April 26, 2004, 7:59 PM

Search