Hi all, I need some help regarding dynamic vector.

Shape2DLink.cpp

void Shape2DLink::InputSensor()
{
    string shape,type;  

    cout<<endl<<"\n"<<"[ Input sensor data ]"<<endl;


    cout << "Please enter name of shape: " << endl;
        cin >> shape;
        shape2D.setName(shape); 

    cout << "Please enter special type : " << endl;
        cin >> type;
        shape2D.setWarpSpace(type);

    if(shape == "Square")
    {
            square.setSquareCord();
            square.computeArea();
    }
    else if(shape =="Rectangle")
    {
        rect.setRectCord();
        rect.computeArea();

    }
    else if(shape == "Cross")
    {
        cross.setCrossCord();
        cross.computeArea();

    }
}   

ShapeTwoD.cpp (parent class)

ShapeTwoD::ShapeTwoD()
{
    string name = "";
    bool containsWarpSpace = true;
}

ShapeTwoD::ShapeTwoD(string ShapeName, bool warpspace)
{
    name = ShapeName;
    containsWarpSpace = true;
}
double ShapeTwoD::computeArea()
{

    return 0.00;
}
}

cross.cpp (children)

Cross::Cross()
{
    xVal = 0;
    yVal = 0;
}

Cross::Cross(string ShapeName, bool warpspace, int xval, int yval):ShapeTwoD(ShapeName, warpspace), xVal(xval), yVal(yval)
{
    xVal = xval;
    yVal = yval;
}

void Cross::setCrossCord()
{
    for (int j=0; j<12; j++)
    {
        cout << "Please enter x-ordinate of pt " << j+1 << ": "; 
        cin >> xVal;
        xvalue[j] = xVal;
        cout << endl;
        cout << "Please enter y-ordinate of pt " << j+1 << ": ";
        cin >> yVal;
        yvalue[j] = yVal;
        cout << endl;
    }
}


double Cross::computeArea()
{
    int points = 12;
    int running_total =0;
    int i;
    running_total = 0;
    for (i=0; i<points-1; i++)
    {
      running_total += xvalue[i]*yvalue[i+1] - xvalue[i+1]*yvalue[i]; //cross calculation of coord in a cross 
    }                                                                 //(x1*y2)-(y1*x1)

    running_total += xvalue[points-1]*yvalue[0] - xvalue[0]*yvalue[points-1];   // traverse back to the origin point (xn*y1)-(yn*x1)
    area = abs(running_total / 2); //purpose of absolute is to make sure result is positive. 
                                  //polygon are specified in counter-clockwise order (i.e. by the right-hand rule), then the area will be positive.
    //cout << area;
    return (area);
}
}

So i'm actually using polyphormism for calculating the area of the cross and other shapes.
so how do I actually make use of dynamically create an object this way?

do I create them in my Shape2DLink or in my individual child classes?

Recommended Answers

All 3 Replies

The text of your post is "Hi all, I need some help regarding dynamic vector." - where are you using vectors (std::vector<type>) in your code?

What about your "area computation" algorithm? Have you accomodated the possibility of range over-run?

@rubberman hi this is my updated post from my question.

Shape2DLink.h

class Shape2DLink:public ShapeTwoD
{
    public:
        int getx();
        int gety();

        double getArea();

        void InputSensor();//allow user to input data
        void StoreData();
        vector<ShapeTwoD*> objs;
};

Shape2DLink.cpp

void Shape2DLink::InputSensor()
{
    string shape,type;  

    cout<<endl<<"\n"<<"[ Input sensor data ]"<<endl;


    cout << "Please enter name of shape: " << endl;
        cin >> shape;
        shape2D.setName(shape); 

    cout << "Please enter special type : " << endl;
        cin >> type;
        shape2D.setWarpSpace(type);

    if(shape == "Square")
    {
            square.setSquareCord();
            square.computeArea();
            objs.push_back(new Square(square[0]));
    }
    else if(shape =="Rectangle")
    {
        rect.setRectCord();
        rect.computeArea();
        objs.push_back(new Rectangle(rect[0]));

    }
    else if(shape == "Cross")
    {
        cross.setCrossCord();
        cross.computeArea();
        objs.push_back(new Cross(cross[0]));
    }
}   

ShapeTwoD.h

class ShapeTwoD
{
    public:
        string name, warpSpace;
        bool containsWarpSpace;
        int xCord, yCord;

        int xVal, yVal;
        int length, breath;
        double area;
    //public:
        //constructor
        ShapeTwoD();
        ShapeTwoD(string, bool);

        //accessors/set function
        void setName(string);
        void setValues(int, int);
        bool setContainsWarpSpace();
        void setWarpSpace(string);

        //mutator/get function
        string getName();
        bool getContainsWarpSpace();
        string getWarpSpace(); //get value of warpspace

        //methods
        string toString();
        virtual double computeArea();
        bool isPointInShape(int x, int y);
        bool isPointOnShape(int x, int y); 

        int xvalue[4];
        int yvalue[4];
};

ShapeTwoD.cpp

ShapeTwoD::ShapeTwoD()
{
    string name = "";
    bool containsWarpSpace = true;
}

ShapeTwoD::ShapeTwoD(string ShapeName, bool warpspace)
{
    name = ShapeName;
    containsWarpSpace = true;
}
double ShapeTwoD::computeArea()
{

    return 0.00;
}
}

Cross.h

class Cross:public ShapeTwoD
{
    private:
        int xVal,yVal;
        int length, breath;
        double area;

    public:
        Cross();
        Cross(string, bool, int, int);

        //mutator method
        //void setSquareDetails(int x, int y);
        void setCrossCord();

        //acessor method
        //int getSquareDetails();
        int getxCord();
        int getyCord();

        bool isPointInShape(int x, int y);
        bool isPointOnShape(int x, int y);

        string toString();
        double computeArea();

        int xvalue[12];
        int yvalue[12];


};

Cross.cpp

Cross::Cross()
{
    xVal = 0;
    yVal = 0;
}

Cross::Cross(string ShapeName, bool warpspace, int xval, int yval):ShapeTwoD(ShapeName, warpspace), xVal(xval), yVal(yval)
{
    xVal = xval;
    yVal = yval;
}

void Cross::setCrossCord()
{
    for (int j=0; j<12; j++)
    {
        cout << "Please enter x-ordinate of pt " << j+1 << ": "; 
        cin >> xVal;
        xvalue[j] = xVal;
        cout << endl;
        cout << "Please enter y-ordinate of pt " << j+1 << ": ";
        cin >> yVal;
        yvalue[j] = yVal;
        cout << endl;
    }
}

Sorry for the long spam I decided to put everything here again for easy reading.

I'm not sure if my codes are making sense cause i'm kinda lost on how to store all 3 different object into one vector so that i'm able to do a sort after tat.

You cannot store different types in a vector. However, if all your class objects are of the same base class, they are all objects of that base class, so you can store in the vector a pointer-to-base-class; each pointer in the vector pointing to an instance of your objects.

vector<ShapeTwoD*> vectorOfPointers;
Cross         aCrossObject;
ShapeTwoD     aShapeTwoDObject;
ShapeTwoDLink aShapeTwoDLinkObject; 

vectorOfPointers.push_back(&aCrossObject);
vectorOfPointers.push_back(&aShapeTwoDObject);
vectorOfPointers.push_back(&aShapeTwoDLinkObject);

It will be up to you to ensure that the objects being pointed to still exist when you need them. A good way to do this is to use a smart pointer.

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.