#include <iostream>
#include <string>
#include <vector>
#include <memory> // **** added
class Object
{
protected:
std::string name;
int id;
int value;
public:
virtual ~Object() // **** needs to be virtual
{
}
virtual void Display() const // made const-correct
{
}
};
class Weapon : public Object
{
private:
int damage;
float speed;
public:
Weapon(std::string newName, int newValue, int newDamage, float newSpeed)
{
name = newName;
value = newValue;
damage = newDamage;
speed = newSpeed;
}
/****
~Weapon() // **** implicitly overridden
{
}
****/
void Display() const override // ****
{
std::cout << "Name: " << name << std::endl;
std::cout << "Value: " << value << std::endl;
std::cout << "Damage: " << damage << std::endl;
std::cout << "Speed: " << speed << std::endl;
}
};
int main()
{
std::vector< std::shared_ptr<Object> > inventory; // modified
//Weapon BronzeDagger("Bronze Dagger", 5, 1, 1); // ****
//inventory.push_back(BronzeDagger); // **** modified
inventory.push_back( std::make_shared<Weapon>( "Bronze Dagger", 5, 1, 1 ) ) ; // ****
inventory.push_back( std::make_shared<Weapon>( "Golden Sword", 500, 10, 2 ) ) ; // ****
// for(int i = 0; i < inventory.size(); i++) // ****
for( auto p : inventory )
{
//inventory[i].Display(); // ****
p->Display();
std::cout << '\n' ;
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8bWVtb3J5PiAvLyAqKioqIGFkZGVkCgpjbGFzcyBPYmplY3QKewpwcm90ZWN0ZWQ6CiAgICBzdGQ6OnN0cmluZyBuYW1lOwoJaW50IGlkOwoJaW50IHZhbHVlOwoKCnB1YmxpYzoKCXZpcnR1YWwgfk9iamVjdCgpIC8vICoqKiogbmVlZHMgdG8gYmUgdmlydHVhbAoJewoJfQoKCXZpcnR1YWwgdm9pZCBEaXNwbGF5KCkgY29uc3QgLy8gbWFkZSBjb25zdC1jb3JyZWN0Cgl7CgoJfQoKfTsKCmNsYXNzIFdlYXBvbiA6IHB1YmxpYyBPYmplY3QKewpwcml2YXRlOgoJaW50IGRhbWFnZTsKCWZsb2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