Skip to content

Commit 734146d

Browse files
committed
auto id generation & bug fixes
1 parent e80c3ce commit 734146d

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

Semester_2/Lab_14/list.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void FigureList::print_all() {
7575
Node* curr = head;
7676
int i = 1;
7777
while (curr) {
78-
cout << "\nitem: " << i++ << endl;
78+
cout << endl << "item: " << i++ << endl;
7979
curr->shape->print();
8080
curr = curr->next;
8181
}

Semester_2/Lab_14/shape.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using namespace std;
66

77
// class Point
88

9-
Point::Point(int x, int y):
9+
Point::Point(double x, double y):
1010
x(x), y(y) {
1111
}
1212

@@ -17,16 +17,21 @@ ostream& operator<<(ostream &out, Point &p) {
1717

1818
// class Shape
1919

20-
Shape::Shape(int id, Point pos):
21-
id(id), pos(pos) {
20+
Shape::Shape(Point pos):
21+
id(next_id()), pos(pos) {
22+
}
23+
24+
unsigned long Shape::next_id() {
25+
static unsigned long next_id = 0;
26+
return next_id++;
2227
}
2328

2429
void Shape::print() {
2530
cout << "id: " << id << endl;
2631
cout << "pos: " << pos << endl;
2732
}
2833

29-
int Shape::get_id() {
34+
unsigned long Shape::get_id() {
3035
return id;
3136
}
3237

@@ -40,8 +45,8 @@ bool Shape::operator==(Shape &s) {
4045

4146
// class Circle
4247

43-
Circle::Circle(int id, Point pos, int r, string text):
44-
Shape(id, pos), r(r), text(text) {
48+
Circle::Circle(Point pos, double r, string text):
49+
Shape(pos), r(r), text(text) {
4550
}
4651

4752
void Circle::print() {
@@ -57,8 +62,8 @@ double Circle::get_contour_length() {
5762

5863
// class Segment
5964

60-
Segment::Segment(int id, Point start, Point end):
61-
Shape(id, start), start(start), end(end) {
65+
Segment::Segment(Point start, Point end):
66+
Shape(start), start(start), end(end) {
6267
}
6368

6469
void Segment::print() {

Semester_2/Lab_14/shape.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,32 @@ using namespace std;
88

99
class Point {
1010
public:
11-
int x, y;
12-
Point(int x = 0, int y = 0);
11+
double x, y;
12+
Point(double x = 0, double y = 0);
1313
friend ostream& operator<<(ostream &out, Point &p);
1414
};
1515

1616
class Shape {
1717
protected:
18-
int id;
1918
Point pos;
19+
unsigned long id;
2020

2121
public:
22-
Shape(int id, Point pos = {0});
22+
Shape(Point pos = {0});
2323
virtual void print();
24-
int get_id();
24+
unsigned long get_id();
25+
unsigned long next_id();
2526
virtual double get_contour_length();
2627
bool operator==(Shape &s);
2728
};
2829

2930
class Circle : public Shape {
3031
protected:
31-
int r;
32+
double r;
3233
string text;
3334

3435
public:
35-
Circle(int id, Point pos = {0}, int r = 1, string text = "");
36+
Circle(Point pos = {0}, double r = 0, string text = "");
3637
virtual void print();
3738
virtual double get_contour_length();
3839
};
@@ -42,7 +43,7 @@ class Segment : public Shape {
4243
Point start, end;
4344

4445
public:
45-
Segment(int id, Point start = {0}, Point end = {0});
46+
Segment(Point start = {0}, Point end = {0});
4647
virtual void print();
4748
virtual double get_contour_length();
4849
};

0 commit comments

Comments
 (0)