changing the value of an attribute of a vector after calculating

hi i am doing a assignment on polymorphism and i am stuck, i can calculate the area but i cannot set the value of the area. How do i get and set certain values of my vector of unique_ptr?

I have only coded my rectangle.

Display
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>
#include "Rectangle.h"
#include "Square.h"
#include "Cross.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

void print(vector<unique_ptr<ShapeTwoD>>& newCoords)
{
	for (int i=0;i<newCoords.size();i++)
	{
		(*newCoords[i]).toString();
	}
}

void compute(vector<unique_ptr<ShapeTwoD>>& newCoords)
{
	ShapeTwoD sh;
	double a;
	for (int i=0;i<newCoords.size();i++)
	{
		a =(*newCoords[i]).computeArea();
		//(*newCoords[i]).setArea(a);
	}
}

void input(vector<unique_ptr<ShapeTwoD>>& newCoords)
{
	
	
	int xCoord,yCoord,x1Coord,y1Coord,x2Coord,y2Coord,x3Coord,y3Coord;
	string s;
	double a;
	bool b = true;
		cout<<"name?"<<endl;
		getline(cin,s);
		
		if(s=="Rectangle")
		{
		cout<<"Please enter value of x"<<endl;
		cin>>xCoord;
		cout<<"Please enter value of y"<<endl;
		cin>>yCoord;
		cout<<"Please enter value of x1"<<endl;
		cin>>x1Coord;
		cout<<"Please enter value of y1"<<endl;
		cin>>y1Coord;
		cout<<"Please enter value of x2"<<endl;
		cin>>x2Coord;
		cout<<"Please enter value of y2"<<endl;
		cin>>y2Coord;
		cout<<"Please enter value of x3"<<endl;
		cin>>x3Coord;
		cout<<"Please enter value of y3"<<endl;
		cin>>y3Coord;
		//Square sq(s,b,x,y);
		newCoords.emplace_back(new Rectangle(s,b,xCoord,yCoord,x1Coord,y1Coord,x2Coord,y2Coord,x3Coord,y3Coord,a));
	}
	if(s=="Square")
		{
		cout<<"Please enter value of x"<<endl;
		cin>>xCoord;
		cout<<"Please enter value of y"<<endl;
		cin>>yCoord;
		cout<<"Please enter value of x1"<<endl;
		cin>>x1Coord;
		cout<<"Please enter value of y1"<<endl;
		cin>>y1Coord;
		cout<<"Please enter value of x2"<<endl;
		cin>>x2Coord;
		cout<<"Please enter value of y2"<<endl;
		cin>>y2Coord;
		cout<<"Please enter value of x3"<<endl;
		cin>>x3Coord;
		cout<<"Please enter value of y3"<<endl;
		cin>>y3Coord;
		//Square sq(s,b,x,y);
		newCoords.emplace_back(new Square(s,b,xCoord,yCoord,x1Coord,y1Coord,x2Coord,y2Coord,x3Coord,y3Coord,a));
	}
		/*cout<<"name?"<<endl;
	
		getline(cin,s);
		cout<<"Please enter value of x"<<endl;
		cin>>x;
		cout<<"Please enter value of y"<<endl;
		cin>>y;
		
		//Square sq(s,b,x,y);
		newCoords.emplace_back(new Rectangle(s,b,x,y));
	*/
}
	


int main(int argc, char** argv) {
	
	//ShapeTwoD sh;
	stringstream stream;
	int choice;
	string choiceString; 
	vector<unique_ptr<ShapeTwoD>>shape;
	
	
	
	do{

	cout << "1)	Input sensor data" << endl;
	cout << "2)	Compute area (for all records)" << endl;
	cout << "3)	Print shapes report" << endl;
	cout << "4)	Sort shape data" << endl;
	cout << "5)	Quit" << endl;
	cout <<endl;
	cout << "Please enter your choice : ";
	
	stream.clear();//clears stringstream
	getline(cin,choiceString);//gets whole line of input and store it into a string
	stream.str(choiceString);//stores string into stringstream
	choice= atoi(stream.str().c_str());//convert string into int
				
	while(!(stream >> choice))//loop if convert from string to int fail
	{
			
	cout<<"Please enter a valid choice"<<endl;
	stream.clear();//clears stringstream
	getline(cin,choiceString);//gets whole line of input and store it into a string
	stream.str(choiceString);//stores string into stringstream
	choice= atoi(stream.str().c_str());//convert string into int
					
					
				}
	cout<<endl;
	cout<<endl;
	
	switch(choice)
	{
		case 1:
			{
				input(shape);	
			}break;
		case 2:
			{
				compute(shape);
			}break;
		case 3:
			{
				print(shape);
			}break;
		case 4:
			{
			}break;
		case 5:
			{
			}break;
				}	
}while(choice!=5);	
	return 0;
}


Rectangle.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
#include "ShapeTwoD.h"

class Rectangle:public ShapeTwoD{
		private:
		int x,y,x1,x2,x3,y1,y2,y3;
		double area;
		public:
		
		Rectangle(string,bool,int,int,int,int,int,int,int,int,double);
		double computeArea();
		string toString();
		void setArea(double);
		double getArea();
		int getX();
		int getY();
		int getX1();
		int getY1();
		int getX2();
		int getY2();
		int getX3();
		int getY3();
		void setX(int);
		void setY(int);
		void setX1(int);
		void setY1(int);
		void setX2(int);
		void setY2(int);
		void setX3(int);
		void setY3(int);
	};


Rectangle.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "Rectangle.h"

Rectangle::Rectangle(string sName,bool contain,int coordX, int coordY,int coordX1, int coordY1,int coordX2, int coordY2,int coordX3, int coordY3,double sArea):ShapeTwoD(sName,contain)
{
	setX(coordX);
	setY(coordY);
	setX1(coordX1);
	setY1(coordY1);
	setX2(coordX2);
	setY2(coordY2);
	setX3(coordX3);
	setY3(coordY3);
}

int Rectangle::getX()
{
	return x;
}
int Rectangle::getY()
{
	return y;
}
int Rectangle::getX1()
{
	return x1;
}
int Rectangle::getY1()
{
	return y1;
}
int Rectangle::getX2()
{
	return x2;
}
int Rectangle::getY2()
{
	return y2;
}
int Rectangle::getX3()
{
	return x3;
}
int Rectangle::getY3()
{
	return y3;
}

double Rectangle::getArea()
{
	return area;
}



void Rectangle::setArea(double sArea)
{
	area=sArea;
}

void Rectangle::setX(int coordX)
{
	x=coordX;
}
void Rectangle::setY(int coordY)
{
	y=coordY;
}
void Rectangle::setX1(int coordX)
{
	x1=coordX;
}
void Rectangle::setY1(int coordY)
{
	y1=coordY;
}
void Rectangle::setX2(int coordX)
{
	x2=coordX;
}
void Rectangle::setY2(int coordY)
{
	y2=coordY;
}
void Rectangle::setX3(int coordX)
{
	x3=coordX;
}
void Rectangle::setY3(int coordY)
{
	y3=coordY;
}

double Rectangle::computeArea()
{
	double area;
	cout<<"I'm a rectangle"<<endl;
	
	area = ((sqrt(pow((x-x1),2)+(pow((y-y1),2)))+(sqrt(pow((x-x3),2)+(pow((y-y3),2))))));
	cout<<area<<endl;
	setArea(area);
	return area;
}

string Rectangle::toString()
{
	cout<<"The area is:" <<area<<endl;
	
}
Step 1.
Make sure that the functions from in object that you want to use from outside the object are public and not private.

Possible step 2.
(*vectorOfPointers[i]).functionName();
gets you the actual object that the pointer in position i from the vector is pointing to and then calls its member function functionName() (if that function is declared as public).
You could also just do
vectorOfPointers[i]->functionName();

Note that you have to use a -> instead of a . if you are using a pointer instead of the actual object.
Erm not to be rude but i am trying to set the area of what i have calculated through compute(), the calculation part i ave no problems. The problematic part is that i want to set the value of area into the vector. And at the end able to print out the variables in the format i want

eg.cout<<"the area is: " << area<<endl;
cout<<"At coordinates ("<<x<<","<<y<<")"<<endl;
I think I misunderstood your question then.

Your Rectangle class has a private member area and the member function Rectangle::computeArea has a local variable area. To save the result of your calculation to the private variable instead of to the local variable, just remove the local variable in the Rectangle::computeArea function.

OP: you already have all the information you need through the class constructors, you just have to create the appropriate class objects and access the right class methods through indirection of the unique pointers. A stylized outline is shown below:

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
#include<iostream>
#include<vector>
#include<memory>
using namespace std;

class ShapeTwoD{
    public:
    ShapeTwoD(){}
    ShapeTwoD(int, int, int, int){}
    void getArea(){}
    void printArea () {}
};
class Rectangle : public ShapeTwoD{
    public:
    Rectangle(){}
};

int main(){
vector<unique_ptr<ShapeTwoD>> shape;
shape.push_back(unique_ptr<ShapeTwoD> (new Rectangle));
shape.push_back(unique_ptr<ShapeTwoD> (new ShapeTwoD (3, 4, 5, 6)));

shape[0]->getArea();//if getArea() is declared virtual and defined with Rectangle then the pointer will pick up the correct definition; 
shape[1]->printArea();
}

1
2
shape[0]->getArea();//if getArea() is declared virtual and defined with Rectangle then the pointer will pick up the correct definition; 
shape[1]->printArea();


i don't get this part, is it the same as my code,
 
(*newCoords[i]).computeArea();
As mentioned in my post, my code is a stylized outline to show how to get and print the area. You'd have to do the final bit yourself and adapt them for use in your classes.
The vector contains pointers to objects of type ShapeTwoD, so you can only use the functions that are declared public in ShapeTwoD, computeArea() is not defined there, so you can't use it.

Try changing the function name in your Rectangle class from computeArea() to getArea() and add "virtual" in front of the declaration in the ShapeTwoD class to indicate that that function may be overwritten by classes that extend ShapeTwoD.

Then you should be able to use either:
(*newCoords[i]).getArea();
or
newCoords[i]->getArea();
Topic archived. No new replies allowed.