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
|
#include<iostream>
#include<vector>
class AbstractComponent
{
public:
virtual void display();
//virtual AbstractComponent() {}
};
template<class T>
class Component : public AbstractComponent
{
public:
T v1;
T v2;
T v3;
void display() {std::cout<<'(' << v1 <<", "<<v2<<", "<<v3<<")"<<std::endl;}
Component(T v1_, T v2_, T v3_) : v1(v1_), v2(v2_), v3(v3_) {}
};
int main()
{
Component<int> c1(3,4,4);
Component<int> c2(4,5,6);
Component<char> c3('x', 'y', 'z');
Component<double> c4(3.3, 6.2, 4.7);
std::vector<AbstractComponent*> V;
V.push_back(&c1);
V.push_back(&c2);
V.push_back(&c3);
V.push_back(&c4);
for (int i=0; i<V.size(); i++)
V[i]->display();
return 0;
}
Thank you in advance for your kind help.
My best regards, Giuseppe
|