You should change a few things.
You need a type of pointer or reference for polymorphic behaviour.
You need to call the base class constructor.
You need to override your virtual methods.
You should watch out for proper memory management once you rely on pointers.
You should try to be const correct.
Consider this example below:
#include <string>
#include <iostream>
#include <vector>
#include <memory>
#include <sstream>
class Product
{
private:
double Price;
std::string Name;
public:
Product(const std::string& name, double price)
: Price(price),
Name(name)
{
std::cout << "Created " << name << " for $" << price << std::endl;
}
virtual std::string GetName() const
{
return Name;
}
virtual double GetPrice() const
{
return Price;
}
};
class MultiProduct :public Product
{
private:
int Quantity;
public:
MultiProduct(const std::string& name, double price, int quantity)
: Product(name, price),
Quantity(quantity)
{
std::cout << "Created " << quantity << "x " << name << " for $" << price << " each." << std::endl;
}
virtual double GetPrice() const
{
return Product::GetPrice() * Quantity;
}
virtual std::string GetName() const
{
std::stringstream s;
s << Product::GetName();
s << " x";
s << Quantity;
return s.str();
}
};
class ShoppingCart
{
private:
std::vector< std::shared_ptr<Product> > Cart;
public:
void Add( std::shared_ptr<Product> product )
{
Cart.push_back( product );
}
void PrintInvoice() const
{
std::cout << "Printing Invoice:" << std::endl;
for( auto it = Cart.begin() ; it != Cart.end() ; ++it )
{
std::cout << (*it)->GetName() << ": " << (*it)->GetPrice() << std::endl;;
}
}
};
int main()
{
ShoppingCart cart;
cart.Add( std::shared_ptr<Product>( new Product( "cheese", 1.23 ) ) );
cart.Add( std::shared_ptr<Product>( new MultiProduct( "bread", 2.33, 3 ) ) );
cart.Add( std::shared_ptr<Product>( new Product( "butter", 3.21 ) ) );
cart.Add( std::shared_ptr<Product>( new MultiProduct( "milk", 0.55, 5 ) ) );
cart.Add( std::shared_ptr<Product>( new Product( "honey", 5.55 ) ) );
cart.PrintInvoice();
std::cout << "Press any key to continue" << std::endl;
std::cin.get();
return 0;
}