-3

This one the question from my assignment i can't understand what it's mean can anyone help me out with this please?

  1. Write a class that contains two class data members numBorn and numliving. The value of numBorn should be equal to the number of object of the class that have been instanced. The value of numLiving should be equal to the total number of objects in existence currently(i.e the object that have been constructed but not yet destructed.)
  • 1
    If you don't understand the assignment you really should discuss it with your instructor or their assistant. – Captain Obvlious May 14 '18 at 18:58
  • IMHO and based on your response to the answer you **need** to talk to your instructor or at least someone else in the class. If you don't you will continue to be lost on this and other C++ topics as much you are today. – Captain Obvlious May 14 '18 at 21:09

1 Answers1

1

You'll need to make your variables static data members of your class. Then your constructor(s) and destructor will increment and decrement as needed.

class A
{
public:
    static std::size_t numBorn;
    static std::size_t numLiving;

    A()
    {
        ++numBorn;
        ++numLiving;
    }

    ~A()
    {
        --numLiving;
    }
};

std::size_t A::numBorn = 0;
std::size_t A::numLiving = 0;

As a small demo

int main()
{
    A a1;
    A a2;

    {
        A a3;
        std::cout << "living: " << A::numLiving << "  born: " << A::numBorn << '\n';
    }

    std::cout << "living: " << A::numLiving << "  born: " << A::numBorn << '\n';
}

will output

living: 3  born: 3
living: 2  born: 3

Note that when a3 falls out of scope, the numLiving is decremented from its destructor.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • I have no idea, I'm not in your class, and I'm not your professor. This code does what described in your problem statement. – Cory Kramer May 14 '18 at 20:38