# Accessing static members using non-static member functions in C++

1: //Listing 14.3 private static data members

2:

3: #include <iostream.h>

4:

5: class Cat

6: {

7: public:

8: Cat(int age):itsAge(age){HowManyCats++; }

9: virtual ~Cat() { HowManyCats--; }

10: virtual int GetAge() { return itsAge; }

11: virtual void SetAge(int age) { itsAge = age; }

12: virtual int GetHowMany() { return HowManyCats; }

13:

14:

15: private:

16: int itsAge;

17: static int HowManyCats;

18: };

19:

20: int Cat::HowManyCats = 0;

21:

22: int main()

23: {

24: const int MaxCats = 5; int i;

25: Cat *CatHouse[MaxCats];

26: for (i = 0; i<MaxCats; i++)

27: CatHouse[i] = new Cat(i);

28:

29: for (i = 0; i<MaxCats; i++)

30: {

31: cout << "There are ";

32: cout << CatHouse[i]->GetHowMany();

33: cout << " cats left!\n";

34: cout << "Deleting the one which is ";

35: cout << CatHouse[i]->GetAge()+2;

36: cout << " years old\n";

37: delete CatHouse[i];

38: CatHouse[i] = 0;

39: }

40: return 0;

Output: There are 5 cats left!

Deleting the one which is 2 years old

There are 4 cats left!

Deleting the one which is 3 years old

There are 3 cats left!

Deleting the one which is 4 years old

There are 2 cats left!

Deleting the one which is 5 years old

There are 1 cats left!

Deleting the one which is 6 years old

2:

3: #include <iostream.h>

4:

5: class Cat

6: {

7: public:

8: Cat(int age):itsAge(age){HowManyCats++; }

9: virtual ~Cat() { HowManyCats--; }

10: virtual int GetAge() { return itsAge; }

11: virtual void SetAge(int age) { itsAge = age; }

12: virtual int GetHowMany() { return HowManyCats; }

13:

14:

15: private:

16: int itsAge;

17: static int HowManyCats;

18: };

19:

20: int Cat::HowManyCats = 0;

21:

22: int main()

23: {

24: const int MaxCats = 5; int i;

25: Cat *CatHouse[MaxCats];

26: for (i = 0; i<MaxCats; i++)

27: CatHouse[i] = new Cat(i);

28:

29: for (i = 0; i<MaxCats; i++)

30: {

31: cout << "There are ";

32: cout << CatHouse[i]->GetHowMany();

33: cout << " cats left!\n";

34: cout << "Deleting the one which is ";

35: cout << CatHouse[i]->GetAge()+2;

36: cout << " years old\n";

37: delete CatHouse[i];

38: CatHouse[i] = 0;

39: }

40: return 0;

`41: }`Output: There are 5 cats left!

Deleting the one which is 2 years old

There are 4 cats left!

Deleting the one which is 3 years old

There are 3 cats left!

Deleting the one which is 4 years old

There are 2 cats left!

Deleting the one which is 5 years old

There are 1 cats left!

Deleting the one which is 6 years old

**Analysis:**

**On line 17, the static member variable**

`HowManyCats`is declared to have private access. Now you cannot access this variable from non-member functions, such as

`TelepathicFunction`from the previous listing.

Even though

`HowManyCats`is static, it is still within the scope of the class. Any class function, such as

`GetHowMany()`, can access it, just as member functions can access any member data. However, for a function to call

`GetHowMany()`, it must have an object on which to call the function.

DOuse static member variables to share data among all instances of a class.DOmake static member variables protected or private if you wish to restrict access to them.DON'Tuse static member variables to store data for one object. Static member data is shared among all objects of its class.

Accessing static members using non-static member functions in C++
Reviewed by 1000sourcecodes
on
02:03
Rating: