I have tried the following program:
#include<iostream.h>
#include<fstream.h>
#include<string.h>
class Member
{
private:
int Age;
int l,L;
char Name[40];
public:
void SetName(char* name)
{
l= strlen(name);
cout<<"l="<<l<<endl;
for(int i=0;i<l;i++)
{
Name[i] = name[i];
cout<<"Name["<<i<<"] = "<<Name[i]<<endl;
}
cout<<Name;
for(int j=l;j<sizeof(Name);j++)
{
Name[j] = ' ';
}
}
void DisplayName()
{
L = strlen(Name);
cout<<"The length of the Name is :"<<L<<endl;
for(int y=0;y<l;y++)
{
cout<<Name[y];
}
}
void SetAge(int age)
{
Age = age;
}
void DisplayAge()
{
cout<<"Age = "<<Age<<endl;
}
};
void main()
{
char s[40];
Member Kokila;
cout<<"Please enter a name"<<endl;
cin.getline(s,40);
Kokila.SetAge(14);
Kokila.DisplayAge();
Kokila.SetName(s);
Kokila.DisplayName();
}
I have entered Tom as input. In SetName() method, the value of l =3. In DisplayName() method, the value of L = 43( ie sum of the length of Name[40] from the class member and the length of the input string). I cannot understand why .
Anybody ..can explain me...
Thanks,
Kokila.