Member Avatar for Smartflight

Ok, I received an assignment today, and we have our weekly test tomorrow.
I was going through the 25 questions, and this is my first problem...

The question is as follows:

Write a program to keep count of created objects for a class. Make suitable assumptions.
For example, in the class Student, assign roll number to all students starting from 1 in increasing order at the time of creating the objects.

Now I am really clueless... thanks to "at the time of creating the objects."
Any help?

Recommended Answers

All 6 Replies

Maybe you can use a static member variable and increment it by 1, everytime an object is created. (ie: Everytime the constructor is called ;) )

Member Avatar for Smartflight

Can this not be done without a constructor?
Because we haven't learnt that yet... it's our next chapter, though.

Can this not be done without a constructor?
Because we haven't learnt that yet... it's our next chapter, though.

Yes, it can. A viable solution to this problem is to create a base counter class that is templatized. But I assume you haven't learn that before. So for now you can just create a counter variable and increment it in the proper place and decrement it in proper place. As suggested, use a static count variable, ex.

class People{
private:
 static unsigned PEOPLE_COUNT; //this creates one people_count variable for all class People class object
public:
 People(); //increment it here
 ~People(); //decrement here
 People(const People& p); //what is this, what should u do here?
 People& operator=(const People& p) ; //what should u do here?
 //...are there any other place you should worry about?
};

Generally, to be safe, you implement these kinds of "instance counters" wrapped in a factory function. Typically, you also don't want spurious copies and things to be created, so you make the class non-copyable. This way you count only the "uniquely new" objects that were created, not copies and temporaries. By making the class non-copyable, you force it to be used via a pointer, and the only function to create that pointer is your factory function, which records the instances created:

class People {
  private:
    int ssn;
    People(int aSSN) : ssn(aSSN) { }; //constructible only from factory Create().
    ~People() { }; //destructible only from factory Destroy().
    People(const People); //non-copyable.
    People& operator=(const People&); //non-assignable.
  public:
    static People* Create() {
      static int objectCount = 0;
      return new People( ++objectCount );
    };
    static void Destroy(People* p) { delete p; };
};

int main() {
  People p1 = People::Create();
  People::Destroy(p1);
  return 0;
};

Should u not decrement objectCount in Destroy function?

>>Should u not decrement objectCount in Destroy function?

The OP does not require that. If you were to assign a new Student ID for every new student, you probably don't want to decrement the counter on destruction (e.g. you create two students, with ID 1 and 2, then delete Student 1, and create a third Student, which will result in two Students with the ID 2). But, of course, for counting existing instances or reference counting of an object (e.g. in a smart pointer), then, yes, you should decrement the counter on destruction, which results in the following:

class People {
  private:
    People() { }; //constructible only from factory Create().
    ~People() { }; //destructible only from factory Destroy().
    People(const People); //non-copyable.
    People& operator=(const People&); //non-assignable.
    static int& getActivePeopleCount_impl() { 
      static int objectCount = 0;
      return objectCount;
    };
  public:
    static int getActivePeopleCount() { return getActivePeopleCount_impl(); };
    static People* Create() {
      ++getActivePeopleCount_impl();
      return new People();
    };
    static void Destroy(People* p) {
      --getActivePeopleCount_impl(); 
      delete p; 
    };
};

int main() {
  People p1 = People::Create();
  std::cout << "There are " << People::getActivePeopleCount() << " active people, now." << std::endl;
  People::Destroy(p1);
  std::cout << "There are " << People::getActivePeopleCount() << " active people, now." << std::endl;
  return 0;
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.