0

I am very very new to C++, and I am trying to create a math engine

I have a class called Point, and objects from this class need to be able to hold an unknown amount of dimension values. I have a variable called DAmount that is decided when a point is created. I want to have a method that takes DAmount, and creates that many variables connected to the Point object.

For example, I declare a Point named "SixDPoint" and then set its DAmount to 6. I then use a method named CreateDVars to create six variables connected to the object "SixDPoint". I can then use these six variables to act as XYZ+ axises.

I have no idea how to do this. Here is some code to explain what I am trying to do. Thanks so much


#include <iostream>
using namespace std;

class Point
{
  public:

    int DAmount;

    void CreateDVars(int)
    {
      //Variable declaration Function
    }

};

int main()
{

  Point SixDPoint;  //declaring a Point named SixDPoint

  SixDPoint.DAmount = 6;  // this sets the dimension amount, of SixDPoint, to 6

  SixDPoint.D1 = 1;
  SixDPoint.D2 = 1;
  SixDPoint.D3 = 1;
  SixDPoint.D4 = 1;
  SixDPoint.D5 = 1;
  SixDPoint.D6 = 1;
  //this should assign all six dimensions of SixDPoint, to 1
  cout << SixDPoint.D1 << "/n";
  cout << SixDPoint.D2 << "/n";
  cout << SixDPoint.D3 << "/n";
  cout << SixDPoint.D4 << "/n";
  cout << SixDPoint.D5 << "/n";
  cout << SixDPoint.D6 << "/n";
//this should print out all of the coordinates of SixDPoint
  return 0;
}

  • 4
    You probably want to use a `std::vector` instead of using individual variables. – πάντα ῥεῖ Jun 03 '21 at 23:09
  • Definitely start with `vector`. Fall back on `new` if you are not allowed to use library containers. But if you're forced to use `new` don't forget to `delete`. [RAII](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) should help you figure out where to place that `delete`, and that will lead to you needing to know [The Rule of Three (and friends)](https://en.cppreference.com/w/cpp/language/rule_of_three). – user4581301 Jun 03 '21 at 23:28
  • 1
    General rule of thumb: Anytime you find yourself making sequentially named variables, you'll almost always find an array or library container will make your life easier. – user4581301 Jun 03 '21 at 23:29

1 Answers1

0

I believe you want something like this:

#include <iostream>
#include <vector>
using namespace std;

class Point
{
public:

    int DAmount;

    void CreateDVars(int k)
    {
        dim.resize(k);
    }
    int& operator[](int k)
    {
        if (k >= 0 && k < dim.size())
            return dim[k];
    }
private:
    vector<int> dim;
};

int main()
{

    Point SixDPoint;  //declaring a Point named SixDPoint

    SixDPoint.CreateDVars(6);
    SixDPoint[0] = 1;
    SixDPoint[1] = 1;
    SixDPoint[2] = 1;
    SixDPoint[3] = 1;
    SixDPoint[4] = 1;
    SixDPoint[5] = 1;
    //this should assign all six dimensions of SixDPoint, to 1
    cout << SixDPoint[0] << "/n";
    cout << SixDPoint[1] << "/n";
    cout << SixDPoint[2] << "/n";
    cout << SixDPoint[3] << "/n";
    cout << SixDPoint[4] << "/n";
    cout << SixDPoint[5] << "/n";
    //this should print out all of the coordinates of SixDPoint
    return 0;
}