So in my code I would like to make an array of classes. This is because I want to change the order with which they do things. However, I need the classes in this array to have a function that is the same name but will do different things, depending on the type of class that is called.
For example, let's say I make an array of Cars. Every car has a function called drive() that determines the speed with which it drives. I make an array of Cars "CarArray[]" In it, I put two types of classes, Hondas and Fords. Both Hondas and toyotas inherit from the base class of Cars. They both have a function called drive(). If I call CarrArray[].drive() and I get a honda, it should out put a different value than if the space were occupied by a Ford.
So, for example, let's say I make a Honda drive at 10 mph and a Ford drive at 30 mph.
My Array looks like this:
CarArray [0] // Honda
CarArray [1] // Ford
CarArray [2] //Honda
If I call CarArray[0].drive() I should get 10 mph
But if I call CarArray[1].drive I should get 30 mph.
Since it is an array. I can manipulate the classes and switch them around so I get different values.
In my program I want to do more than just print out values but I hope you get the idea.
Here is my test code.
#include <iostream>
using namespace std;
class BaseClass{
public:
virtual void OutPut();
protected:
int BaseData;
};
void BaseClass::OutPut(){
cout<< "BaseClass printed something"<<endl;
}
class BaseA: public BaseClass{
public:
virtual void OutPut();
protected:
int BaseData;
};
void BaseA::OutPut(){
cout<< "BaseA printed something"<<endl;
}
class BaseB: public BaseClass{
public:
virtual void OutPut();
protected:
int BaseData;
};
void BaseB::OutPut(){
cout<< "BaseB printed something"<<endl;
}
int main()
{
BaseA Class1;
BaseB Class2;
BaseClass BaseArray[2];
BaseArray[0] = Class1;
BaseArray[1] = Class2;
BaseArray[0].OutPut();
BaseArray[1].OutPut();
return 0;
}