Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main.cpp
- #include <iostream>
- #include "Composite.h"
- using namespace std;
- int main() {
- Composite s = Composite(1,1);
- Composite *enc1 = new Composite(3, 4);
- Composite *enc2 = new Composite(30, 400);
- Composite *enc4 = new Composite(121, 456);
- s.addChild(enc1);
- s.addChild(enc2);
- s.addChild(enc4);
- s.removeChild(enc4);
- s.printChildren();
- return 0;
- }
- // Composite.h
- #ifndef COMPOSITE_H_
- #define COMPOSITE_H_
- #include <list>
- class Composite {
- public:
- Composite(int x, int y);
- int x;
- int y;
- std::list<Composite *> children;
- void addChild(Composite * enc);
- void removeChild(Composite * enc);
- void printChildren();
- void printSelf();
- virtual ~Composite();
- };
- #endif /* COMPOSITE_H_ */
- // Composite.cpp
- #include "Composite.h"
- #include <iostream>
- Composite::Composite(int x , int y) {
- this->x = x;
- this->y = y;
- }
- Composite::~Composite() {
- // TODO Auto-generated destructor stub
- }
- void Composite::addChild(Composite * enc) {
- this->children.push_back(enc);
- }
- void Composite::removeChild(Composite * enc) {
- this->children.remove(enc);
- }
- void Composite::printChildren() {
- using namespace std;
- for (list<Composite*>::iterator it = this->children.begin();
- it != this->children.end(); it++) {
- Composite *enc = *it;
- enc->printSelf();
- }
- }
- void Composite::printSelf() {
- std::cout << this->x << " " << this->y << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement