Calling An Individual Class When There Is Many With Same Name

Calling An Individual Class When There Is Many With Same Name?

The below code creates many instances of the Property class. However how would I go about calling them individually?
1
2
3
4
5
6
7
8
9
  for (size_t i = 0; i + 5 < myPropertyClassCreator.size(); i += 6) {
		Property propertyCard(
			stringToInt(myPropertyClassCreator[i + 0]),
			myPropertyClassCreator[i + 1],
			myPropertyClassCreator[i + 2],
			stringToInt(myPropertyClassCreator[i + 3]),
			stringToInt(myPropertyClassCreator[i + 4]),
			stringToInt(myPropertyClassCreator[i + 5]));
	}
Ladies and Gentlemen,

@Moschops answered the question below.
Last edited on
Every one of the Property objects you create on line 2 (with constructor parameters going from line 3 to line 8) ceases to exist when the loop goes around again.

You create one (named propertyCard), and then it is destructed, and then you create one (named propertyCard), and it is destructed, and you create one (named propertyCard), and it is destructed, and so on. After line 9, they have all been destructed.
Last edited on
@Moschops

so basically I am just rewriting the class everytime I go through it.
should I declare a new Property and do it like that?
If you want to have more than one at any one time, you'll need to either give them different names, or don't bother with names and just put them into a vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
vector<Property> vectorOfProperties;
for (size_t i = 0; i + 5 < myPropertyClassCreator.size(); i += 6) {
		Property propertyCard(
			stringToInt(myPropertyClassCreator[i + 0]),
			myPropertyClassCreator[i + 1],
			myPropertyClassCreator[i + 2],
			stringToInt(myPropertyClassCreator[i + 3]),
			stringToInt(myPropertyClassCreator[i + 4]),
			stringToInt(myPropertyClassCreator[i + 5]));
vectorOfProperties.push_back(propertyCard);
	}

// now vectorOfProperties contains all the Property objects that were made. 
@Moschops thank you for the reply, It says I need to create a pointer as the getName function requires it. I thought doing it the vector way would mean I wouldn't have to have a pointer and do polymorphism.

1
2
3
        for (int j = 0; j < vectorOfProperties.size(); j++){
		cout << vectorOfProperties[j].getName << endl; //says I need pointer
	}


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
class Property : public Card{
private:
	int identificationNum;
	string propertyName;
	int propertyCost;
	int propertyRent;
	int propertyColour;
public:
	//constructor
	Property::Property();
	Property::Property(int inputIdentificationNum, string inputFName, string inputSName, int inputCost, int inputPropertyRent, int inputPropertyColour){
		setIdentificationNum(inputIdentificationNum);
		setFirstName(inputFName, inputSName);
		setPropertyCost(inputCost);
		setPropertyRent(inputPropertyRent);
		setPropertyColour(inputPropertyColour);
		cout << propertyName + " Has been set up. ID : COST : RENT : COLOUR :" << identificationNum << " " << propertyCost << " " << propertyRent << " " << propertyColour << endl;
	}
	//set data
	void setIdentificationNum(int inputIdentificationNum){
		identificationNum = inputIdentificationNum;
	}
	void setFirstName(string inputFName, string inputSName){
		string nameCombined = inputFName + " " + inputSName;
		propertyName = nameCombined;
	}
	void setPropertyCost(int inputCost){
		propertyCost = inputCost;
	}
	void setPropertyRent(int inputPropertyRent){
		propertyRent = inputPropertyRent;
	}
	void setPropertyColour(int inputPropertyColour){
		propertyColour = inputPropertyColour;
	}
	//retreive data
	int getIdentificationNum() {
		return identificationNum;
	}
	string getName(){
		return propertyName;
	}
	int getPropertyCost(){
		return propertyCost;
	}
	int getPropertyRent(){
		return propertyRent;
	}
	int getPropertyColour(){
		return propertyColour;
	}
};


sorry for being a pain also
Last edited on
Topic archived. No new replies allowed.