0

This is a simple program

#include <iostream>
using namespace std;

class Employee
{
    public:

    Employee(string="default", int=10){};
    void display();

    private:
    static int x;
    static string s;
};
int Employee::x=7;
string Employee::s="Johnson";
void Employee::display()
{
    cout << s << x << endl;
}


int main()
{
    int num;
    string name;
    Employee e1;
    Employee e2("Arthur",33);
    e2.Employee::display();

}

I have the following questions

1) I need help understanding why the output of the program is Johnson7 and not Arthur33( i know it has something to do with static variables )

2) Normally a constructor is defined as such Employee(string,int)

What does the parameters string="default",int=10 in the constructor

Employee(string="default", int=10){};

actually mean ???

3 Answers 3

2

1) I need help understanding why the output of the program is Johnson7 and not Arthur33( i know it has something to do with static variables )

Because display() prints the static data members. The constructor does not affect that at all. In fact, it does nothing with its arguments.

2) Normally a constructor is defined as such Employee(string,int). What does the parameters string="default",int=10 in the constructor

Those are default parameters. It means that if you don't provide some or all of the arguments, the default values get taken. For example:

void foo(int i = 42, double d = 3.1416);

foo(1, 2.3); // calls with i = 1, d = 2.3
foo(1); // calls with i = 1, d = 3.1416
foo(); // calls with i = 42, d = 3.1416

This has no effect in your example because your constructor doesn't do anything with the arguments anyway.

This stuff is explained in the most basic C++ books. Have a look at the definitive list.

2
cout << s << x << endl;

prints s followed by x which are set to Johnsen and 7 respectively.

Your call to Employee e2("Arthur",33); actually does nothing, the constructor ignores the arguments you supplly to them. x and s are not overwritten.

Static varables mean that they are not bound to a specific instance of a class. So for both e1 and e2, x and s refer to the same variable and the same piece of memory.

If you want x and s to be instance members (so they can be different for e1 and e2), you should remove the static keyword and implement the constructor:

   Employee(string s="default", int x =10):x(x), s(s) {} 
   void display();

private:
    int x;
    string s;

Remove the definition of x and s:

int Employee::x=7;
string Employee::s="Johnson";

"default" and 10 are default values for the constructor arguments, so if you instantiate an employee like:

Employee e3;

s will be "default" and x=10

0

It may help understanding the code if you realise that this is not real code that does anything useful. Normally you would expect that an "Employee" object has maybe a "name" and an "age" and that the next "Employee" object also has a "name" and an "age" but most likely a different one. And you would expect that there is a constructor taking a "name" and "age" parameter, and storing these in an "Employee" object. That's how any sane person would write the code.

This code however doesn't look like it's written by a sane person. There are two "static" class variables x and s. Here, "static" means these variables don't exist once for each employee object, but only once per class. The "display" method doesn't display data about that particular Employee, but it displays the two static variables. That means, no matter what Employee object you are using, calling "display" will always display the same values.

There's a constructor with default parameters, but since there are no variables that are there per object, there is nothing useful to do with these parameters, so it just ignores them. So what you pass as parameters in the constructor, or whether you pass anything, doesn't make any difference at all. As I said, the code doesn't look like it's written by a sane person.

(Jasper's code is how a sane person would write it, and as a result it behaves in a useful way as you would expect).

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.