I am learning polymorphism and I am familiar with php.
I came across this excellent example from https://stackoverflow.com/a/749738/80353. reproduced below.
How
As someone not familiar with C++, create a index.cpp file and fill it up with the following
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
class Animal {
public:
std::string name;
Animal (const std::string& givenName) : name(givenName) {}
virtual string speak () = 0;
virtual ~Animal() {}
};
class Dog: public Animal {
public:
Dog (const std::string& givenName) : Animal (givenName) {
}
string speak ()
{ return "Woof, woof!"; }
};
class Cat: public Animal {
public:
Cat (const std::string& givenName) : Animal (givenName) {
}
string speak ()
{ return "Meow..."; }
};
int main() {
std::vector<std::unique_ptr<Animal>> animals;
animals.push_back( std::unique_ptr<Animal>(new Dog("Skip")) );
animals.push_back( std::unique_ptr<Animal>(new Cat("Snowball")) );
for( int i = 0; i< animals.size(); ++i ) {
cout << animals[i]->name << " says: " << animals[i]->speak() << endl;
}
}
afterwards, compile the index.cpp file with the following command
c++ index.cpp -std=c++11 -stdlib=libc++
you need that because of the use of smart pointers unique_ptr in the code.
finally you can execute the compiled executable output file which should be a.out
./a.out
Well, you can use a pointer with abstract classes using pure virtual methods like so:
class Polygon {
public:
virtual void setValue(int k) = 0; // Declaring our pure virtual method.
};
class Rect : public Polygon {
public:
virtual void setValue(int k); = 0; // Declaring the pure virtual method again.
};
Rect::setValue(int k) { // You must create a setValue() method for every class, since we made a pure virtual method. This is the Polymorphic part.
// Code in here that setValue() method executes.
int foo = a;
std::cout << foo << std::endl;
}
Now you can access the methods declaring object pointers.
int main() {
Polygon* pSomePolygon = nullptr; // Assuming using C++11.
Rect* pRect = new Rect; // Declare our object.
pSomePolygon = pRect; // Point our pointer to object 'pRect'.
pSomePolygon->setValue(18); // Pointer accesses pRect and uses the pure virtual method.
delete pRect; // Clean up!
return 0;
}
Since you only asked about storing them, I'll omit the implementation of Animal
, Dog
and Cat
.
vector< shared_ptr<Animal> > animals;
animals.push_back( new Dog("Skip") );
animals.push_back( new Cat("Snowball") );
for( size_t i = 0; i< animals.size(); ++i )
cout << animals[i]->name << " says: " << animals[i]->speak() << endl;
Essentially you need to store pointers to the objects. Put simply, shared_ptr<T>
is a class that stores a pointer and deletes it when it is no longer referenced.