-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCLASS.h
More file actions
180 lines (143 loc) · 3.44 KB
/
CLASS.h
File metadata and controls
180 lines (143 loc) · 3.44 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// Created by fatima_msd on 11/11/18.
//
#ifndef HW3_CLASS_H
#define HW3_CLASS_H
#endif //HW3_CLASS_H
#include <string>
#include <list>
#include <vector>
#include <iostream>
using namespace std;
class Exception{
public:
Exception(const string& msg) : msg_(msg){}
string getMessage() const {return(msg_);}
private:
string msg_;
}; //TODO: Give exceptions a better structure. search google (optional)
class list_shape;
class Rectangle;
class Circle;
class Square;
class shape{
public:
virtual float Area()=0;
virtual void draw()=0;
//virtual bool operator ==(shape * a)=0;
};
class Circle:public shape{
int r;
public:
Circle(int r1=0):r(r1){}
void draw(){
cout << "Shape:Circle --> Radius : "<<r<<" , Area :"<< Area()<<endl;
}
float Area(){
return 3.14*r*r;
}
bool operator==(Circle *cr){
return r==cr->r;
}
};
class Rectangle:public shape{
int a,b;
friend Square;
public:
Rectangle(int a1=0,int b1=0):a(a1),b(b1){}
void draw(){
cout << "Shape:Rectangle --> length : "<<a<<" , width : "<<b<<" , Area :"<< Area()<<endl;
}
float Area(){
return a*b;
}
bool operator==(Rectangle *re){
return a==re->a && a==re->b;
}
};
class Square:public Rectangle{
public:
Square(int a1=0){
a=a1;
b=a1;
}
void draw(){
cout << "Shape:Square --> length : "<<a<<" , Area :"<< Area()<<endl;
}
float Area(){
return a*a;
}
bool operator==(Square *sq){
return a==sq->a;
}
};
class list_shape{
vector<shape *> SHAPE;
public:
void add(shape* s){
SHAPE.push_back(s);
}
shape * find(float AREA){//find area
for(auto sh = SHAPE.begin(); sh != SHAPE.end(); sh++){
if((*sh)->Area()==AREA){
return *sh;
}
}
Exception ex("not found Area");
throw ex;
}
int getCount() const {return SHAPE.size();}
shape * GetItem(int index){
if (index < 0 || (size_t) index >= SHAPE.size()){
Exception ex("index is incorrect");
throw ex;
}
return SHAPE[index];
}
void del(shape* s){
for(auto sh = SHAPE.begin(); sh != SHAPE.end(); sh++){
if(*(sh)==s){
SHAPE.erase(sh);
return;
}
}
Exception ex("not found shape");
throw ex;
}
void remove(int index){
if (index < 0 || (size_t) index >= SHAPE.size()){
Exception ex("index is incorrect");
throw ex;
}
auto sh = SHAPE.begin()+index;
SHAPE.erase(sh);
}
void insert(shape* s, int index) {
if (index < 0){
Exception ex("index is incorrect");
throw ex;
}
if ((size_t)index >= SHAPE.size())
index = SHAPE.size();
SHAPE.insert(SHAPE.begin() + index, s);
}
void print_list(){
for(int i=0;i<getCount();i++) {
shape *l = GetItem(i);
l->draw();
}
}
void sort(){
for(auto sh = SHAPE.begin(); sh != SHAPE.end(); sh++){
float Arsh=(*sh)->Area();
for(auto sh2 = sh+1; sh2 != SHAPE.end(); sh2++){
if((*sh2)->Area()<Arsh){
shape* s12=*sh2;
*sh2=*sh;
*sh=s12;
Arsh=(*sh)->Area();
}
}
}
}
};