Question relating to error C2243
What does it mean by this:
"Access protection (protected or private) prevented conversion from a pointer to a derived class to a pointer to the base class."
When I try to instantiate an object like this
PizzaStore *nyStore = new NYPizzaStore();
that's when it gives the error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
//
// Sample : Lab 2
//
// Class : CST 276
//
// Author : Robert Halsey
//
// File : Pizza.h
//
#include <iostream>
#include <deque>
#include <string>
using namespace std;
#ifndef Pizza_H
#define Pizza_H
class Pizza
{
protected :
string name;
string dough;
string sauce;
deque<string> toppings;
public :
void prepare() {
cout << "Preparing " + name;
cout << "Tossing dough...";
cout << "Adding sauce...";
cout << "Adding toppings: ";
for (unsigned int i = 0; i < toppings.size(); i++) {
cout << " " + toppings[i];
}
}
void bake() {
cout << "Bake for 25 minutes at 350";
}
void cut() {
cout << "Cutting the pizza into diagonal slices";
}
void box() {
cout << "Place pizza in official box";
}
string getName() {
return name;
}
};
class NYStyleCheesePizza : Pizza {
public :
NYStyleCheesePizza() {
name = "NY Style Sauce and Cheese Pizza";
dough = "Thin Crust Dough";
sauce = "Marinara Sauce";
toppings.push_front("Grated Reggiano Cheese");
}
};
class PizzaStore
{
public :
Pizza createPizza( string type );
Pizza orderPizza( string type ) {
Pizza pizza;
pizza = createPizza(type);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
};
class NYPizzaStore : PizzaStore
{
public :
Pizza *createPizza( string item ) {
if ( item == "cheese" )
{
return new NYStyleCheesePizza();
}
}
};
#endif
|
Use public inheritance.
class NYStyleCheesePizza : public Pizza {
class NYPizzaStore : public PizzaStore
Last edited on
Hahaha wow I am stuuuuuuuuuuuuuuuuuuuuupid. Thanks man.
Hmm after I did that it started giving me a linker error when I tried this
1 2
|
PizzaStore *nyStore = new NYPizzaStore();
Pizza pizza = nyStore->orderPizza("cheese");
|
Last edited on
And the error was...?
Topic archived. No new replies allowed.