In this code, we made a const char pointer id, as it is a char so it should store only one element address. but in default constructor we assigned, "NULL" to that id, how is that id is holding 4 char? and in parameterized constructor we pass i[] array and assign the value to id, same again how char id is holding complete array? further why we need const char?
class Employee
{
public:
string name;
const char *id;
int age;
long salary;
Employee ()
{
name = "NULL";
id = "NULL";
age =0;
salary = 0;
};
// Employee(string n, char id[], int a, long s): name(n), id(id), age(a), salary(s)
// {};
Employee (string n,const char i[], int a, long s) //const use krne k baighar warning a rhe
{
name = n;
id = i;
age = a;
salary = s;
};
void getData()
{
cout<< "Employee ID: "<< id <<endl;
cout<< "Employee Name: "<< name <<endl;
cout<< "Employee Age: "<< age <<endl;
cout<< "Employee Salary: "<< salary <<endl;
};
};
I am confused that if id is char then how it is holding a string literal of 5 character. CHAR type can store only single character, but if i am making it a pointer then it is holding many character. Please clear this point, i am very confused as how it is working.
idis a pointer, the only thing it "holds" is the address of an object - it does not "hold" the entire arraystd::stringforname, why isidachar *?iditself is not const, it points to const. You can change what it points to in the program. In fact, if it were const you could not assign to it even in the constructor; you'd have to initialize it. That it points to const, by the way, does not prevent you from assigning the address of a non-const character (array)! It just indicates that the contents of what it points to is not to be changed by the class.