Thanks for the assistance, guys. Yeah, I probably will use a container class and a vector rather than just an array, but since I'm rather novice at this, I'm going for "one concept at a time". Or rather trying to anyway.
Running in a test environment, I found that if a sub-class had a function that the super-class did not, I had to define a virtual function in the super class, even if it was just there to take up space.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
//PointerTest.h
#include <iostream>
using namespace std;
class superClass{
public:
superClass();
superClass(int y);
virtual void getInt() { }//This HAD to be included for it to work
protected:
int x;
};
class subClass : public superClass{
public:
subClass();
subClass(int b);
void getInt();
private:
int a;
};
void subClass::getInt(){
cout << "x == " << x << endl;
cout << "a == " << a << endl;
}
superClass::superClass(){
cout << "superClass Default Constructor\n";
x = 0;
}
superClass::superClass(int y){
cout << "superClass Constructor\n";
x = y;
}
subClass::subClass(){
cout << "subClass Default Constructor\n";
a = 0;
x = 0;
}
|
1 2 3 4 5 6 7 8 9 10
|
//main.cpp
#include "PointerTest.h"
int main(){
superClass* superX = createSub(45);
superX->getInt();
system("PAUSE");
return 0;
}
|
I couldn't, for example, just write a "getA()" function in the subClass and use it; my compiler complained that superClass didn't have the member function. However, writing in the virtual func made it work perfectly:
Output
superClass Default Constructor
subClass Constructor
a=45, x=90
Leaving Constructor
x == 90
a == 45
Press any key to continue . . .
End Output
Again, this hacked together bit of code works as intended, but does that mean that for every sub class member function, I'm going to have to make a virtual func in the superClass that essentially does nothing but take up space? I mean, I guess I could use it for debugging or error catching, but other than that it'd be useless.