Другие языки программирования и технологии

C++ Где ошибка? Помогите

#include <iostream>
using namespace std;

class Shape
{
public:
Shape(double a = 0, double b = 0)
{
x = a;
y = b;
}
virtual double getArea() = 0;
double x;
double y;
double area;
};

class Triangle : public Shape
{
public:
Triangle(double xx = 0,double yy = 0):Shape(x, y)
{

}
double getArea()
{
area = 0.5*x*y;
cout << "Area of triangle = " << endl; return area;
}
};

class Rectangle : public Shape
{
public:
Rectangle(double xx = 0, double yy = 0):Shape(x, y)
{

}
double getArea()
{
area = x*y;
cout << "Area of rectangle = " << endl; return area;
}
};

class Square : public Shape
{
public:
Square(double xx = 0, double yy = 0):Shape(x, y)
{

}
double getArea()
{
area = x*y;
cout << "Area of square = " << endl; return area;
}
};

int main()
{
Triangle n(2.0, 7.0);
Rectangle m(9.0, 4.0);
Square l(7.0, 8.0);

Shape *first = &n;
Shape *second = &m;
Shape *third = &l;

first->getArea();
second->getArea();
third->getArea();
system("pause");
return 0;
}
#define _USE_MATH_DEFINES

#include <iostream>

using namespace std;

class shape {
public:
    virtual double get_area()const = 0;
    virtual double get_perimeter()const = 0;
    virtual ~shape() { }
};

class circle : public shape {
public:
    circle(double);
    virtual double get_area()const;
    virtual double get_perimeter()const;
private:
    circle() { }
    double radius;
};

class rectangle : public shape {
public:
    rectangle(double, double);
    virtual double get_area()const;
    virtual double get_perimeter()const;
private:
    rectangle() { }
    double width;
    double height;
};

class triangle : public shape {
public:
    triangle(double, double, double);
    virtual double get_area()const;
    virtual double get_perimeter()const;
private:
    triangle() { }
    double a;
    double b;
    double c;
};

int main() {
    const shape * ptr = NULL;

    const circle c(3.25);
    const rectangle r(7.12, 4.3);
    const triangle t(2.65, 3.15, 2.88);

    ptr = &c;
    cout << " Sc = " << ptr->get_area() << "\tPc = " << ptr->get_perimeter() << endl;
    ptr = &r;
    cout << " Sr = " << ptr->get_area() << "\tPr = " << ptr->get_perimeter() << endl;
    ptr = &t;
    cout << " St = " << ptr->get_area() << "\tPt = " << ptr->get_perimeter() << endl;
    cout << endl;

    const size_t size = 3;
    const shape * descendants[size];
    descendants[0] = &c;
    descendants[1] = &r;
    descendants[2] = &t;

    for (rsize_t n = 0; n < size; n++)
        cout << " S = " << descendants[n]->get_area() << "\tP = " << descendants[n]->get_perimeter() << endl;
    
    cin.get();
    return 0;
}

circle::circle(double _r) : radius(_r) { }
double circle::get_perimeter()const { return 2 * M_PI * radius; }
double circle::get_area()const { return M_PI * radius * radius; }

rectangle::rectangle(double _w, double _h) : width(_w), height(_h) { }
double rectangle::get_perimeter()const { return (width + height) * 2; }
double rectangle::get_area()const { return width * height; }

triangle::triangle(double _a, double _b, double _c) : a(_a), b(_b), c(_c) { }
double triangle::get_perimeter()const { return a + b + c; }
double triangle::get_area()const {
    double p = get_perimeter() / 2;
    return sqrt(p * (p - a) * (p - b) * (p - c));
}
Михаил Орлов
Михаил Орлов
76 843
Лучший ответ
А почему ты считаешь, что тут есть ошибка?
Или ты хочешь, чтобы кто-то другой сохранил это текст (не факт что не порезанный Ответами) , запустил компилятор, увидел текст ошибки и продублиловал его тебе? А сам ты безрукий, не можешь этого сделать? Найти ошибку - ТВОЯ обязанность. Вот если ты не понимаешь, от чего она возникает - обращайся. А пока - в слове secоnd буква о русская.
Triangle(double xx = 0,double yy = 0):Shape(x, y) и аналогичные конструкторы неверно передают параметры базовому классу (нужно xx,yy).
Алексей Гогин
Алексей Гогин
35 996
Во-первых, во всех конструкторах от базового класса: Square(double xx = 0, double yy = 0):Shape(x, y) ты принимаешь параметра xx и yy, а передаешь в базовый конструктор x и y. (разные имена) Затем, ты возвращаешь дабл в функциях, но его нигде не выводишь. Ты просто получаешь значение и все. Тебе нужно сделать так:
printf("%f", third->getArea()); тогда он у тебя выведет значение. Возмоно еще какие-то ошибки есть, я код не компилил. Так, на первый взгляд сказал. И кстати, нахрена в конструкторе square два параметра? У квадрата ж все стороны равны =)