mirror of
https://github.com/dancojocaru2000/y3s2-cciot-hw.git
synced 2025-02-22 17:09:35 +02:00
Solved Lab1 Slide17
This commit is contained in:
parent
e90300843f
commit
0c2d77569a
5 changed files with 284 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
a.exe
|
||||
a.out
|
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
|
@ -3,5 +3,8 @@
|
|||
"MD024": {
|
||||
"siblings_only": true
|
||||
}
|
||||
},
|
||||
"files.associations": {
|
||||
"ostream": "cpp"
|
||||
}
|
||||
}
|
|
@ -63,3 +63,14 @@ public:
|
|||
};
|
||||
```
|
||||
|
||||
## Slide 17
|
||||
|
||||
### Problem
|
||||
|
||||
Write a program that implements a class hierarchy that can be used to model geometric shapes, based on the following structure:
|
||||
|
||||
[shape_start.cpp](./shape_start.cpp)
|
||||
|
||||
### Solution
|
||||
|
||||
See [shape_start_sol.cpp](./shape_start_sol.cpp).
|
||||
|
|
95
Lab1/shape_start.cpp
Normal file
95
Lab1/shape_start.cpp
Normal file
|
@ -0,0 +1,95 @@
|
|||
#include <iostream>
|
||||
#include <math.h>
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI (3.14159265358979323846)
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
|
||||
enum BOOL { FALSE, TRUE };
|
||||
|
||||
class GeneralShape
|
||||
{
|
||||
public:
|
||||
GeneralShape(){};
|
||||
~GeneralShape(){};
|
||||
virtual long GetArea() = 0;
|
||||
virtual long GetPerim()= 0;
|
||||
virtual void Draw() = 0;
|
||||
};
|
||||
|
||||
void GeneralShape::Draw()
|
||||
{
|
||||
cout << "drawing mechanism!" << endl;
|
||||
}
|
||||
|
||||
class Circle : public GeneralShape
|
||||
{
|
||||
public:
|
||||
Circle(int radius):itsRadius(radius){}
|
||||
~Circle(){}
|
||||
|
||||
private:
|
||||
int itsRadius;
|
||||
int itsCircumference;
|
||||
};
|
||||
|
||||
class Rectangle : public GeneralShape
|
||||
{
|
||||
public:
|
||||
Rectangle(int len, int width):
|
||||
itsLength(len), itsWidth(width){}
|
||||
~Rectangle(){}
|
||||
virtual int GetLength() { return itsLength; }
|
||||
virtual int GetWidth() { return itsWidth; }
|
||||
|
||||
private:
|
||||
int itsWidth;
|
||||
int itsLength;
|
||||
};
|
||||
|
||||
class Square : public Rectangle
|
||||
{
|
||||
public:
|
||||
Square(int len);
|
||||
Square(int len, int width);
|
||||
~Square(){}
|
||||
long GetPerim() {return 4 * GetLength();}
|
||||
};
|
||||
|
||||
Square::Square(int len):
|
||||
Rectangle(len,len)
|
||||
{}
|
||||
|
||||
Square::Square(int len, int width):
|
||||
Rectangle(len,width)
|
||||
{
|
||||
if (GetLength() != GetWidth())
|
||||
cout << "Error, not a square... a Rectangle??\n";
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int choice;
|
||||
BOOL quit = FALSE;
|
||||
GeneralShape *sp;
|
||||
|
||||
while (1)
|
||||
{
|
||||
cout << "(1)Circle (2)Rectangle (3)Square (0)Quit: ";
|
||||
cin >> choice;
|
||||
|
||||
switch (choice)
|
||||
{
|
||||
default: quit = TRUE;
|
||||
break;
|
||||
}
|
||||
|
||||
if (quit) break;
|
||||
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
173
Lab1/shape_start_sol.cpp
Normal file
173
Lab1/shape_start_sol.cpp
Normal file
|
@ -0,0 +1,173 @@
|
|||
#include <iostream>
|
||||
#include <math.h>
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI (3.14159265358979323846)
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
|
||||
enum BOOL { FALSE, TRUE };
|
||||
|
||||
class GeneralShape {
|
||||
public:
|
||||
GeneralShape(){};
|
||||
~GeneralShape(){};
|
||||
virtual long GetArea() = 0;
|
||||
virtual long GetPerim()= 0;
|
||||
virtual void Draw() = 0;
|
||||
};
|
||||
|
||||
void GeneralShape::Draw() {
|
||||
cout << "drawing mechanism!" << endl;
|
||||
}
|
||||
|
||||
class Circle : public GeneralShape {
|
||||
public:
|
||||
Circle(int radius):itsRadius(radius), itsCircumference(2*M_PI*radius){}
|
||||
~Circle(){}
|
||||
virtual long GetArea();
|
||||
virtual long GetPerim();
|
||||
virtual void Draw();
|
||||
|
||||
private:
|
||||
int itsRadius;
|
||||
int itsCircumference;
|
||||
};
|
||||
|
||||
long Circle::GetArea() {
|
||||
return M_PI * itsRadius * itsRadius;
|
||||
}
|
||||
|
||||
long Circle::GetPerim() {
|
||||
return itsCircumference;
|
||||
}
|
||||
|
||||
void Circle::Draw() {
|
||||
cout << "Drawing circle with r=" << itsRadius << ", area=" << GetArea() << ", perim=" << GetPerim() << endl;
|
||||
}
|
||||
|
||||
class Rectangle : public GeneralShape {
|
||||
public:
|
||||
Rectangle(int len, int width):
|
||||
itsLength(len), itsWidth(width){}
|
||||
~Rectangle(){}
|
||||
virtual int GetLength() { return itsLength; }
|
||||
virtual int GetWidth() { return itsWidth; }
|
||||
virtual long GetArea();
|
||||
virtual long GetPerim();
|
||||
virtual void Draw();
|
||||
|
||||
private:
|
||||
int itsWidth;
|
||||
int itsLength;
|
||||
};
|
||||
|
||||
long Rectangle::GetArea() {
|
||||
return itsLength * itsWidth;
|
||||
}
|
||||
|
||||
long Rectangle::GetPerim() {
|
||||
return (itsLength + itsWidth) * 2;
|
||||
}
|
||||
|
||||
void Rectangle::Draw() {
|
||||
cout << "Drawing rectangle with W=" << itsWidth << ", H=" << itsLength << ", area=" << GetArea() << ", perim=" << GetPerim() << endl;
|
||||
}
|
||||
|
||||
class Square : public Rectangle {
|
||||
public:
|
||||
Square(int len);
|
||||
Square(int len, int width);
|
||||
~Square(){}
|
||||
long GetPerim() {return 4 * GetLength();}
|
||||
virtual void Draw();
|
||||
};
|
||||
|
||||
Square::Square(int len): Rectangle(len,len) {}
|
||||
|
||||
Square::Square(int len, int width): Rectangle(len,width) {
|
||||
if (GetLength() != GetWidth()) {
|
||||
cout << "Error, not a square... a Rectangle??\n";
|
||||
}
|
||||
}
|
||||
|
||||
void Square::Draw() {
|
||||
cout << "Drawing square with L=" << GetLength() << ", area=" << GetArea() << ", perim=" << GetPerim() << endl;
|
||||
}
|
||||
|
||||
void shapeMenu(GeneralShape& shape) {
|
||||
bool stop {false};
|
||||
int choice {};
|
||||
while (1) {
|
||||
cout << "(1)Draw (2)Get Perimeter (3)Get Area (0)Back: ";
|
||||
cin >> choice;
|
||||
switch (choice) {
|
||||
case 1:
|
||||
shape.Draw();
|
||||
break;
|
||||
case 2:
|
||||
cout << shape.GetPerim() << endl;
|
||||
break;
|
||||
case 3:
|
||||
cout << shape.GetArea() << endl;
|
||||
break;
|
||||
default:
|
||||
stop = true;
|
||||
}
|
||||
if (stop) break;
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
void circleMenu() {
|
||||
int radius;
|
||||
cout << "Radius? ";
|
||||
cin >> radius;
|
||||
Circle c {radius};
|
||||
shapeMenu(c);
|
||||
}
|
||||
|
||||
void rectMenu() {
|
||||
int width, height;
|
||||
cout << "Width? ";
|
||||
cin >> width;
|
||||
cout << "Height? ";
|
||||
cin >> height;
|
||||
Rectangle r {height, width};
|
||||
shapeMenu(r);
|
||||
}
|
||||
|
||||
void squareMenu() {
|
||||
int length;
|
||||
cout << "Side Length? ";
|
||||
cin >> length;
|
||||
Square s {length};
|
||||
shapeMenu(s);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int choice;
|
||||
BOOL quit = FALSE;
|
||||
GeneralShape *sp;
|
||||
while (1) {
|
||||
cout << "(1)Circle (2)Rectangle (3)Square (0)Quit: ";
|
||||
cin >> choice;
|
||||
switch (choice) {
|
||||
case 1:
|
||||
circleMenu();
|
||||
break;
|
||||
case 2:
|
||||
rectMenu();
|
||||
break;
|
||||
case 3:
|
||||
squareMenu();
|
||||
break;
|
||||
default: quit = TRUE;
|
||||
break;
|
||||
}
|
||||
if (quit) break;
|
||||
cout << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Reference in a new issue