Code getting Crashed( C++)
#include<iostream.h>
#include<memory.h>
#include<string.h>
// product
class Pizza
{
private :
std::string Topping;
std::string Sauce;
std::string Dough;
public :
Pizza(){}
~Pizza(){}
void SetTopping(const std::string t) { Topping = t; }
void SetSauce(const std::string s){ Sauce = s; }
void SetDough(const std::string d){ Dough = d; }
void ShowPizza()
{
std::cout <<"What a Pizza"<<std::endl
<<"Topping is "<<Topping
<<"Sauce is "<<Sauce
<<"Dough is "<<Dough
<<"! ! !"<<'\n';
}
};
// Abstract Builder
class PizzaBuilder
{
protected :
std::auto_ptr<Pizza> pizza;
public :
PizzaBuilder(){}
virtual ~PizzaBuilder(){}
std::auto_ptr<Pizza> GetPizza(){return pizza;}
virtual void BuildTopping() = 0;
virtual void BuildSauce() = 0;
virtual void BuildDough() = 0;
std::auto_ptr<Pizza> CreateNew_PizzaProduct()
{ pizza.reset(new Pizza()); }
/*********** code getting crashed here ***************/
};
// concrete Pizza Builder 1
class Hawian_PizzaBuilder : public PizzaBuilder
{
public :
Hawian_PizzaBuilder():PizzaBuilder(){ }
~Hawian_PizzaBuilder(){ }
void BuildTopping() { pizza->SetTopping(" Hawian_PizzaBuilder
Topping ");}
void BuildSauce() { pizza->SetSauce("Hawian_PizzaBuilder
Sauce ");}
void BuildDough() { pizza->SetDough("Hawian_PizzaBuilder
Dough ");}
};
// concrete Pizza Builder 2
class Spicy_PizzaBuilder : public PizzaBuilder
{
public :
Spicy_PizzaBuilder():PizzaBuilder(){}
~Spicy_PizzaBuilder(){}
void BuildTopping() { pizza->SetTopping("Spicy_PizzaBuilder Topping
"); }
void BuildSauce() { pizza->SetSauce("Spicy_PizzaBuilder
Sauce "); }
void BuildDough() { pizza->SetDough("Spicy_PizzaBuilder
Dough "); }
};
// Director
class waiter
{
private :
PizzaBuilder * pizza_builder;
public :
waiter(): pizza_builder(NULL){}
~waiter(){}
void SetPizzaBuilder(PizzaBuilder * b){ pizza_builder = b ;}
std::auto_ptr<Pizza> GetPizza(){ return pizza_builder-
GetPizza();}
void ConstructPizza()
{
pizza_builder->CreateNew_PizzaProduct();
pizza_builder->BuildDough();
pizza_builder->BuildSauce();
pizza_builder->BuildTopping();
}
};
// user
int main()
{
waiter waiter_1;
Spicy_PizzaBuilder spicyPizzaBuilder;
waiter_1.SetPizzaBuilder(&spicyPizzaBuilder);
waiter_1.ConstructPizza(); // code getting Crashed here
std::auto_ptr<Pizza> pizza = waiter_1.GetPizza();
pizza->ShowPizza();
return 0;
}