C++ examples for Class:Constructor
Add Default Constructors to a class
#include <cinttypes> #include <iostream> using namespace std; class Car/* w w w . ja v a2 s.c om*/ { private: int m_NumberOfWheels{}; public: Car() = default; 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; Car noWheels; cout << "Number of wheels: " << noWheels.GetNumberOfWheels() << endl; return 0; }