constructors & default constructors

Hello all,
I am trying to understand a little bit more about constructors and default constructors when making a class.

If i make a class with a constructor, then the arguments must be passed when i create an instance of that class. With that same class, would it be possible to make it so that if i do not have arguments (yet) it can still be created?

If so, that would mean having a constructor and a default constructor right?
I can't seem to figure out how to make a default constructor and a constructor. I thought it looked like this:


1
2
3
4
5
6
7
8
9
10
11
12

class Employee {
public:   Employee();
private:  
float sal;
string name;
};

Employee::Employee(float s = 100000, string n = "bob") {
   sal = s;
   name = n;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Employee {
public:   
    Employee(); // Default 0 argument constructor
    Employee(float s; string n); // custom constructor

private:  
    float sal;
    string name;
};
/*  ... */
// Custom constructor body
Employee::Employee(float s, string n) {
   sal = s;
   name = n;
}
// Default constructor - needs a body, but may be empty:
Employee::Employee () {
// empty. Might as well add {} inside a class definition in line 3
}


EDIT:
If you want to use default arguments without specifying them while creating instance, you should put them in to class declaration, not implementation:
1
2
3
4
5
6
7
8
9
10
11
12
class Employee {
public:
   Employee(float s = 100000, string n = "bob"); // this becomes default constructor
private:  
    float sal;
    string name;
};
// Commented out, but left for transparency
Employee::Employee(float s /*= 100000*/, string n /*= "bob"*/) {
   sal = s;
   name = n;
}
Last edited on
Wow I cannot believe I didn't understand why my code refused to compile... I thought by giving it default values it would just make the instances of Employee with those values and let me make an array of them.

Thank you so much!!!
Hi strife,

Here is a little improvement:


1
2
3
4
5
6
7
8
class Employee {
public:   
    Employee(); // Default 0 argument constructor
    Employee(float s; string n) : // custom constructor with initialiser list
           sal(s),
           name(n){} // no need for any code here

//rest of class here 


The above looks similar, but is better. When an object of a class type is created, it's member variables are initialised by default before the code in the constructor is run. If the code initialises values by assignment, then this is a waste because it is essentially being done twice. If the constructor has an initialisation list as above, then member variables are only initialised once. Initialisation of base class constructors can be done from here as well.

Btw, I would recommend you have more meaningful names for your variables : s, n sal are not particularly good IMO. An exception to naming might be for mathematical equations, or when documentation one is working from has a particular format, or one wants to make the variable names similar to the documentation. For example: r = sqrt(x2 + y2)

Another example is if documentation has Greek letters, so names like RAlpha would be OK.

Hope all is well :+)

EDIT:

One can still use an initialiser list even though there are no arguments for the constructor, so this would set the default values you want.
Last edited on
Topic archived. No new replies allowed.