Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8919707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:05:38+00:00 2026-06-15T06:05:38+00:00

how can one create a class with a constructor that counts the number of

  • 0

how can one create a class with a constructor that counts the number of people who are alive,if i have 10 people it should count the quantity of people who are alive,people should be objects in this example,lets say i have a class man like this below

class man{

     private:
     string name;

   man(string name=""){
     cout<<"there 10 people alive"<<endl;

~man(){}
};
int main(){


}

i am getting confused on how to go about it,i really need a simple example i want to use the set and get methods

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:05:39+00:00Added an answer on June 15, 2026 at 6:05 am

    The sane version of this would be to store your man objects in a collection, say a std::vector. From this std::vector, you could use size on the vector to fetch the number of items contained in it.

    Otherwise, go with a solution that doesn’t make sense in this context, which would be to store a static variable that is increased in the constructor and decreased in the destructor.

    By the way, your man class has a few mistakes, the constructor is private and you’re missing brackets… here’s a simple version of what you’re looking for:

    class man
    {
    private:
        std::string mName;
    public:
        man(std::string name="") : mName(name)
        {
        }
    
        const std::string& GetName() const
        {
            return mName;
        }
    };
    
    int main(int argc, char *argv[])
    {
        man Bob = man("Bob");
        man Jimmy = man("Jimmy");
    
        std::vector<man> men; // This will copy the man object, might want to consider using pointers
        men.push_back(Bob);
        men.push_back(Jimmy);
    
        std::cout << "There is " << men.size() << " men." << std::endl;
    
        std::cout << "Known men are:" << std::endl;
        for(std::vector<man>::const_iterator itor = men.begin(); itor != men.end(); ++itor)
        {
            std::cout << itor->GetName() << std::endl;
        }
    
        // You could also iterate using something like for(unsigned int i = 0; i < men.size(); ++i)
    }
    

    Edit:

    Lets add how to handle it if you’re going with a static variable… see the following scenario (I collapsed much of the brackets just to make it shorter)

    See the following code:

    class man
    {
    private:
        static unsigned int sCount;
        std::string mName;
    public:
        man(std::string name="") : mName(name) { ++sCount; }
        ~man() { --sCount; }
        const std::string& GetName() const { return mName; }
        static unsigned int Count() { return sCount; }
    };
    
    unsigned int man::sCount = 0;
    
    int main(int argc, char *argv[])
    {
        man Bob = man("Bob");
        man Jimmy = man("Jimmy");
        std::cout << "There is " << man::Count() << " men" << std::endl;
        std::vector<man> men; // This will copy the man object, might want to consider using pointers
        men.push_back(Bob);
        men.push_back(Jimmy);
        std::cout << "There is " << man::Count() << " men" << std::endl;
    }
    

    The output to that will be

    There is 2 men
    There is 1 men
    

    Wait… what?!? Where did we lose a man? Well, this happened in the vector resizing, since we didn’t define a copy constructor, the compiler did it for us, but the compiler wasn’t aware that we wanted to increment sCount, so when the vector resized, new objects were created, old ones destructed, and sCount didn’t get updated properly.

    By changing our man class to:

    class man
    {
    private:
        static unsigned int sCount;
        std::string mName;
    public:
        man(std::string name="") : mName(name) { ++sCount; }
        man(const man& in_man) : mName(in_man.mName) { ++sCount; }
        ~man() { --sCount; }
        const std::string& GetName() const { return mName; }
        static unsigned int Count() { return sCount; }
    };
    

    We now have:

    There is 2 men
    There is 4 men
    

    But why?!?. There are 2 man objects on the stack, Bob and Jimmy. Then there’s 2 copy of these these objects in std::vector<man> men because the vector contains “objects of type man“.

    If we changed this to pointers:

    std::vector<man*> men; 
    men.push_back(&Bob);
    men.push_back(&Jimmy);
    

    We now have the following output, which is what we’re looking for:

    There is 2 men
    There is 2 men
    

    Hope this clears things up for you!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One can create an anonymous object that is initialized through constructor parameters, such as
I want to create a class that can use one of four algorithms (and
Let's say that I have a class with a constructor that takes one or
Can we able to create two instance of log4j in class one to write
There seem to be a number of different ways in which one can create
Possible Duplicate: Why can’t I create an abstract constructor on an abstract C# class?
I have a base class called SCO that a number of classes inherit from.
I want create constructor that will take one or more integers and save it
In Scala, I can create a method that takes more than one argument list:
I know that calling a virtual method from a base class constructor can be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.