C++ examples for Class:Constructor
Adding a Constructor to a Class
#include <cinttypes> #include <iostream> using namespace std; class Car/*from w w w . j ava 2s . c om*/ { private: int m_NumberOfWheels{}; public: Car(int numberOfWheels) : m_NumberOfWheels{ numberOfWheels } { } int GetNumberOfWheels() { return m_NumberOfWheels; } }; int main(int argc, char* argv[]) { Car myCar{ 4 }; cout << "Number of wheels: " << myCar.GetNumberOfWheels() << endl; Car myMotorcycle{ 2 }; cout << "Number of wheels: " << myMotorcycle.GetNumberOfWheels() << endl; return 0; }