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 6802329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:12:37+00:00 2026-05-26T19:12:37+00:00

Note: This is a homework assignment. I have two classes, one inherits from the

  • 0

Note: This is a homework assignment.

I have two classes, one inherits from the other. The assignment tells us to make the classes able to keep track of the total amount of instances created of each class.

class FirstClass 
{
  private static int _instancesCreated = 0;
  public FirstClass()
  {
    _instancesCreated++;
  }
}

That piece of code works well as far as I know. The problem is that I have a subclass which has to call the constructor of it’s parent class.

class SecondClass:FirstClass 
{
  public SecondClass() : base()
  {
  }
}

So now the problem becomes how to properly separate the two. Since I have to call the parent constructor the _instancesCreated for that class will become incorrect, although I suppose it’s true in a sense…?

Thanks for your help

  • 1 1 Answer
  • 1 View
  • 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-05-26T19:12:37+00:00Added an answer on May 26, 2026 at 7:12 pm

    The easiest way is to make _instancesCreated protected, and then decrement it in each of your subclasses constructors. That’s how you make _instancesCreated store only the number of base class instances. For counting subclasses instances you should add static fields for each of them, and increment them in constructors, just like you did in your base class.

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

Sidebar

Related Questions

I have a homework assignment, and i am finished other then one question (see
NOTE: THIS IS NOT HOMEWORK IT IS FROM A PRACTICE EXAM GIVEN TO US
Please note that this is not homework and i did search before starting this
(Note: This is for MySQL's SQL, not SQL Server.) I have a database column
Note: This is the opposite direction to most similar questions! I have an iPhone
Note: this was inspired by WebBrowser Event Properties? Why am I able to access
I have a great idea for a homework note pad and I wrote the
I have this question: suppose that the size of char is one byte and
**NOTE: This is related to a college assignment. I'm only requesting some guidance in
I'm looking at all different sorts. Note that this is not homework (I'm in

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.