Question

Q.Write a program that shows a class called gamma that keeps track of how many objects...

Q.Write a program that shows a class called gamma that keeps track of how many objects of itself there are. Each gamma object has its own identification called ID. The ID number is set equal to total of current gamma objects when an object is created.

Test you class by using the main function below.


int main()

   {

   gamma g1;

   gamma::showtotal();

   gamma g2, g3;

   gamma::showtotal();

   g1.showid();

   g2.showid();

   g3.showid();

   cout << "----------end of program----------\n";

   return 0;

   }

Runtime output

Total is 1

Total is 3

ID number is 1

ID number is 2

ID number is 3

----------end of program----------

Destroying ID number 3

Destroying ID number 2

Destroying ID number 1

C++ please

Homework Answers

Answer #1
#include <iostream> 
using namespace std; 

//Idea is to use static variable to keep track
//of number of classes
class gamma 
{ 
    // This is to store ID
    int ID;
    
    //This is to keep track of total number
    //objects of the gamma class
    static int objCount; 
  
public:
    //Constructor
    gamma()  
    { 
        //Pre increment objCount and store in ID
        ID = ++objCount;  
    } 
    //Destructor
    ~gamma() 
    { 
        cout<<"Destroying ID number"<<ID<<"\n";
        //Pre decrementing objCount as one object is deleted
        --objCount; 
    } 
    void showid() 
    { 
        cout << "ID number is:" << ID << "\n"; 
    } 
    static void showtotal(void) 
    { 
        cout << "Total is:" << objCount<< "\n"; 
    } 
}; 

//Initializing the stact variable.
//Static variables are set to 0(by default).
int gamma::objCount;

int main()
{

   gamma g1;

   gamma::showtotal();

   gamma g2, g3;

   gamma::showtotal();

   g1.showid();

   g2.showid();

   g3.showid();

   cout << "----------end of program----------\n";

   return 0;

}

Output Screenshot:

(Note: Please refer to the screenshots of the code to understand the indentation of the code)

Code Screenshots:

gamma class:

main():

Know the answer?
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
Similar Questions
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
Let’s say we’re designing an Employee class that has two data fields. One data field, called...
Let’s say we’re designing an Employee class that has two data fields. One data field, called id, is for storing each employee’s ID number. Another data field, called numEmployees, keeps track of how many Employee objects have been created. For each of the blanks, indicate the appropriate choice. id should be ( public/private ) and ( static/not static ) numEmployees should be (public/private) and (static/not static )
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {...
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {     int value; public:     Test(int v); };    Test::Test(int v) {     value = v; }    int main() {     Test t[100];     return 0; } _______________ #include <iostream> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { for(j=1; j<=i; j++ ) { cout<<"*"; } cout << "\n";   } return 0; }
In Java Let’s say we’re designing a Student class that has two data fields. One data...
In Java Let’s say we’re designing a Student class that has two data fields. One data field, called id, is for storing each student’s ID number. Another data field, called numStudents, keeps track of how many Student objects have been created. For each of the blanks, indicate the appropriate choice. id should be   public/private   and   static/not static . numStudents should be   public/private   and   static/not static . The next three questions use the following class: class Counter {     private int...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
How to stop the program from exiting after display detail. When there is food detail, it...
How to stop the program from exiting after display detail. When there is food detail, it will display and exit the program. What can i do to make it not exit the program and back to main menu. #include <iostream> #include <iomanip> #include<string.h> using namespace std; struct food{ int order_id; string food_code,flavor,customer_id; string address,name; int weight,unit_price,qty,contact_number; struct food *next; };    class Foodsystem{ food *head,*temp,*temp2,*end; static int id;    public: Foodsystem(){ head=NULL;end=NULL;} void Place_Order(); void View_food_details(); void Modify_food_details(); void Delete_food_details();...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). Details and Requirements Your class must allow for storage of rational numbers in a mixed number format. Remember that a mixed number consists of an integer part and a fraction part (like 3 1/2 – “three and one-half”). The Mixed class must allow for both positive and negative mixed number values. A...
Program C++ (use visual studio) Q1. What default copy constructor does the compiler insert in the...
Program C++ (use visual studio) Q1. What default copy constructor does the compiler insert in the following class? class Student { string name; string id; double grade; }; =========================== Q2 .What is the factor transfer method used when the f() function is called from? void f(int n[]); int main() { int m[3]= {1, 2, 3}; f(m); } ================================== Q3. Write a program that produces a bigger() with a prototype as shown below and outputs a large value by inputting two...
we defined, implemented, and tested a class Time. In this lab, we will continue working with...
we defined, implemented, and tested a class Time. In this lab, we will continue working with the Time class. A. Separate the class definition and class implementation from the client program. Move the class definition into a header file named Time.h and move the class implementation into a cpp file named Time.cpp. Use preprocessor directive to include header files as needed. B. Modify the Time class as follows: 1. Replace the default constructor and overloaded constructor with a constructor with...
USING C++ Topics: Friend functions Copy constructor ----------------------------------------------------------------------------------------------------------------------------------------- Lab 3.1 Create your objects in the stack...
USING C++ Topics: Friend functions Copy constructor ----------------------------------------------------------------------------------------------------------------------------------------- Lab 3.1 Create your objects in the stack (not on the heap). Add a friend function, kilotopound, which will convert kilograms to pounds. Change your weight mutator to ask whether weight is input in kilograms or pounds. If it is kilograms, call the friend function kilotopound to convert it to pounds and return pounds. There are 2.2 pounds in one kilogram. Create an object on the stack with the following information:     ...