0

Why this code showing uninitialized/garbage value? It's showing correct value when the field name and constructor parameters not identical.

#include<bits/stdc++.h>
using namespace std;
class Employee {
public:
     Employee(string name,string company,int age)
     {
         name = name;
         company = company;
         age = age;
     }
     string name;
     string company;
     int age;
     void IntroduceYourself()
     {
         cout<<name<<' '<<company<<' '<<age<<endl;
     }

};
int main()
{

    Employee employee1= Employee("Saldina","Codebeauty",25);

    employee1.IntroduceYourself();

    Employee employee2 = Employee("John","Amazon",35);

    employee2.IntroduceYourself();

    return 0;
}
Roman
  • 23
  • 7
  • What do you think `name = name;` is doing? How should a compiler make a distinction between data member name and function parameter name? – Evg Jul 12 '21 at 05:22
  • It is ambiguous as to which variable is being set, class variable or local variable. If you want to use same field names, use this keyword in the constructor e.g. this->name = name; – Ajit Vaze Jul 12 '21 at 05:22
  • [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Evg Jul 12 '21 at 05:32
  • What does this keyword do actually? Does it refer to the class variable ?@AjitVaze – Roman Jul 12 '21 at 05:33
  • @Roman It refers to the object you're working with. – Evg Jul 12 '21 at 05:37

1 Answers1

2

If we declare a name in an inner scope, that name hides uses of that name declared in an outer scope.

The name which is a function parameter hides the name which is a data member. How would you tell which of the name is meant to be on which side in here:

name = name
// data_member = parameter ?
// parameter = data_member ?
// parameter = parameter !
// data_member = data_member ?

Thus, your output, the IntroduceYourself, prints two empty strings and a default initialized int, since in the body of the constructor you reassign values to the parameters, local variables, while the data members are default initialized.

There are two common approaches:

// Not the best practice, but can take place 
 Employee(string name,string company,int age) { 
     this->name = name;
     this->company = company;
     this->age = age;
 }

And:

Employee(string name, string company, int age) 
    : name(name), company(company), age(age) {} // Compiler will sort it out
// Data members
string name;
string company;
int age;

Note, that you should use the member initializer list, as in the second example. Otherwise, you default initialize data members and then reassign new values to them, when you "initialize" them in the body of a constructor.

rawrex
  • 4,044
  • 2
  • 8
  • 24
  • thanx for your answer buddy. – Roman Jul 12 '21 at 05:31
  • Can I use same name in the 2nd approach ? – Roman Jul 12 '21 at 05:48
  • https://stackoverflow.com/questions/34959703/what-happens-when-constructors-parameter-has-same-name-as-member-variable But here seems to be same name . – Roman Jul 12 '21 at 06:08
  • Don;t you use memeber initializer list in the 2nd approach? – Roman Jul 12 '21 at 06:11
  • @Roman Yeah, That's true! Check [this](https://stackoverflow.com/questions/6185020/initializing-member-variables-using-the-same-name-for-constructor-arguments-as-f/6185043#6185043). – rawrex Jul 12 '21 at 07:28