Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- class Point {
- private:
- int x1;
- int y1;
- public:
- Point() {
- x1 = 0;
- y1 = 0;
- }
- Point(int _x, int _y) {
- x1 = _x;
- y1 = _y;
- }
- int getX() {
- return x1;
- }
- int getY() {
- return y1;
- }
- void setX(int _x) {
- x1 = _x;
- }
- void setY(int _y) {
- y1 = _y;
- }
- };
- class Vector {
- private:
- int x1;
- int y1;
- public:
- Vector(Point p1, Point p2) {
- x1 = p2.getX() - p1.getX();
- y1 = p2.getY() - p1.getY();
- }
- int getX() {
- return x1;
- }
- int getY() {
- return y1;
- }
- bool IsPerpend(Vector v1) {
- if (x1 * v1.getX() + y1 * v1.getY() == 0) {
- return true;
- }
- return false;
- }
- bool operator==(Vector v1) {
- if (pow(this->x1, 2.0) + pow(this->y1, 2.0) == pow(v1.getX(), 2.0) + pow(v1.getY(), 2.0)) {
- return true;
- }
- return false;
- }
- double lenght() {
- return sqrt(x1*x1 + y1 * y1);
- }
- };
- class Shape {
- protected:
- Point p1;
- public:
- Shape() {
- p1 = Point(0, 0);
- }
- Shape(int x1, int y1) {
- p1 = Point(x1, y1);
- }
- virtual double Area() {
- return 0;
- }
- };
- class Circle : public Shape {
- private:
- double r;
- public:
- Circle() {
- r = 1;
- }
- Circle(double _r) {
- r = _r;
- }
- Circle(int _x, int _y) {
- p1.setX(_x);
- p1.setY(_y);
- r = 1;
- }
- Circle(int _x, int _y, double _r) {
- p1.setX(_x);
- p1.setY(_y);
- r = _r;
- }
- double Area() override {
- return 3.14*r*r;
- }
- void Print() {
- cout << p1.getX() << " " << p1.getX() << " " << r << endl;
- }
- };
- class Rectangle : public Shape {
- protected:
- Point p2;
- Point p3;
- Point p4;
- public:
- Rectangle() {
- p2 = Point(0, 1);
- p3 = Point(1, 1);
- p4 = Point(1, 0);
- }
- Rectangle(Point _p2, Point _p3, Point _p4) {
- p2 = _p2;
- p3 = _p3;
- p4 = _p4;
- Vector v1(p1, p2), v2(p2, p3), v3(p3, p4), v4(p4, p1);
- if (v1.IsPerpend(v2) && v2.IsPerpend(v3) && v3.IsPerpend(v4) && v4.IsPerpend(v1)) {
- }
- else {
- cout << "Error rectangle isn't rectangular" << endl;
- abort();
- }
- }
- Rectangle(Point _p1, Point _p2, Point _p3, Point _p4) {
- p1 = _p1;
- p2 = _p2;
- p3 = _p3;
- p4 = _p4;
- Vector v1(p1, p2), v2(p2, p3), v3(p3, p4), v4(p4, p1);
- if (v1.IsPerpend(v2) && v2.IsPerpend(v3) && v3.IsPerpend(v4) && v4.IsPerpend(v1)) {
- }
- else {
- cout << "Error rectangle isn't rectangular" << endl;
- abort();
- }
- }
- double Area() override {
- Vector v1(p1, p2), v2(p2, p3);
- return v1.lenght() * v2.lenght();
- }
- void Print() {
- cout << p1.getX() << " " << p1.getY() << endl;
- cout << p2.getX() << " " << p2.getY() << endl;
- cout << p3.getX() << " " << p3.getY() << endl;
- cout << p4.getX() << " " << p4.getY() << endl << endl;
- }
- };
- class Square : public Rectangle {
- public:
- Square() {
- }
- Square(Point _p2, Point _p3, Point _p4) {
- p2 = _p2;
- p3 = _p3;
- p4 = _p4;
- Vector v1(p1, p2), v2(p2, p3), v3(p3, p4), v4(p4, p1);
- if (v1.IsPerpend(v2) && v2.IsPerpend(v3) && v3.IsPerpend(v4) && v4.IsPerpend(v1)) {
- }
- else {
- cout << "Error square isn't square" << endl;
- abort();
- }
- if (v1 == v2 && v2 == v3 && v3 == v4) {
- }
- else {
- cout << "The sides of the square are not equal" << endl;
- abort();
- }
- }
- Square(Point _p1, Point _p2, Point _p3, Point _p4) {
- p1 = _p1;
- p2 = _p2;
- p3 = _p3;
- p4 = _p4;
- Vector v1(p1, p2), v2(p2, p3), v3(p3, p4), v4(p4, p1);
- if (v1.IsPerpend(v2) && v2.IsPerpend(v3) && v3.IsPerpend(v4) && v4.IsPerpend(v1)) {
- }
- else {
- cout << "Error square isn't square" << endl;
- abort();
- }
- }
- double Area() override {
- Vector v1(p1, p2);
- return v1.lenght() * v1.lenght();
- }
- void Print() {
- cout << p1.getX() << " " << p1.getY() << endl;
- cout << p2.getX() << " " << p2.getY() << endl;
- cout << p3.getX() << " " << p3.getY() << endl;
- cout << p4.getX() << " " << p4.getY() << endl << endl;
- }
- };
- int main() {
- Circle c1;
- c1.Print();
- Circle c2(2);
- c2.Print();
- Circle c3(0, 0);
- c3.Print();
- Circle c4(0, 0, 5);
- c4.Print();
- cout << c4.Area() << endl;
- Rectangle r1;
- r1.Print();
- Rectangle r2(Point(1, 0), Point(1, 2), Point(0, 2));
- r2.Print();
- Rectangle r3(Point(0, 0), Point(1, 0), Point(1, 2), Point(0, 2));
- r3.Print();
- cout << r3.Area() << endl;
- Square s1;
- s1.Print();
- Square s2(Point(2, 0), Point(2, 2), Point(0, 2));
- s2.Print();
- Square s3(Point(0, 0), Point(2, 0), Point(2, 2), Point(0, 2));
- s3.Print();
- cout << s3.Area() << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement