C++ Constructor Pass arguments to the members' constructors

Description

C++ Constructor Pass arguments to the members' constructors

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

class Employee{/*www  .j  a v a  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;
}
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int nextEmployeeId = 1000; // first legal Employee ID
class EmployeeId/*from ww w.j av  a  2s. com*/
{
  public:
    EmployeeId(){
        value = nextEmployeeId++;
        cout << "Take next employee id " << value << endl;
    }
    EmployeeId(int id){
        value = id;
        cout << "Assign employee id " << value << endl;
    }
  protected:
    int value;
};

class Employee
{
  public:
    Employee(const char* pName)
    {
        cout << "constructing Employee " << pName << endl;
        name = pName;
        workHour = 0;
        salary = 0.0;
    }
  protected:
    string    name;
    int       workHour;
    double    salary;
    EmployeeId id;
};

int main(int argcs, char* pArgs[])
{
    Employee s1("Jack");
    Employee s2("Tom");

    return 0;
}



PreviousNext

Related