Knowee
Questions
Features
Study Tools

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++

🧐 Not the exact question you are looking for?Go ask a question

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:

  1. We declare a static data member count in the Auditorium class. 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.

  2. We define a constructor that increments count each time a new object is created.

  3. We define a static member function getCount() that returns the current value of count.

  4. In the main() function, we create three Auditorium objects (representing three people entering the auditorium), and then we print the value of count (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;.

This problem has been solved

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

1/1

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.