#include <iostream>
#include <string>
using namespace std;
class Part{
public:
std::string spec;
Part(std::string str){
this->spec = str;
}
std::string getSpec(){
return spec;
}
};
class Car{
public:
Part getEngine();
Part getWheels();
Part getBody();
};
class Benz:public Car{
public:
Part getEngine(){
return Part("Benz Engine");
}
Part getWheels(){
return Part("Benz Wheels");
}
Part getBody(){
return Part("Benz Body");
}
};
class Audi:public Car{
public:
Part getEngine(){
return Part("Audi Engine");
}
Part getWheels(){
return Part("Audi Wheels");
}
Part getBody(){
return Part("Audi Body");
}
};
class CarFactory{
public:
Car *pcar;
Car *getCar(std::string carType){
if (carType.compare("Benz") == 0){
pcar = new Benz();
}else if (carType.compare("Audi") == 0){
pcar = new Audi();
}
return pcar;
}
};
int main(){
CarFactory *cf = new CarFactory();
Car *mycar = cf->getCar("Benz");
Part myCarBody = mycar->getBody();
cout <<myCarBody.getSpec() <<endl;
//cout << mycar->getBody().getSpec() <<endl;
}
In the above code getting, undefined reference to object error at line Part myCarBody = mycar->getBody(); line of main function. Can you please help me for fixing this and also please explain the difference between Car *mycar and Car mycar. Which one to choose when?
Thanks for the help.