Grrrr.
I know I have set this problem up oddly. However, the point is to handle objects correctly, not to be the cleanest code. i.e. I’m trying to do this in a kind of round-about way just to ensure that I understand how objects work – and even more importantly, how a vector of objects works.
I feel like I am getting the hard part to work fine (creating objects on the fly, storing them in a vector and calling up details from the objects. However, I have stored a counter integer in a vector and I can’t seem to get it to work correctly. Why store a counter in an object? I want to have the counter in a safe place – away from the other objects, but not as a global variable.
It looks like it is resetting to 0 each time I reach in to get a unique ID number.
Does anyone out there see what I’m doing wrong?
code:
//
// main.cpp
// clowncar
//
// Created by Jack on 5/4/14.
// Copyright (c) 2014 Jack Treml. All rights reserved.
//
/*
1. empty car (0)
2. pick up Pennywise and John Wayne Gacy (2)
3. Pick up Bozo and Krusty (4)
4. Pick up Ronald McDonald (5)
5. Ronald McDonald gets kicked out (4)
6. All the rest stop for a drink at the Alehouse together (0)
*/
#include <iostream>
#include <vector>
usingnamespacestd;
using std::vector;
class IdNumbers{ // a place to store a counter for creating idNumbers
public:
IdNumbers(int x){
absoluteNumber = x;
}
int getANewNumber(void){
cout<<“number is “<<absoluteNumber<<endl;
absoluteNumber = advanceNumber();
cout<<“number is now “<<absoluteNumber<<endl;
returnabsoluteNumber;
}
int advanceNumber(void){
absoluteNumber ++;
returnabsoluteNumber;
}
private:
int absoluteNumber;
};
class Clown{ // the clown objects
public:
Clown(){};
Clown(int clownId){
idNum = clownId;
};
Clown(string nameOfClown, IdNumbers theNumber){
clownName = nameOfClown;
idNum = theNumber.getANewNumber();
};
int getClownId(void){
return idNum;
}
void printClown(void){
cout<<“idNumber = “<<idNum<<endl;
cout<<“name = “<<clownName<<endl;
};
private:
string clownName;
int idNum;
};
//functions
void printVectorContents(vector<Clown>);
int main(int argc, const char * argv[])
{
IdNumbers *absoluteNumbers = newIdNumbers(0); //initializes a running id Number
vector<Clown> clowncar; //sets up a vector for fish
int vectorSize = 0;
vectorSize = clowncar.size(); //get array size
cout<<“**************”<<endl;
cout<<“vector is size: “<<vectorSize<<endl;
Clown pennywise(“Pennywise”, *absoluteNumbers);
clowncar.push_back(pennywise);
Clown jwgacy(“John Wayne Gacy”, *absoluteNumbers);
clowncar.push_back(jwgacy);
vectorSize = clowncar.size(); //get array size
cout<<“**************”<<endl;
cout<<“vector is size: “<<vectorSize<<endl;
printVectorContents(clowncar);
Clown bozo(“Bozo”, *absoluteNumbers);
clowncar.push_back(bozo);
Clown krusty(“Krusty”, *absoluteNumbers);
clowncar.push_back(krusty);
vectorSize = clowncar.size(); //get array size
cout<<“**************”<<endl;
cout<<“vector is size: “<<vectorSize<<endl;
printVectorContents(clowncar);
Clown ronald(“Ronald McDonald”, *absoluteNumbers);
clowncar.push_back(ronald);
vectorSize = clowncar.size(); //get array size
cout<<“**************”<<endl;
cout<<“vector is size: “<<vectorSize<<endl;
printVectorContents(clowncar);
clowncar.pop_back();
vectorSize = clowncar.size(); //get array size
cout<<“**************”<<endl;
cout<<“vector is size: “<<vectorSize<<endl;
printVectorContents(clowncar);
while(clowncar.size() > 0){
clowncar.pop_back();
}
vectorSize = clowncar.size(); //get array size
cout<<“**************”<<endl;
cout<<“vector is size: “<<vectorSize<<endl;
printVectorContents(clowncar);
return 0;
}
//functions
void printVectorContents(vector<Clown> car){
for(int i=0; i < car.size(); i++){
car[i].printClown();
}
}
/* output:
**************
vector is size: 0
number is 0
number is now 1
number is 0
number is now 1
**************
vector is size: 2
idNumber = 1
name = Pennywise
idNumber = 1
name = John Wayne Gacy
number is 0
number is now 1
number is 0
number is now 1
**************
vector is size: 4
idNumber = 1
name = Pennywise
idNumber = 1
name = John Wayne Gacy
idNumber = 1
name = Bozo
idNumber = 1
name = Krusty
number is 0
number is now 1
**************
vector is size: 5
idNumber = 1
name = Pennywise
idNumber = 1
name = John Wayne Gacy
idNumber = 1
name = Bozo
idNumber = 1
name = Krusty
idNumber = 1
name = Ronald McDonald
**************
vector is size: 4
idNumber = 1
name = Pennywise
idNumber = 1
name = John Wayne Gacy
idNumber = 1
name = Bozo
idNumber = 1
name = Krusty
**************
vector is size: 0
Program ended with exit code: 0
*/
Recent Comments