C++ examples for Class:Constructor
Provide the class multiple ways to create objects by overloading the constructor
#include <cstdio> #include <cstdlib> #include <iostream> #include <string.h> using namespace std; class Employee/*from w w w . j a v a 2s . c o m*/ { public: Employee() { cout << "constructing employee No Name" << endl; name = "No Name"; workHour = 0; salary = 0.0; } Employee(const char *pName) { cout << "constructing employee " << pName << endl; name = pName; workHour = 0; salary = 0; } Employee(const char *pName, int xfrHours,double xSalary) { cout << "constructing employee " << pName << endl; name = pName; workHour = xfrHours; salary = xSalary; } protected: string name; int workHour; double salary; }; int main(int argcs, char* pArgs[]) { Employee noName; Employee s2("abc"); Employee s1("def", 80, 2.5); return 0; }