View difference between Paste ID: jKp7t1aT and 5tfNUTu6
SHOW: | | - or go back to the newest paste.
1
	class Shape;
2
3
	typedef std::unique_ptr<Shape> shape_ptr;
4
5
	class Shape{
6
7
		public:
8
			Shape(){};
9
			virtual ~Shape(){};
10
11
			virtual void draw() const = 0;
12
			virtual float area() const = 0;
13
14
			virtual shape_ptr clone() const = 0;
15
			virtual shape_ptr create() const = 0;
16
	};
17
18
	class Rectangle:public Shape{
19
		public:
20
21
			Rectangle(int height=0, int width=0):m_Height(height),m_Width(width){};
22
			~Rectangle(){};
23
24
			virtual void draw() const;
25
			virtual float area() const;
26
27
			virtual shape_ptr clone() const{ return shape_ptr(new Rectangle(*this)); };
28
			virtual shape_ptr create() const{ return shape_ptr(new Rectangle()); };
29
30
		private:
31
			int m_Height;
32
			int m_Width;
33
	};
34
35
36
	class Circle:public Shape{
37
		public:
38
39
			Circle(float radius=0):m_Radius(radius){};
40
			~Circle(){};
41
42
			virtual void draw() const;
43
			virtual float area() const;
44
45
			virtual shape_ptr clone() const{ return shape_ptr(new Circle(*this)); };
46
			virtual shape_ptr create() const{ return shape_ptr(new Circle()); };
47
48
		private:
49
			float m_Radius;
50
	};
51
52
	class ShapeContainer{
53
54
		public:
55
56
			typedef std::unique_ptr<Shape> ShapePtr;
57
			typedef std::vector<ShapePtr> ShapePtrContainer;
58
59
			ShapeContainer(){};
60
			ShapeContainer( ShapeContainer && );
61
			ShapeContainer( const ShapeContainer & );
62
			~ShapeContainer(){};
63
64
			const ShapePtr & at(int ) const;
65
66
			ShapeContainer & operator = (ShapeContainer&&);
67
			int size() const{ return static_cast<int>(m_vect.size()); };
68
69
			friend ShapeContainer createBasicShapes();
70
			friend ShapeContainer createComplexShapes();
71
72
			friend void swap(ShapeContainer& x, ShapeContainer& y) {x.m_vect.swap(y.m_vect);}
73
74
		private:
75
76
			ShapePtrContainer m_vect;
77
	};
78
79-
	class Foo{
79+
	class FooObj{
80
81
		public:
82
83-
			Foo(){};
83+
			FooObj(){};
84-
			Foo( const ShapeContainer & );
84+
			FooObj ( FooObj && );
85-
			~Foo(){};
85+
			FooObj( const ShapeContainer & );
86
			FooObj ( const FooObj & );
87
			~FooObj(){};
88
89
			FooObj & operator = (FooObj && );
90
91
			void drawShapes() const;
92
93
94
		private:
95
			ShapeContainer m_container;
96
	};
97-
		std::cout << "draw rectangle!\n";
97+
98
	class FooRef{
99
100
		public:
101
102-
		std::cout << "this is the Rectangle's area: " << area << "\n";
102+
			typedef std::unique_ptr<ShapeContainer> uptr;
103
104
			FooRef(){};
105
			FooRef( const ShapeContainer & );
106
			FooRef( const FooRef & );
107-
		std::cout << "draw circle!\n";
107+
			~FooRef(){};
108
109
			FooRef & operator = (const FooRef & );
110
111
			void drawShapes() const;
112-
		std::cout << "this is the Circle's area: " << area << "\n";
112+
113
		private:
114
			uptr m_container;
115
116
	};
117
118
	void Rectangle::draw() const{
119
		std::cout << "draw rectangle at address: " << this << std::endl;
120
	}
121
 
122
	float Rectangle::area() const{
123
		float area = static_cast<float>(m_Height*m_Width);
124
		std::cout << "this is the Rectangle's area: " << area << std::endl;
125
		return area;
126
	}
127
128
	void Circle::draw() const{
129
		std::cout << "draw circle at address: " << this << std::endl;
130
	}
131
 
132
	float Circle::area() const{
133
		float area = static_cast<float>(m_Radius*2*3.1415);
134
		std::cout << "this is the Circle's area: " << area << std::endl;
135
		return area;
136
	}
137
138
	ShapeContainer::ShapeContainer(ShapeContainer && other)
139
		:m_vect(std::move(other.m_vect))
140
	{
141
	
142
	}
143
144
	ShapeContainer & ShapeContainer::operator = (ShapeContainer&& other){
145
		m_vect = std::move(other.m_vect);
146
		return (*this);
147
	}
148
149
	ShapeContainer::ShapeContainer( const ShapeContainer & other ){
150-
	Foo::Foo( const ShapeContainer & cont )
150+
151
			m_vect.push_back(other.m_vect[i]->clone());
152
		}
153
	}
154
155
	ShapeContainer createBasicShapes(){
156-
	void Foo::drawShapes() const{
156+
157
		cont.m_vect.push_back(ShapeContainer::ShapePtr(new Circle()));
158
		cont.m_vect.push_back(ShapeContainer::ShapePtr(new Rectangle()));
159
		return cont;
160
	}
161
162
	ShapeContainer createComplexShapes(){
163
		ShapeContainer cont;
164-
		Foo myfoo(createBasicShapes());
164+
165
		return cont;
166
	}
167
168-
168+
169-
169+
170
	}
171
172
	FooObj::FooObj( const ShapeContainer & cont )
173
		:m_container(cont)
174
	{
175
	
176
	}
177
178
	FooObj::FooObj( const FooObj & other)
179
		:m_container(other.m_container)
180
	{
181
	
182
	}
183
184
	FooObj & FooObj::operator = ( FooObj && other ){
185
		m_container = std::move(other.m_container);
186
		return (*this);
187
	}
188
189
	void FooObj::drawShapes() const{
190
	
191
		for(int i=0; i < m_container.size(); i++){
192
			m_container.at(i)->draw();
193
		}
194
	}
195
196
	FooRef::FooRef( const ShapeContainer & shapec )
197
		:m_container( uptr(new ShapeContainer(shapec)))
198
	{
199
	
200
	}
201
202
	void FooRef::drawShapes() const{
203
	
204
		for(int i=0; i < m_container->size(); i++){
205
			m_container->at(i)->draw();
206
		}
207
	}
208
209
	int main(int argc, char ** argv){
210
211
		ShapeContainer mycontainer = createBasicShapes();
212
		cout << "hi" << endl;
213
214
		FooObj myfoo(createBasicShapes());
215
		myfoo.drawShapes();
216
217
		FooObj myfoo2 = myfoo;
218
		myfoo2.drawShapes();
219
220
		return (EXIT_SUCCESS);
221
222
	}