This code is written for TURBO C++ 3.0 version
Code:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#define EVENTS 5
#define OBJ_FUN 2
class store
{
int qty;
public: int Fn_stor()
{
cout<<"I'm in Fn_stor function of class store \n";
return 5;
}
int Fn_quant()
{
cout<<"I'm in Fn_quant function of class store \n";
return 0;
}
int Fn_get()
{
cout<<"I'm in Fn_get function of class store \n";
return 6;
}
};
class purc
{
public: int Fn_pur()
{
cout<<"I'm in Fn_pur function of class purc \n";
return 7;
}
int Fn_order()
{
cout<<"I'm in Fn_order function of class purc \n";
return 8;
}
int Fn_amount()
{
cout<<"I'm in Fn_amount function of class purc \n";
return 9;
}
};
class manu
{
public: int Fn_man()
{
cout<<"I'm in Fn_manu function of class manu \n";
return 2;
}
int Fn_Time()
{
cout<<"I'm in Fn_Time function of class manu \n";
return 3;
}
int Fn_work()
{
cout<<"I'm in Fn_work function of class manu \n";
return 4;
}
};
class Generic : public store, public purc, public manu
{
};
void main()
{
store str;
purc prc;
manu mnf;
int class_id,Fn_id;
int (Generic::*pt[3][3])() = {
(&store::Fn_stor,&store::Fn_quant,&store::Fn_get),
(&purc::Fn_pur,&purc::Fn_amount,&purc::Fn_order),
(&manu::Fn_man,&manu::Fn_Time,&manu::Fn_work)
};
clrscr();
enum { C_STOR,C_PURC,C_MANF };
enum { Fn1,Fn2,Fn3 };
void *obj_ptr[] = { &str,&prc,&mnf };
int seq[EVENTS][OBJ_FUN] = {
{C_STOR,Fn2},
{C_PURC,Fn1},
{C_STOR,Fn3},
{C_MANF,Fn1},
{C_PURC,Fn2}
};
for(int counter=0;counter<EVENTS;counter++)
{
class_id = seq[counter][0];
Fn_id = seq[counter][1];
((Generic*)obj_ptr[class_id]->*pt[class_id][Fn_id])();
}
getch();
}
Here, all function are of return type int and I have created class Generic for type casting of object at run time , because I will be not knowing which object user is going to call. Like this if the functions are of different return types then how I can type cast it at run time ?
Vinod !!