Dynamically create class objects?

Hi all, I need some help regarding dynamic vector.


Shape2DLink.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    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();
    }
    } 


Shape2D.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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();
		
		int xvalue[4];
		int yvalue[4];
};





ShapeTwoD.cpp (parent class)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[code]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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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();
		
		string toString();
        double computeArea();
		
		int xvalue[12];
		int yvalue[12];
		
		
};




cross.cpp (children)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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?
please advice..
Last edited on
I think you're mashing two separate concepts into one. The fact that you are storing your shapes in a vector has no bearing on the fact that you are using polymorphism to calculate the area of a shape. What is it you're having trouble with? And can we see the header files? There maybe something that I'm missing.
@Computergeek01 Hi, yup you're right i'm actually using polymorphism to calculate the area of a shape. so how that I have 3 different classes of shape 1) square 2) rect) 3) cross and I need to store them all in a vector so that I can do a sort later..

so how do i go ard doing it?
I gotcha, then yes you could make an array of pointers to the base class and store them that way. As for calculating the area of each shape, you would make the area function virtual and define it for each struct\class.
@Computergeek01 yup i've already done the calculation of each shape in their respective classes.. am I right to do it this way?

vector<ShapeTwoD*> objs;
objs.push_back(new Square(*s));
objs.push_back(new Cross(*c));

vector<ShapeTwoD*>objs - is because my base/parent class is ShapeTwoD

than for "objs.push_back(new Square(*s));" where do I actually put this? in my Shape2DLink?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if(shape == "Square")
    {
    square.setSquareCord();
    square.computeArea();
    objs.push_back(new Square(*s));
    }
    else if(shape =="Rectangle")
    {
    rect.setRectCord();
    rect.computeArea();
    objs.push_back(new Rect(*r));
    }
    else if(shape == "Cross")
    {
    cross.setCrossCord();
    cross.computeArea();
    objs.push_back(new Cross(*c));
    }
I would personally put that in my main function but that's really more of an organizational question.

EDIT: You seem to have a functional idea of what you are doing and other then some evidence suggesting that you may be including the std namespace in your header files I don't see any problems.
Last edited on
@Computergeek01 but it's ok to put implement it this way right? am i right to say that if the user choose square.

after keying in all the coord and computing the area, all the coordinates, name of shape etc would all be stored in Shape2D vector because of "objs.push_back(new Square(*s));"
It all looks OK so far. Have you tried compiling it yet?
@Computergeek01 yup I'm have already compiled it and i'm getting some compilation error

Shape2DLink.cpp: In member function 'void Shape2DLink::InputSensor()':
Shape2DLink.cpp:46:31: error: no match for 'operator*' in '*square'
Shape2DLink.cpp:52:33: error: no match for 'operator*' in '*rect'
Shape2DLink.cpp:58:29: error: no match for 'operator*' in '*cross'


Are 's', 'r' and 'c' pointers to 'square', 'rect' and 'cross'? If they are you can get rid of them because the "new" keyword creates the new object as a pointer. You may need to write a copy constructor for these though, I can never remember that one.

EDIT: Also that error maybe because you're using the wrong operator to assign your pointers maybe? For example, you want Square* s = &square; not something like Square* s = *square;
Last edited on
Topic archived. No new replies allowed.