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?