Polymorphism/classes
Hello, I got the following code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std ;
// Function Prototypes
void pause(void);
/** defines an abstract class */
class Jukebox {
public:
virtual void play() { cout << "Record unknown\n" ; }
} ;
/** defining subclasses */
class NewAge: public Jukebox {
public:
void play() { cout << "NewAge is playing\n\n" ; }
} ;
class RandB: public Jukebox {
public:
void play() { cout << "R&B is playing\n\n" ; }
} ;
class BlueGrass: public Jukebox {
public:
void play() { cout << "Blue Grass is playing\n\n" ; }
} ;
/** An entry point for program execution */
int main() {
Jukebox *pointer[3] = {new NewAge(), new RandB(), new BlueGrass()} ;
// set seed for the random generator
srand( (unsigned)time(NULL) ) ;
// runs a test
for (int i=0; i < 10; i++) {
int index = rand() % 3 ;
pointer[index]->play() ; // polymorphic funcation call
}
pause();
return 0;
}
I want to write my own set of classes with polymorphism support for a problem involving automobiles and different model of cars, such as, "Ford Mustang" and so on.
Do I just have to substitute "Jukebox", "New Age" etc.? I guess Toyota would be the main class and Yaris the subclass?
XodoX
Junior Poster in Training
82 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
I would suggest the following :
1) create an abstract base class that has the similar properties for
all class
2) Derive a base class from the abstract, maybe a CarType class
3) Derive a Toyota, Honda, Lexus ... from CarType
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
Hello ,
you want polymorphism for car,
for that u can create a base class like name CAR
i.e.
class car{
}
you can define some abstart or virtual method in this base class
like color of car,price or model etc,which will be common for any class.
so if u create such virtual method in base class class,
all child class need to have this method ,
so some method which u think that, that should for all car can be define in base class.
you can inherit this class in other classes like ford ,toyota....
and ford and toyota wll be inherited in the cars like carolla and iCon and all..........
u can define some unique property of each car in that specific car class i.e. you can define some specific feature of iCon car in to iCon class and common feature of FORD company can be there in ford class and common for all car will be in CAR class,the parent class for all other classes
crazyboy
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 6
Solved Threads: 0
I still don't quite get it. Jukebox would be "cartype'. New Age, RandB and Bluegrass would be the brands...? BMW, Ford etc.
XodoX
Junior Poster in Training
82 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
Now, that's what I did. It lists each type now multiple times... didn't do that before...
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std ;
// Function Prototypes
void pause(void);
class Car {
public:
virtual void play() { cout << "Car unknown\n" ; }
} ;
class Toyota: public Car {
public:
void play() { cout << "Toyota Prius\n" ; }
} ;
class Ford: public Car {
public:
void play() { cout << "Ford Mustang\n" ; }
} ;
class Volkswagen: public Car {
public:
void play() { cout << "Volkswagen Jetta\n" ; }
} ;
int main() {
Car *pointer[3] = {new Toyota(), new Ford(), new Volkswagen()} ;
// set seed for the random generator
srand( (unsigned)time(NULL) ) ;
// runs a test
for (int i=0; i < 10; i++) {
int index = rand() % 3 ;
pointer[index]->play() ;
}
system("pause");
return 0;
}
At least it's a lsit and a random output. Is that kinda it what you guys meant?
XodoX
Junior Poster in Training
82 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
Then,
I think you r not so clear about concept of Polymophism.
if yes,
then you should be first clear about your concepts ,
for that you can refer books.
crazyboy
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 6
Solved Threads: 0