Multiple constructors can often be combined with the definition of default arguments - C++ Class

C++ examples for Class:Constructor

Description

Multiple constructors can often be combined with the definition of default arguments

Demo Code

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

class Employee{//from   ww  w .  ja v  a 2  s. c om
  public:
    Employee(const char *pName = "No Name",int xfrHours = 0,double xSalary = 0.0){
        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("Mary");
    Employee s1("Tom", 80, 2.5);

    return 0;
}

Result


Related Tutorials