Like Tree4Likes
  • 2 Post By grumpy
  • 2 Post By hk_mp5kpdw

Auto Increment static variables

This is a discussion on Auto Increment static variables within the C++ Programming forums, part of the General Programming Boards category; I am trying to figure out using static variables to auto increment each new instance of a class. Here is ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    21

    Auto Increment static variables

    I am trying to figure out using static variables to auto increment each new instance of a class. Here is what I have so far:

    Code:
    People.h
    
    #ifndef PEOPLE_H
    #define PEOPLE_H
    
    #include <string>
    using namespace std;
    
    class People
    {
    private:
    	int id;
    	string name;
    	int age;
    	string instrument;
    public:
    	People(string n, int a, string i);
    	void setName(string newName);
    	void setAge(int newAge);
    	void setInstrument(string newInstrument);
    
    	string getName();
    	int getAge();
    	string getInstrument();
    	int getID();
    
    	void printAll();
    };
    #endif
    Code:
    People.cpp
    
    #include "People.h"
    #include <iostream>
    
    using namespace std;
    
    People::People(string n, int a, string i)
    {
    	name = n;
    	age = a;
    	instrument = i;
    	id++;
    }
    
    void People::setName(string newName)
    {
    	name = newName;
    }
    
    void People::setAge(int newAge)
    {
    	age = newAge;
    }
    
    void People::setInstrument(string newInstrument)
    {
    	instrument = newInstrument;
    }
    
    string People::getName()
    {
    	return name;
    }
    
    int People::getAge()
    {
    	return age;
    }
    
    string People::getInstrument()
    {
    	return instrument;
    }
    
    int People::getID()
    {
    	return id;
    }
    
    void People::printAll()
    {
    	cout << id << name << " is " << age << " years old and plays the " << instrument << endl;
    }
    Code:
    Main.cpp
    
    #include "People.h"
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	People luke("Luke", 16, "Piano");
    	People alex("Alex", 17, "Piano");
    	People ewan("Ewan", 19, "Saxophone");
    
    	luke.printAll();
    	alex.printAll();
    	ewan.printAll();
    
    	cin.get();
    	return 0;
    }
    To be fair I found other examples for static variables but I am probably lost in the separate files setup I try to use. Any hints how/where I should be using the static variables?

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Make a simple test case first for not getting confused.

    In this case, making the id static would do what you want.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    You're almost there!

    In people.h, you declare the class variable as
    static int id;

    In people.cpp, you just add this line.
    int People::id = 0;


    Then the results look like this.
    $ g++ foo.cpp
    $ ./a.out
    3Luke is 16 years old and plays the Piano
    3Alex is 17 years old and plays the Piano
    3Ewan is 19 years old and plays the Saxophone
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    if you want to get a class's member , you can declare a static variable.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    No hengshan, static member data is shared among all of the objects. It is specifically defined wherever (in this case) int People::id = 0; is, and that memory is what each People object uses for the member id.

    If you want to "get a class's member," you should use the class interface; basically, the public member function that returns the member.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Quote Originally Posted by Salem View Post
    You're almost there!

    In people.h, you declare the class variable as
    static int id;

    In people.cpp, you just add this line.
    int People::id = 0;


    Then the results look like this.
    $ g++ foo.cpp
    $ ./a.out
    3Luke is 16 years old and plays the Piano
    3Alex is 17 years old and plays the Piano
    3Ewan is 19 years old and plays the Saxophone
    Close but, assuming the aim is that every object of type People has a unique id number ....

    Code:
    // in People.h
    
    class People
    {
    private:
    	int id;
            static int id_counter;
    	string name;
    	int age;
    	string instrument;
    public:
    	People(string n, int a, string i);
    
            // other stuff
    
    	int getID();
    
    	void printAll();
    };
    and in the .cpp file.
    Code:
    int People::id_counter = 0;
    
    //   and somewhere in EVERY accessible constructor
    
        id = id_counter++;
    Note that comment about what is needed in EVERY constructor. It will be necessary to implement a copy constructor and assignment operator to ensure both id and static_id are maintained appropriately (or, alternatively, declare the copy constructor and assignment operator as private, and not implement them).

    All bets are off in the above if the People class is used from multiple threads though.
    Salem and AndrewHunter like this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Avoid putting a using namespace std statement in your header file, you already have one in the CPP file that takes care of that code. For the objects in the header itself you should explicitly qualify those with std:: as needed.
    Salem and Elysia like this.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Increment number of variables?
    By JonathanS in forum C++ Programming
    Replies: 1
    Last Post: 10-04-2011, 07:57 PM
  2. static variables
    By frs in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2010, 09:44 AM
  3. Calling non-static functions on static variables
    By pandu in forum C++ Programming
    Replies: 14
    Last Post: 06-19-2008, 04:07 AM
  4. Replies: 2
    Last Post: 10-02-2004, 11:12 AM
  5. C++ Builder auto-increment
    By iain in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-16-2002, 02:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21