Register the entrance of people in the auditorium using static class data using oops in c++
Question
Register the entrance of people in the auditorium using static class data using OOP in C++
Solution
Sure, here is a simple example of how you can use a static class data member to register the entrance of people in an auditorium using OOP in C++.
#include <iostream>
using namespace std;
class Auditorium {
static int count; // static data member
public:
Auditorium() { // constructor
count++;
}
static int getCount() { // static member function
return count;
}
};
int Auditorium::count = 0; // initialize static data member
int main() {
cout << "Initial count: " << Auditorium::getCount() << endl;
Auditorium person1;
Auditorium person2;
Auditorium person3;
cout << "After 3 people enter, count: " << Auditorium::getCount() << endl;
return 0;
}
In this code:
-
We declare a static data member
countin theAuditoriumclass. This member will be shared by all objects of the class, so it can be used to count the total number of people (objects) created. -
We define a constructor that increments
counteach time a new object is created. -
We define a static member function
getCount()that returns the current value ofcount. -
In the
main()function, we create threeAuditoriumobjects (representing three people entering the auditorium), and then we print the value ofcount(which should be 3).
Remember that static data members need to be initialized outside the class, as shown in the line int Auditorium::count = 0;.
Similar Questions
Register the entrance of people in the auditorium using static class data using oops in c++
Discuss what are static data members in class and how these are defined? How static functions are accessed?
Write a programme in C++ which read and print employee information with department and pf information using hierarchical inheritance
C++ programs consist of pieces called classes and ___________Question 3Answera.functionsb.objectsc.basesd.subjectse.spaces
BasicsWhat is storage class for variable A in below code?void main() { int A; A = 10; printf("%d", A);}Optionsautoexternregisterstatic
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.