#include
#include "stdafx.h"
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop /
class cartp
{
public:
virtual char getcar()=0;
};
class BMW:public cartp
{
public:
char* getcar()
{
return "BMW";
}
};
class BENZ:public cartp
{
public:
char* getcar()
{
return "BENZ";
}
};
class carfactory
{
public:
static cartp* getcarname(char* name)
{
if(name=="BMW")
{
return new BMW();
}
else if(name=="BENZ")
{
return new BENZ();
}
else
{
return NULL;
}
}
};
int main(int argc, char** argv)
{
char name[20]={0};
char* car=name;
cout<<"input cartype:";
cin>>name;
cartp* cc=carfactory::getcarname(car);
cout<<"type is"<getcar()<<endl;
return 0;
}
class cartp
{
public:
virtual char* getcar()=0;
};
class BMW:public cartp
{
public:
char* getcar()
{
return "BMW";
}
};
class BENZ:public cartp
{
public:
char* getcar()
{
return "BENZ";
}
};
class carfactory
{
public:
static cartp* getcarname(char* name)
{
if(strcmp(name,"BMW") == 0)
{
return new BMW();
}
else if(strcmp(name,"BENZ") == 0)
{
return new BENZ();
}
else
{
return NULL;
}
}
};
int main(int argc, char** argv)
{
char name[20] = {0};
char* car = name;
cout<<"input cartype:";
cin>>name;
cartp* cc = carfactory::getcarname(car);
cout<<"type is"<<cc->getcar()<<endl;
return 0;
}