Success!

Successfully added object counter as a static variable inside an object. Used to generate unique, sequential ID numbers for a vector of other objects.

Image

As easy as 00 01 11 …

This means, that after an afternoon of work and reflection, I was finally able to count up past “1”.

I need to come up with a way to build this project into something larger to ensure that I can manipulate the objects in my vector at will.

Perhaps the best thing to add is a dialog asking the user for the name of a clown to kick out, then ice out that clown, kick them out of the vector and report on everyone’s ID number (should remain unchanged) and the size of the vector (it should shrink by one).

Here’s the completed project:

//

//  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(){

    }

    static int getANewNumber(){

        counter++;

        return counter;

    }

protected:

     static int counter;

};

class Clown{   // the clown objects

public:

    Clown(){};

    ~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 IdNumbers ::counter = 0;

int main(int argc, const char * argv[])

{

    IdNumbers theCounter;

    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”, theCounter);

    clowncar.push_back(pennywise);

    Clown jwgacy(“John Wayne Gacy”, theCounter);

    clowncar.push_back(jwgacy);

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    Clown bozo(“Bozo”, theCounter);

    clowncar.push_back(bozo);

    Clown krusty(“Krusty”, theCounter);

    clowncar.push_back(krusty);

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    Clown ronald(“Ronald McDonald”, theCounter);

    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

 **************

 vector is size: 2

 idNumber = 1

 name = Pennywise

 idNumber = 2

 name = John Wayne Gacy

 **************

 vector is size: 4

 idNumber = 1

 name = Pennywise

 idNumber = 2

 name = John Wayne Gacy

 idNumber = 3

 name = Bozo

 idNumber = 4

 name = Krusty

 **************

 vector is size: 5

 idNumber = 1

 name = Pennywise

 idNumber = 2

 name = John Wayne Gacy

 idNumber = 3

 name = Bozo

 idNumber = 4

 name = Krusty

 idNumber = 5

 name = Ronald McDonald

 **************

 vector is size: 4

 idNumber = 1

 name = Pennywise

 idNumber = 2

 name = John Wayne Gacy

 idNumber = 3

 name = Bozo

 idNumber = 4

 name = Krusty

 **************

 vector is size: 0

 Program ended with exit code: 0

 */

 

 

Advertisements

1 Comment (+add yours?)

  1. Trackback: Clowns, Psychology and C++ | DownHouseSoftware

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: