Форма класса и связанные с ней функции
Это выход, который я должен получить, но я получаю много ошибок . Пожалуйста, помогите мне
int main() { int size = 5; Shape** shapes = new Shape*[size]; Point* centre = new Point(5,5); Rectangle* rectangle1 = new Rectangle(*centre, 4, 3); shapes[0] = rectangle1; shapes[1] = new Rectangle (*new Point(10, -10), 4, 4); Rectangle* rectangle3 = new Square(*new Point(-8.5,-10), 2.5); shapes[2] = rectangle3; Square* square2 = new Square (*new Point(-1,-1), 1.5); shapes[3] = square2; Circle* circle = new Circle(*new Point(100,100), 25.4); shapes[4] = circle; cout << "\n11111111111111111111111111\n"; for (int i = 0; i < size; ++i) shapes[i] -> display(); cout << "\n22222222222222222222222222\n"; cout << "\nthe area of the first rectangle is: "; cout << rectangle1->area() << endl; cout << "It is a square, the area is: "; Square* square = dynamic_cast<Square*> (rectangle3); cout << square->area() << endl; cout << "the perimeter of the circle is: "; cout << circle->perimeter() << endl; cout << "\n33333333333333333333333333\n"; cout << "\nThe four vertices of the first rectangle are: (clockwise from top-left)\n"; rectangle1->printFourVertices(); cout << "\nThe four vertices of the third rectangle are:\n"; rectangle3->printFourVertices(); cout << "\n44444444444444444444444444\n"; rectangle1->reflectX(); shapes[2] -> translate(1.5, 3); shapes[4] -> translate(-100,-100); cout << "\nAfter reflection and translation, here are the moved shapes:\n" ; for (int i = 0; i < size; ++i) shapes[i] -> display(); cout << "\n55555555555555555555555555\n"; cout << "\nNow, the four vertices of the first rectangle are:\n"; rectangle1->printFourVertices(); cout << "\nNow, the four vertices of the third rectangle are:\n"; rectangle3->printFourVertices(); cout << "\n66666666666666666666666666\n\n"; for (int i = 0; i < size; ++i) cout << shapes[i] -> area() << endl; // From the following statements, understand how setLength() and setWidth() // work for rectangles and squares. cout << "\n777777777777777777777777777\n"; shapes[1] -> display(); dynamic_cast<Rectangle*> (shapes[1]) -> setLength(8.2); cout << "\nAfter setLength(8.2), the rectangle is: "; shapes[1] -> display(); dynamic_cast<Rectangle*> (shapes[1]) -> setWidth(5); cout << "\nAfter setWidth(5), the rectangle is: "; shapes[1] -> display(); square2 -> display(); square2 -> setLength(8.2); cout << "\nAfter setLength(8.2), the rectangle is: "; square2 -> display(); square2 -> setWidth(5); cout << "\nAfter setWidth(5), the rectangle is: "; square2 -> display(); system("pause"); return 0; } /* 11111111111111111111111111 Rectangle: Centre: (5, 5) Length: 4 Width: 3 Rectangle: Centre: (10, -10) Length: 4 Width: 4 Square: Centre: (-8.5, -10) Side Length: 2.5 Square: Centre: (-1, -1) Side Length: 1.5 Circle: Centre: (100, 100) Radius: 25.4 22222222222222222222222222 the area of the first rectangle is: 12 It is a square, the area is: 6.25 the perimeter of the circle is: 159.512 33333333333333333333333333 The four vertices of the first rectangle are: (clockwise from top-left) (3, 6.5) (7, 6.5) (7, 3.5) (3, 3.5) The four vertices of the third rectangle are: (-9.75, -8.75) (-7.25, -8.75) (-7.25, -11.25) (-9.75, -11.25) 44444444444444444444444444 After reflection and translation, here are the moved shapes: Rectangle: Centre: (5, -5) Length: 4 Width: 3 Rectangle: Centre: (10, -10) Length: 4 Width: 4 Square: Centre: (-7, -7) Side Length: 2.5 Square: Centre: (-1, -1) Side Length: 1.5 Circle: Centre: (0, 0) Radius: 25.4 55555555555555555555555555 Now, the four vertices of the first rectangle are: (3, -3.5) (7, -3.5) (7, -6.5) (3, -6.5) Now, the four vertices of the third rectangle are: (-8.25, -5.75) (-5.75, -5.75) (-5.75, -8.25) (-8.25, -8.25) 66666666666666666666666666 12 16 6.25 2.25 2025.8 777777777777777777777777777 Rectangle: Centre: (10, -10) Length: 4 Width: 4 After setLength(8.2), the rectangle is: Rectangle: Centre: (10, -10) Length: 8.2 Width: 4 After setWidth(5), the rectangle is: Rectangle: Centre: (10, -10) Length: 8.2 Width: 5 Square: Centre: (-1, -1) Side Length: 1.5 After setLength(8.2), the rectangle is: Square: Centre: (-1, -1) Side Length: 8.2 After setWidth(5), the rectangle is: Square: Centre: (-1, -1) Side Length: 5 */
Что я уже пробовал:
// jianger.cpp : Defines the entry point for the console application. // #include "stdafx.h" # include<iostream> # include<cmath> # include<string> using namespace std; class Point { private: double x; double y; public: Point(); Point(const double &x,const double &y); void setX(const double &x); void setY(const double &y); double getX()const; double getY()const; double DisFromOrigin()const; virtual void translate(const double &xDis, const double &yDis); void reflectX(); // chnage my private members void reflectY(); virtual void PrintName()const; }; Point::Point() { x=0; y=0; } Point:: Point(const double &x,const double &y) { this->x=x; this->y=y; } void Point::setX(const double &x) { this->x=x; this->y=y; } void Point:: setY(const double &y) { this->y =y; } double Point:: getX()const { return x; } double Point:: getY()const { return y; } double Point::DisFromOrigin()const { return (sqrt(x*x + y*y)); } void Point:: translate(const double &xDis, const double &yDis) { double a; double b; a = this->x + xDis; b= this->y + yDis; this->x= a; this->y=b; } void Point::reflectX() { this->y = -1 *y; } void Point::reflectY() { this->x= -1*x; } void Point::PrintName()const { cout<<"Point ("<<getX()<<" , "<<getY()<<")"<<endl; } class Shape { public: virtual double Peri()const=0; virtual void PrintName()const=0; virtual void translate(double x1,double y1)=0; virtual void reflectX()=0; virtual void reflectY()=0; virtual double getArea()const=0; virtual double getPeri()=0; virtual string getName()const=0; } class Circle:public Shape { public: Circle(); Circle(const Point &p, const double &r); double getR()const; void setR(const double &r); virtual double getPeri()const; virtual void PrintName()const; virtual void translate(const double &x1, const double &y1); virtual void reflectX(); virtual void reflectY(); virtual double getArea()const; virtual string getName()const; protected: Point center; string name; double radius; }; Circle::Circle() { name = "Circle"; center = Point(0,0); radius = 1; } Circle::Circle(const Point &p, const double &r) { name = "Circle"; this->center= p; this->radius=r; } double Circle::getPeri()const { return (2*3.14*getR()); } double Circle::getR()const { return radius; } void Circle::setR(const double &r) { this->radius = r; } void Circle:: PrintName()const { cout<<"Circle"<<endl; } void Circle::translate(const double &x1, const double &y1) { this->center.translate(x1 , y1); } void Circle::reflectX() { this->center.reflectX(); } void Circle::reflectY() { this->center.reflectY(); } double Circle::getArea()const { return (3.14*this->getR()*this->getR()); } string Circle::getName()const { return this->name; } class Rectangle:public Shape { private: double len; double width; Point center; string name; public: Rectangle(); Rectangle(const Point &p, const double &len , const double &width); void setLen(double len); void setWid(double width); double getLen()const; double getWid()const; virtual double getPeri()const; virtual void PrintName()const; virtual void translate(const double &x1, const double &y1); virtual void reflectX(); virtual void reflectY(); virtual double getArea()const; virtual string getName()const; }; Rectangle::Rectangle() { len =1; width=1; this->name = "Rectangle"; center = Point(0,0); } Rectangle:: Rectangle(const Point &p, const double &len , const double &width) { this->name ="Rectangle"; this->center = p; this->len = len; this->width = width; } double Rectangle:: getPeri()const { return (2*getLen() + 2*getWid()); } void Rectangle::PrintName()const { cout<<"Rectangle"<<endl; } void Rectangle::translate(const double &x1, const double &y1) { this->center.translate(x1, y1); } void Rectangle::reflectX() { this->center.reflectX(); } void Rectangle::reflectY() { this->center.reflectY(); } double Rectangle::getArea()const { return(getLen()*getWid()); } string Rectangle::getName()const { return name; } void Rectangle::setLen(double len) { this->len = len; } void Rectangle::setWid(double width) { this->width = width; } double Rectangle::getLen()const { return len; } double Rectangle::getWid()const { return width; } class Square:public Rectangle { private: Point center; string name; double side; public: Square(); Square(const double &side); void setSide(const double &side); double getSide()const; virtual double getPeri()const; virtual void PrintName()const; virtual void translate(const double &x1, const double &y1); virtual void reflectX(); virtual void reflectY(); virtual double getArea()const; virtual string getName()const; }; Square::Square() { side=0; } Square::Square(const double &side) { this->side = side; } void Square::setSide(const double &side) { this->side = side; this->Rectangle::setLen(side); this->Rectangle::setWid(side); } double Square::getSide()const { return this->Rectangle::getLen(); } double Square:: getPeri()const { return(this->Rectangle::getLen()* 4); } void Square::PrintName()const { cout<<"Square"<<endl; } void Square::translate(const double &x1, const double &y1) { this->Rectangle::translate(x1, y1); } void Square::reflectX() { this->Rectangle::reflectX(); } void Square::reflectY() { this->Rectangle::reflectY(); } double Square::getArea()const { return(this->Rectangle::getLen()* this->Rectangle::getLen()); } string Square::getName()const { return name; } int _tmain(int argc, _TCHAR* argv[]) { int size = 5; Shape** shapes = new Shape*[size]; Point* centre = new Point(5,5); Rectangle* rectangle1 = new Rectangle(*centre, 4, 3);// it means that this can not be abstract class!!! coz it is calling object of itself so no virtual functions shapes[0] = rectangle1; shapes[1] = new Rectangle (*new Point(10, -10), 4, 4); Rectangle* rectangle3 = new Square(*new Point(-8.5,-10), 2.5); shapes[2] = rectangle3; Square* square2 = new Square (*new Point(-1,-1), 1.5); shapes[3] = square2; Circle* circle = new Circle(*new Point(100,100), 25.4); shapes[4] = circle; cout << "\n11111111111111111111111111\n"; for (int i = 0; i < size; ++i) shapes[i] -> display(); return 0; }
Patrice T
"но я получаю много ошибок"
Вы забыли сообщить нам сообщения об ошибках и позиции.
Это ошибки времени компиляции или ошибки времени выполнения?
kumn1d
Все работает нормально, но теперь я могу реализовать приведенный ниже вывод. Пожалуйста, скажите мне, как реализовать четыре вершины . Я в замешательстве, потому что у меня есть только один точечный центр в каждом классе, поэтому я не знаю, как найти четыре вершины ?
cout < & lt; " \n четыре вершины первого прямоугольника: (по часовой стрелке сверху слева)\n";
rectangle1- & gt;printFourVertices();
cout < & lt; " \n четыре вершины третьего прямоугольника:\n";
rectangle3- & gt;printFourVertices();
David Crow
Метода printFourVertices() не существует. Вы имеете в виду именно эту ошибку?
Richard MacCutchan
Пожалуйста, отредактируйте свой вопрос и объясните точно, что это за ошибки и где они происходят.
David Crow
Я вижу избыточную операцию присваивания в методе Point:: setX (). Это намеренно?