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
|
#include <iostream>
#include <vector>
#include <string>
class CForme
{
public :
CForme(){};
CForme (CForme &f)
{
std::vector<CForme *>::iterator it =f.m_composite.begin();
std::vector<CForme *>::iterator itend =f.m_composite.end();
while(it!=itend)
{
this->AddComposition(**it);
++it;
}
};
~CForme()
{
std::vector<CForme *>::iterator it =m_composite.begin();
std::vector<CForme *>::iterator itend =m_composite.end();
while(it!=itend)
{
delete (*it);
++it;
}
m_composite.clear();
}
virtual CForme* GetClone() {return new CForme(*this);};
virtual std::string GetName() {return "CForme";}
void print(int niveau =0)
{
for(int i=0;i<niveau;++i) std::cout<<" ";
std::cout<<GetName()<<std::endl;
std::vector<CForme *>::iterator it =m_composite.begin();
std::vector<CForme *>::iterator itend =m_composite.end();
while(it!=itend)
{
(*it)->print(niveau+1);
++it;
}
}
void AddComposition(CForme &f) {m_composite.push_back(f.GetClone());}
private :
std::vector<CForme *> m_composite;
};
class A : public CForme
{
public :
A(){};
A (A &f): CForme(f){};
virtual std::string GetName() {return "A";}
virtual CForme* GetClone() {return new A(*this);};
};
class B : public CForme
{
public :
B(){}
B (B &f): CForme(f) {};
virtual std::string GetName() {return "B";}
virtual CForme* GetClone() {return new B(*this);};
};
class C : public CForme
{
public :
C(){}
C (C &f): CForme(f) {};
virtual std::string GetName() {return "C";}
virtual CForme* GetClone() {return new C(*this);};
};
int main(int argc, char** argv)
{
A a;
B b;
C c;
c.AddComposition(a);
c.AddComposition(b);
CForme F1;
F1.AddComposition(a);
CForme F2;
F2.AddComposition(b);
CForme F3;
F3.AddComposition(a);
F3.AddComposition(b);
F3.AddComposition(c);
c.AddComposition(c);
CForme F4;
F4.AddComposition(c);
F1.print();
std::cout<<std::endl;
F2.print();
std::cout<<std::endl;
F3.print();
std::cout<<std::endl;
F4.print();
std::cout<<std::endl;
return 0;
} |
Partager