Reputation: 191
I'm beginner in object-oriented programming. As the learning project I've decided to create a little program to manage a company. How to count created objects? I've read about it but I don't understand it. I'd like to request for show me how should I do it using as an example my code:
class Employee
{
public:
std::string name;
int age;
static int id;
Employee::Employee()
{
id++;
}
};
int Employee::id = 0;
std::ostream& operator<<(std::ostream& out, const std::vector<Employee>& v)
{
out << "[";
for (size_t i = 0; i < v.size(); ++i)
{
out << "ID: " << v[i].id << " Name: " << v[i].name << " age: " << v[i].age;
if (i != v.size() - 1)
out << ", ";
}
out << "]";
return out;
}
Employee generate_random_employee(Employee obj)
{
std::vector<std::string> male_names = {"Donald", "Piotr", "James", "Tiny", "Ricky"};
std::vector<std::string> female_names = {"Diana", "Joelle", "Sue", "Karolina"};
std::vector<std::string> lastnames = {"Lester", "Pound", "Park", "Ennis", "Duck",
"Tusk", "Disney", "Jurassic", "Looney-Warde" };
int sex = (rand() % 2) + 0;
if (sex == 1)
{
obj.name = male_names[(rand() % (male_names.size() - 1)) + 0];
}
else
{
obj.name = female_names[(rand() % (female_names.size() - 1)) + 0];
}
obj.name += " " + lastnames[(rand() % (lastnames.size() - 1)) + 0];
obj.age = (rand() % 24) + 25;
return obj;
}
int main()
{
srand(time(NULL));
std::vector<Employee> employees;
Employee some_new_employee;
for(int i = 0; i < 2; i++)
{
some_new_employee = generate_random_employee(some_new_employee); //randomly generate a new employee. It is Employee type function
employees.push_back(some_new_employee);
}
std::cout << employees << std::endl;
}
at the moment the program constantly attribute 1 as the ID for all objects
Upvotes: 0
Views: 4195
Reputation: 4875
generate_random_employee(some_new_employee);
employees.push_back(some_new_employee);
None of those lines calls the constructor.
You should overload the assignment operator or the copy constructor to also increase the count.
And you can't have a static id... Static means that all your instances will have the same id...
You want a satic counter and use it to assign the id on construction.
Upvotes: 2
Reputation: 5576
To get a unique id for each employee try:
class Employee
{
public:
std::string name;
int age;
int id;
static int ID;
Employee()
{
id = ID++;
}
};
int Employee::ID = 0;
std::ostream& operator<<(std::ostream& out, const std::vector<Employee>& v)
{
// ...
return out;
}
Main (simplified)
std::vector<Employee> employees;
for(int i = 0; i < 4; i++)
{
Employee e;
employees.push_back(e);
}
std::cout << employees << std::endl;
output:
[ID: 0 Name: age: 0, ID: 1 Name: age: 0, ID: 2 Name: age: 0, ID: 3 Name: age: 0]
Key idea: each instance of the object requires a unique id. A static is just the opposite ... a common value for all instances.
The ctor sets its own id AND increments the static ID.
Note that ID represents how many have been contructed, but not how many might be existing (consider destructors, temps, copies, etc).
Upvotes: 3