In: Computer Science
Write C++ 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
#include <iostream> using namespace std; class gamma { private: int id; static int total; public: gamma(); virtual ~gamma(); static void showtotal(); void showid(); }; int gamma::total = 0; gamma::gamma() { id = ++total; } void gamma::showtotal() { cout << "Total is " << total << endl; } void gamma::showid() { cout << "ID number is " << id << endl; } gamma::~gamma() { cout << "Destroying ID number " << id << endl; } int main() { gamma g1; gamma::showtotal(); gamma g2, g3; gamma::showtotal(); g1.showid(); g2.showid(); g3.showid(); cout << "----------end of program----------\n"; return 0; }