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

bookmark

  1. #include <iostream>
     
    class Car {
        private:
            static std::string colour;
            std::string brand;
            std::string state;
            int model;  
        public:
            Car(int model, std::string brand, std::string state = "off") 
            { 
                this->model = model; 
                this->brand = brand;
                this->state = state;
            }
            void engineOn()
            {
                state = "on";   
            }
            void knowState()
            {
                std::cout << "Is " << brand << " ready?" << std::endl;
            }
            void isReady()
            {
                if(state == "on")
                    std::cout << colour << " " << brand
                              << " is ready to go!" << std::endl;
                else
                    std::cout << colour << " " << brand
                              << " is not ready!" << std::endl;
            }
    };
     
    // Definition of a protected static member
    // Legal expression
    std::string Car::colour = "Red";
     
    int main()
    {
        Car chevy(1965, "Chevy Mint", "on");
        chevy.knowState();
        chevy.isReady();
        Car ferrari(1965, "Ferrari" );
        ferrari.knowState();
        ferrari.isReady();
    }
    
    
    Output:
    Is Chevy Mint ready?
    Red Chevy Mint is ready to go!
    Is Ferrari ready?
    Red Ferrari is not ready!