C++ Program to Demonstrate the use of Static Data Members in a Class

This C++ program demonstrates the use of static members in a class. A static data member is shared by all objects of a class. We can put a static data member inside a class but we would have to initialize it outside the class using scope resolution operator (::).

Here is the source code of the C++ program demonstrates the use of static members in a class. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Demonstrate a Class with Static Members
  3.  */
  4. #include <iostream>
  5.  
  6. class Car {
  7.     private:
  8.         static std::string colour;
  9.         std::string brand;
  10.         std::string state;
  11.         int model;  
  12.     public:
  13.         Car(int model, std::string brand, std::string state = "off") 
  14.         { 
  15.             this->model = model; 
  16.             this->brand = brand;
  17.             this->state = state;
  18.         }
  19.         void engineOn()
  20.         {
  21.             state = "on";   
  22.         }
  23.         void knowState()
  24.         {
  25.             std::cout << "Is " << brand << " ready?" << std::endl;
  26.         }
  27.         void isReady()
  28.         {
  29.             if(state == "on")
  30.                 std::cout << colour << " " << brand
  31.                           << " is ready to go!" << std::endl;
  32.             else
  33.                 std::cout << colour << " " << brand
  34.                           << " is not ready!" << std::endl;
  35.         }
  36. };
  37.  
  38. // Definition of a protected static member
  39. // Legal expression
  40. std::string Car::colour = "Red";
  41.  
  42. int main()
  43. {
  44.     Car chevy(1965, "Chevy Mint", "on");
  45.     chevy.knowState();
  46.     chevy.isReady();
  47.     Car ferrari(1965, "Ferrari" );
  48.     ferrari.knowState();
  49.     ferrari.isReady();
  50. }

$ a.out
Is Chevy Mint ready?
Red Chevy Mint is ready to go!
Is Ferrari ready?
Red Ferrari is not ready!

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.