Question

Make a class, named "ConstructorCounter", that has a function member named "num_constructed". This function takes no parameters, but returns an int that is the number of instances of the class ConstructionCounter. Hint: you will need to use non-synthetic methods and statics to solve this problem.

all in one file, and they should be in the class. do not need main func, but you can have main to test them.

INPUT OF THE TEST CASE ASSERT_EQ(0, ConstructorCounter::num_constructed(); 1 ConstructorCounter c; 2 3 ASSERT_EQ(1, ConstructINPUT OF THE TEST CASE 1 ASSERT_EQ(0, ConstructorCounter::num_constructed( )); 2 ConstructorCounter c; ASSERT_EQ(1, ConstructINPUT OF THE TEST CASE 1 ASSERT_EQ(0, ConstructorCounter::num_constructed (O); 2 ConstructorCounter c; 3 ASSERT_EQ(1, Constru

INPUT OF THE TEST CASE ASSERT_EQ(0, ConstructorCounter::num_constructed(); 1 ConstructorCounter c; 2 3 ASSERT_EQ(1, ConstructorCounter::num_constructed();
INPUT OF THE TEST CASE 1 ASSERT_EQ(0, ConstructorCounter::num_constructed( )); 2 ConstructorCounter c; ASSERT_EQ(1, ConstructorCounter:num_constructed ( ) ); 4 ConstructorCounter d; 5 ASSERT_EQ(2, ConstructorCounter::num_constructed()); ASSERT_EQ(2, ConstructorCounter::num_constructed()); 7 { Constructor Counter e; ASSERT_EQ(3, ConstructorCounter::num_constructed()); 10 } ASSERT_EQ(2, ConstructorCounter::num_constructed(); 11
INPUT OF THE TEST CASE 1 ASSERT_EQ(0, ConstructorCounter::num_constructed (O); 2 ConstructorCounter c; 3 ASSERT_EQ(1, ConstructorCounter::num_constructed() ); 4 ConstructorCounter d(c); ASSERT_EQ(2, ConstructorCounter::num_constructed()); ASSERT_EQ(2, ConstructorCounter::num_constructed( ) ); 7 { ConstructorCounter e = d; ASSERT_EQ(3, ConstructorCounter::num_constructed()); 10 } ASSERT_EQ(2, ConstructorCounter::num_constructed()); 11
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

Code:

#include <iostream>
class ConstructorCounter {
public:
   ConstructorCounter() { ++count; }
   ConstructorCounter(const ConstructorCounter&) { ++count; }
   ~ConstructorCounter() { --count; }

   static size_t num_constructed()
   {
       return count;
   }

private:
   static size_t count;
};
size_t ConstructorCounter::count = 0;

// Main function to test implementation
int main(int argc, const char** argv)
{
   ConstructorCounter a, b, c, d;
   std::cout << ConstructorCounter::num_constructed() << std::endl;
   ConstructorCounter e(a);
   std::cout << ConstructorCounter::num_constructed() << std::endl;
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Make a class, named "ConstructorCounter", that has a function member named "num_constructed". This function takes no...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design a class Holiday that represents a holiday during the year. This class has three private...

    Design a class Holiday that represents a holiday during the year. This class has three private data members: name: A string that represents the name of holiday. day: An integer that holds the day of the month of holiday. month: A string that holds the month the holiday is in. Write a default constructor that initializes each data member of class such that name with NULL, day with 0 and month with NULL Holiday() Write a constructor that accepts the...

  • Write a class declaration named Circle with a private member variable named radius.

     in C++ Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159*radius*radius. Add a default constructor to the Circle class. The constructor should initialize the radius member to 0. Add an overloaded constructor to the Circle class. The constructor should accept an argument and assign its value to the radius...

  • Write a class declaration named Circle with a private member variable named radius.

    CODE IN C++Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159 * radius * radius. Add a default constructor the Circle class. The constructor should initialize the radius member to 0. Add an overloaded constructor to the Circle class. The constructor should accept an argument and assign radius...

  • Write a C++ program Write a class named Employee that has the following member variables: name...

    Write a C++ program Write a class named Employee that has the following member variables: name A string that holds the employee name idNumber An int to hold employee id number department A string to hold the name of the department position A string to hold the job position The class will have three constructors: 1. A constructor that accepts all the internal data such as: Employee susan("Susan Meyers", 47899, "Accounting", "Vice President"); 2. A constructor that accepts some of...

  • Please use C++. Write a class named Circle that has a double data member named radius...

    Please use C++. Write a class named Circle that has a double data member named radius and a static double data member named maxRadius. It should have a default constructor that initializes the radius to 1.0. It should have a constructor that takes a double and uses it to initialize the radius. It should have a method called calcArea that returns the area of the Circle (use 3.14159 for pi). It should have a static set method for the maxRadius....

  • C++ 1. Start with a UML diagram of the class definition for the following problem defining a utility class named Month. 2. Write a class named Month. The class should have an integer field named month...

    C++ 1. Start with a UML diagram of the class definition for the following problem defining a utility class named Month. 2. Write a class named Month. The class should have an integer field named monthNumber that holds the number of the month (January is 1, February is 2, etc.). Also provide the following functions: • A default constructor that sets the monthNumber field to 1. • A constructor that accepts the number of month as an argument and sets...

  • Lisp program count-of (symbol list): Write a function named count-of that takes two parameters, a symbol...

    Lisp program count-of (symbol list): Write a function named count-of that takes two parameters, a symbol and a list. Count the number of instances of x in the list. Do not count the number of x in any sub-lists within the list. Test: count-of 'a '(a 'a(a c) d c a). Result: 2 trim-to (symbol list): Write a function named trim-to that takes a symbol and list as parameters. Return a new list starting from the first occurrence of the...

  • Add a member function template called RemoveRandom to the ArrayBag class (so add it to ArrayBag.h...

    Add a member function template called RemoveRandom to the ArrayBag class (so add it to ArrayBag.h). The RemoveRandom member function should remove a random entry from the list (so use your srand and rand functions). Do not forget to TEST... that means you will need to add the prototype and test with a simple main. All you need to submit though is the member function implementation Add an overloaded constructor to the ArrayBag class (so add it to 2. ArrayBag.h)....

  • In JAVA 1. Create a class called Rectangle that has integer data members named - length...

    In JAVA 1. Create a class called Rectangle that has integer data members named - length and width. Your class should have a default constructor (no arg constructor) Rectangle() that initializes the two instance variables of the object Rect1. The second overloading constructor should initializes the value of the second object Rect2. The class should have a member function named GetWidth() and GetLength() that display rectangle's length and width. OUTPUT: Rectl width: 5 Rectl length: 10 Enter a width :...

  • Code the following Program in C++ Define a class named Payment that contains a member variable...

    Code the following Program in C++ Define a class named Payment that contains a member variable of type float that stores the amount of the payment and appropriate accessor and mutator methods.    Also create a member function named paymentDetails that outputs an English sentence that describes the amount of the payment. Next define a class named CashPayment that is derived from Payment. This class should redefine the paymentDetails function to indicate that the payment is in cash. Include appropriate constructor(s)....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT