Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ problem, array as a private data member for a class

Options
  • 16-11-2002 6:21pm
    #1
    Registered Users Posts: 1,722 ✭✭✭


    I'm currently doing a project for college that will take a list of x, y co-ordinate that make up a polygon and display the area of that polygon.

    We have to implement the program using classes. One class is a point class that just holds x and y. Then there's a triangle class that has three points as private data members and it can calculate the area of the triangle. Last class is a polygon class that for its private data members has an array of points that make up the polygon and an int that holds how many points the polygon has. It also recursively splits the polygon into triangles and calculates the area from that.

    The problem I've encountered is how to declare the array of points in the polygon class so that its index is the same size as the number of points it has to hold. Would the correct way to handle this problem be to have a pointer of type point class ( the class that holds x and y) and then in the constructor of the class it takes an int in the parameter list that would be the number of points in the polygon. You'd then get the pointer to point to an array (of point class ) with the right size that could then hold all the co-ordinates for the rest of the objects lifespan. Here's the code I'm thinking would be needed.
    class polygon
    {
    public:
    polygon(int); // consturctor
    
    .. other functions for loading points and calculating area and so on
    
    private:
    point * ptrForArray;
    int numberOfPoints;
    };
    
    and then in the polygon.cc file the constuctor would look something like this. I'm not sure how to get this to work so here's where my problem is.
    polygon::polygon(int pointsCount)
    {
    ptrForArray = new pointsArray[pointsCount];
    numberOfPoints = pointsCount;
    }
    
    Will the array pointsArray lifespan cover the lifespan of the object or will it end after this member function is finished? I'm getting a segmentation fault when I try to run this code and the debugger is showing it happens when I try and create a polygon object so I'm guessing the problem is with the above code. Could anyone either point out what's wrong with what i'm trying above or suggest a different approach to the problem. I don't want to post up my source code as I'm fairly sure my lecturer would frown on that but I'd really appricate any help anyone could give me.

    edit: just wanted test out [code] tags :P


Comments

  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    Hmm maybe try this instead;

    class whatever{
    private:
    int*polygons;
    int polygon_count;
    public:
    whatever(int lala);
    ~whatever();
    };


    whatever::whatever(int lala)
    {
    polygon_count=lala;
    polygons=new int[lala];
    cout<<"There is no Dana there is only Zool"<<endl;
    return;
    };

    whatever::~whatever()
    {
    delete polygons;
    cout<<"She sleeps above her covers....'Four feet' above her covers"<<endl;
    return;
    };

    Typedef


  • Registered Users Posts: 2,281 ✭✭✭DeadBankClerk


    I heartily endorse Typedef's product or service.


  • Registered Users Posts: 1,722 ✭✭✭Thorbar


    Thanks Typedef worked perfectly.


Advertisement