Pass along arguments to the members' constructors - C++ Class

C++ examples for Class:Constructor

Description

Pass along arguments to the members' constructors

Demo Code

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

class Employee{//from   ww  w . j a  va 2  s.c  om
  public:
    Employee(const char* pName){
        cout << "constructing Employee " << pName << endl;
        name = pName;
        workHour = 0;
        salary = 0.0;
    }
  protected:
    string  name;
    int     workHour;
    double  salary;
};

int main(int argcs, char* pArgs[])
{
    Employee s1("Mary");
    Employee* pS2 = new Employee("Edith");

    delete pS2;

    return 0;
}

Result


Related Tutorials