0

I'm trying to create a class for employees, and have a problem with its constructor.

My class looks like that (please note the name parameter which is char* type):

class Employee {
    int id;
    char * name ;
    float salary;
    int hours;
    int extra;

public:
    //constructor
    Employee(int, char *, float, int, int);

    //Getters and Setters:
    void setId(int a) { id = a; }
    int getId() { return id; }
    void setName(char * c) { name = c; }
    char * getName() { return name; }
    void setSalary(float f) { salary = f; }
    float getSalary() { return salary; }
    void setHours(int h) { hours = h; }
    int getHours() { return hours; }
    void setExtra(int e) { extra = e; }
    int getExtra() { return extra; }
};

I built a constructor and I want it to have default parameters, and I don't know how to deal with the name parameter to have a default of let's say "unknown".

This is the constructor:

Employee::Employee(int i = 123456789, char * na, float sal = 30, int ho = 45, int ex = 10)
{
    id = i;
    name = na;
    salary = sal;
    hours = ho;
    extra = ex;
}
6
  • 5
    This is not how you write C++. Where are you learning this from? Commented Sep 7, 2019 at 19:24
  • 1
    Use member initialization lists. Employee::Employee(): id(i), name(na), etc. Also why have defaults when you should be having a default constructor Employee::Employee() that will set those values for you. Commented Sep 7, 2019 at 19:25
  • also use std::string Commented Sep 7, 2019 at 19:36
  • 1
    You can't have a non-defaulted parameter after a defaulted parameter. And you need to specify the default values in the declaration, not in the implementation Commented Sep 7, 2019 at 19:45
  • @OmidCompSCI If the constructor has the default parameter for na as OP wants, then the constructor is a default constructor. Commented Sep 7, 2019 at 19:50

2 Answers 2

2

You can use a character array, and initialise the array to point to the first element of the array. You can store the array for example as a static member:

// in the class definition
inline static char default_name[] = "unknown";

// in argument list of the constructor
... char * na = default_name, ...

Although, you may want to consider whether it makes sense for name to be pointer to non-const. Perhaps a pointer to const would suffice. In such case, you could initialise it to point to the literal "unknown" directly.

Sign up to request clarification or add additional context in comments.

3 Comments

Or, once you've fixed the almost-certainly missing const or switched to std::string, just literally write it inline...
Can you elaborate more, please? Is the idea to have a "blank" pointer attribute (with the name "default_name") to the literal "unknown"? also, why const is sufficing if I don't know the length of the name?
@e.ad The idea is to have an array that contains the default name, and by default set the pointer to point to (first element of) that array. A pointer does not need to know the length of an array in order to point at an element of that array.
0

A cleaner version

class Employee {
    int id = 0;
    char *name = nullptr;
    float salary = 0.0;
    int hours = 0;
    int extra = 0;

And you don't need to have constructors, this depends on the case, but you get the idea that by initializing the variables on the definition you reduce the inconsistency of having multiples constructors for example

2 Comments

And Employee() = default should be added
got it. can you please give me an example of a case that that building and default constructor will not suffice?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.