Hello,
Need some help with Pointers, please.
I have base class ShapeTwoD and 3 derived classes: Cross, Rectangle and Square. Each have different x and y values input by user. Eg, Cross has 12 sets of x,y coordinates, Rectangle and Square have 4 sets each. Can I know how to store this array of data x and y in ShapeTwoD by using pointers?

For this sets of code below, how do I write such that 4 sets of x,y coordinates will be stored in ShapeTwoD?

//Assn2.cpp

        if(shape == "Square" || shape == "square")
            {
            pShape2d[nPos] = new Square();
            for (int i=0; i<4; i++)
            {
                cout << "Please enter x-ordinate of pt." << i+1 << " : ";
                cin >> x ;
                pShape2d[nPos]->setX(x);
                cout << "Please enter y-ordinate of pt." << i+1 << " : ";
                cin >> y;
                pShape2d[nPos]->setY(y);
            }
            }

I will also need to pass the x and y values to derived classes to compute area.

//Square.cpp
//this is virtual function

double Square::computeArea()
{
    for (int i=0; i<4; i++)
    {
    double length = sqrt(pow((x[i] - x[i+1]), 2) + pow((y[i] - y[i+1]), 2));
        double area = pow(length, 2);
    cout << "Square Area: " << area << endl;
    break;
    }
}

This is what I'm trying to achieve. When computeArea4All() is called, computeArea() function in derived classed will be implemented.

//Assn2.cpp

int Assn2::computeArea4All()
{
    int nCount=0;
    for (int i = 0; i<nPos; i++) 
    {   
        double area = pShape2d[i]->computeArea();
        pShape2d[i]->setArea(area);
        nCount++;
    }
    cout << "\nComputation completed! ( " << nCount << " records were updated)" << endl;
    return nCount;
}

I hope I'm making sense here. Thanks so much for helping. Have been stuck with this for days and still can't figure out.

More codes:

// ShapeTwoD.h

class ShapeTwoD
{
    protected: 
        int x, y;
        string name;
        bool containsWarpSpace;
        double area;

    public:

        ShapeTwoD();
        ShapeTwoD (string, bool);
        string getName();
        bool getContainsWarpSpace();
        string toString();
        //virtual function override by sub-classes and implemented individually
        virtual double computeArea();       
        virtual bool isPointInShape (int x, int y);
        virtual bool isPointOnShape (int x, int y);
        void setName(string);
        void setContainsWarpSpace (bool);
        void setX (int);
        void setY (int);
        void setArea(double);
};






// Shape2D.cpp
ShapeTwoD::ShapeTwoD() 
{
    name = " ";
    containsWarpSpace = true;
}

void ShapeTwoD::setX (int newX)
{
    x = newX;
}

void ShapeTwoD::setY (int newY)
{
    y = newY;
}


ShapeTwoD::ShapeTwoD (string name, bool containsWarpSpace)
{
    this->name = name;
    this->containsWarpSpace = containsWarpSpace;
}

//accessors
string ShapeTwoD::getName() 
{   
    return (name);  
}

bool ShapeTwoD::getContainsWarpSpace()
{
    return (containsWarpSpace);
}


//mutators

void ShapeTwoD::setArea (double area)
{
    this->area = area;
}

void ShapeTwoD::setName(string name)
{
    this->name = name;
}

void ShapeTwoD::setContainsWarpSpace(bool containsWarpSpace)
{
    this->containsWarpSpace = containsWarpSpace;
}

For this sets of code below, how do I write such that 4 sets of x,y coordinates will be stored in ShapeTwoD?

This doesn't store them with pointers. It just stores an array of four points. Does it have to use pointers?

struct point{
int x;
int y;
};

class ShapeTwoD
{
  point arrayOfPoints[4];
  ...
  ...

I need to use polymorphism so that's where pointers come in.

Sounds to me like you want to store an array of ShapeTwoD objects; not an array of x&y coordinates?

You have enlightened me on something I have been stuck with for the past few days. I have created object arrays of ShapeTwoD to invoke virtual function for polymorphism and I have stored x and y values in derived classes using array. It's working. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.