classes and class members

#include
class Employee
{
public:
char m_strName[25];
int m_nID;
double m_dWage;

// Set the employee information
void SetInfo(char *strName, int nID, double dWage)
{
strncpy(m_strName, strName, 25);
m_nID = nID;
m_dWage = dWage;
}

// Print employee information to the screen
void Print()
{
using namespace std;
cout << "Name: " << m_strName << " Id: " <<
m_nID << " Wage: $" << m_dWage << endl;
}
};

int main()
{
// Declare two employees
Employee cAlex;
cAlex.SetInfo("Alex", 1, 25.00);

Employee cJoe;
cJoe.SetInfo("Joe", 2, 22.25);

// Print out the employee information
cAlex.Print();
cJoe.Print();

return 0;
}

can someone tell me why have we used a pointer *strname as a parameter in setinfo function.what if we do not use a pointer?
Is this a homework question?

I'll note that your thread title is misleading. The question has nothing to do with classes or class members, and instead has to do with arrays, pointer use, and pass-by-reference vs pass-by-value.
sorry for the mistake.
can u please answer my question?
Can you answer mine?
actually i read it somewhere on the internet as an example...and was unable to understand the use of a pointer here and so i asked.
As you can see from the calls to SetInfo() in main(), strName isn't a single char, it's a C-style string, i.e. an array of chars. Passing a pointer is one way to pass an array into a function.
k, u mean to say we declared an m_strname array as a member variable, copied that array to a pointer named strname and then passed that pointer as a parameter in setinfo() and alex as the argument in main(). right or wrong?
No. Nothing copies m_strname to strname. strname is set by the call to SetInfo() in main():

cAlex.SetInfo("Alex", 1, 25.00);

Inside SetInfo(), the data held in the memory pointed to by strname , is copied into the memory pointed to by m_strName:

strncpy(m_strName, strName, 25);

By the way, when posting code, please use code tags as I've done. It makes the code more readable.
Last edited on
k now i got it. i confused that

 
strncopy(destination array , source array , size of destination)


follows this and my choice of words were bad.
but thanx this thing was bothering me a lot.
You're welcome - glad I could help :)
Topic archived. No new replies allowed.