Как вектор c++ заполнить объектами разных классов?

Я хочу запонить вектор объектами разных классов, а потом посчитать их точки в пространстве и первую производную. У меня всего три объекта: окружность, эллипс и спираль, у классов есть родитель окружность. Не понимаю, почему все время вызываются методы только родительского класса, а не наследуемых от него. Всегда вызывается метод Calculate класса Circle. В чем моя ошибка?

#include <iostream>
#include <math.h>
#include <vector>
#include <random>

#define PI 3.14159265
using namespace std;

class Circle {
protected:
    double r, t, derivative, x, y, z = 0;
    vector <double> Info;

    void Calculate() {
        this->x = r * cos(t * PI / 180.0);
        this->y = r * sin(t * PI / 180.0);
        this->derivative = -cos(t * PI / 180.0) / sin(t * PI / 180.0);
        this->Info = { x, y, z, derivative };
    }

public:
    Circle(){
    }
    Circle(double r) {
        this->r = r;
    }
    vector <double> GetInfo() {
        this->Calculate();
        return Info;
    }

    double GetRadius() {
        return r;
    }
    void SetT(double t) {
        this->t = t;
    }
};

class Ellips: public Circle {
protected:
    double r1, r2, x, y, derivative;
    vector <double> radiuses;

    void Calculate() {
        this->x = r1 * cos(t * PI / 180.0);
        this->y = r2 * sin(t * PI / 180.0);
        this->derivative = -r2 * cos(t * PI / 180.0) / r1 * sin(t * PI / 180.0);
        this->Info = { x, y, z, derivative };
    }
public:
    Ellips(double r1, double r2) {
        this->r1 = r1;
        this->r2 = r2;
        radiuses = { r1,r2 };
    }

    vector <double> GetRaduises() {
        return radiuses;
    }
};

class Helix : public Circle {
protected:
    double z, step;

    void Calculate() {
        this->x = r * cos(t * PI / 180.0);
        this->y = r * sin(t * PI / 180.0);
        this->z = step * (t * PI / 180.0);
        this->derivative = -step * sin(t * PI / 180.0) / cos(t * PI / 180.0);
        this->Info = { x, y, z, derivative };
    }
public:
    Helix(double r, double step) {
        this->r = r;
        this->step = step;
    }
};

int main()
{
    int countOfElements = 3 + rand() % 15;
    vector <Circle> randomList;
    int number;
    for (int i = 0; i < countOfElements; i++) {
        number = 1 + rand() % 3;
        switch (number) {
        case 1: {;
            randomList.push_back(Circle(rand() % 15));
            break;
        }
        case 2: {
            randomList.push_back(Ellips(rand() % 15, rand() % 15));
            break;
        }
        case 3:
            randomList.push_back(Helix(rand() % 15, rand() % 360));
            break;
        }
    }

    for (int i = 0; i < countOfElements; i++) {
        randomList[i].SetT(45);
        vector <double> tempInfo = randomList[i].GetInfo();
        for (double j : tempInfo) {
            cout << j << "| \t";
        }
        cout << endl;
    }

    return 0;
}

Ответы (1 шт):

Автор решения: Harry

Вобщем, чтоб с минимальными переделками вашего кода — то вот так. Хотя я бы переделал побольше :), но для понимания должно хватить...

#include <iostream>
#include <math.h>
#include <vector>
#include <random>

#define PI 3.14159265
using namespace std;

class Circle {
protected:
    double r, t, derivative, x, y, z = 0;
    vector <double> Info;

    virtual void Calculate() {
        this->x = r * cos(t * PI / 180.0);
        this->y = r * sin(t * PI / 180.0);
        this->derivative = -cos(t * PI / 180.0) / sin(t * PI / 180.0);
        this->Info = { x, y, z, derivative };
    }
    virtual ~Circle() {}

public:
    Circle(){
    }
    Circle(double r) {
        this->r = r;
    }
    vector <double> GetInfo() {
        this->Calculate();
        return Info;
    }

    double GetRadius() {
        return r;
    }
    void SetT(double t) {
        this->t = t;
    }
};

class Ellips: public Circle {
protected:
    double r1, r2, x, y, derivative;
    vector <double> radiuses;

    virtual void Calculate() {
        this->x = r1 * cos(t * PI / 180.0);
        this->y = r2 * sin(t * PI / 180.0);
        this->derivative = -r2 * cos(t * PI / 180.0) / r1 * sin(t * PI / 180.0);
        this->Info = { x, y, z, derivative };
    }
public:
    Ellips(double r1, double r2) {
        this->r1 = r1;
        this->r2 = r2;
        radiuses = { r1,r2 };
    }

    vector <double> GetRaduises() {
        return radiuses;
    }
    virtual ~Ellips() {}
};

class Helix : public Circle {
protected:
    double z, step;

    virtual void Calculate() {
        this->x = r * cos(t * PI / 180.0);
        this->y = r * sin(t * PI / 180.0);
        this->z = step * (t * PI / 180.0);
        this->derivative = -step * sin(t * PI / 180.0) / cos(t * PI / 180.0);
        this->Info = { x, y, z, derivative };
    }
public:
    Helix(double r, double step) {
        this->r = r;
        this->step = step;
    }
    virtual ~Helix() {}
};

int main()
{
    int countOfElements = 3 + rand() % 15;
    vector <Circle*> randomList;
    int number;
    for (int i = 0; i < countOfElements; i++) {
        number = 1 + rand() % 3;
        switch (number) {
        case 1: {;
            randomList.push_back(new Circle(rand() % 15));
            break;
        }
        case 2: {
            randomList.push_back(new Ellips(rand() % 15, rand() % 15));
            break;
        }
        case 3:
            randomList.push_back(new Helix(rand() % 15, rand() % 360));
            break;
        }
    }

    for (int i = 0; i < countOfElements; i++)
    {
        randomList[i]->SetT(45);
        vector <double> tempInfo = randomList[i]->GetInfo();
        for (double j : tempInfo) {
            cout << j << "| \t";
        }
        cout << endl;
    }

    for(auto c: randomList) delete c;

    return 0;
}
→ Ссылка