Ok, thank you both. I had not realised I could pointers to the base function on the derived ones when I was reading about it. Here is my test code (feel free to suggest different approaches):
Code:
int main()
{
int choice=0;
cout<<"chose the model:";
cin>>choice;
base* model_point=NULL; //pointer to a "base-type" object
switch(choice){ //both models are "base-type"
case(1):model_point=new model_1(12);//and can be called by a pointer to them
break;
case(2):model_point=new model_2;
break;
default:model_point=new base;
}
model_point->vfunction1(); //note to self: go refresh on pointers
model_point->nonvirtual();
cout<<endl<<"End of program."<<endl;
return 0;
}
Code:
class base{
public:
int choice;
base();
base(int);
static int counter;
virtual void vfunction1();
void nonvirtual();
};
/*******************************************************************/
class model_1:public base{
public:
model_1();
model_1(int);
void vfunction1();
};
/*******************************************************************/
class model_2:public base{
public:
model_2();
void vfunction1();
void extra_function();
};
Code:
int base::counter=0;
base::base(){
cout<<"base constructor"<<endl;
counter++;
}
base::base(int base_arg){
cout<<"base was called with an argument."<<endl;
}
void base::vfunction1(){
cout<<"vfunction1 was called from the base."<<endl;
choice=0;
}
void base::nonvirtual(){
cout<<"Non-virtual called from base."<<endl;
}
/*******************************************************************/
model_1::model_1(){
cout<<"model_1 default constructor."<<endl;
choice=11;
}
model_1::model_1(int entry){
cout<<"entry="<<entry<<endl;
}
void model_1::vfunction1(){
cout<<"vfunction1 call by model_1"<<endl;
}
/*******************************************************************/
model_2::model_2(){
cout<<"model_2 default constructor."<<endl;
choice=22;
}
void model_2::vfunction1(){
cout<<"vfunction1 call by model_2"<<endl;
}
void model_2::extra_function(){
cout<<"model_2 extra function."<<endl;
}
I did notice (amongst other things) that the base constructor is always called with no arguments when declaring one of the derived classes (even if the derived class was called with an argument). Wouldn't it be more intuitive to allow "virtual constructors" as well? is there a way to do that? just wondering...
anyway, thanks again for the help, now I just have to restructure a 4k line program that I didn't write so it can be reasonably easy to work on.