- #1
yungman
- 5,718
- 241
I don't know why I need to use pointer in one case and not the other:
Why when using class, and pass parameter, I have to use pointers, but shown in line21 to 24, I don't need to use pointer?
Also, why I have to use const in line 11 and 12?
thanks
C++:
#include <iostream>
#include <cstring>
using namespace std;
const int NAME_SIZE = 51;
class Student
{protected:
char name[NAME_SIZE];
public:
Student(){ name[0] = '\0';}
//Why I need pointer here? Why I need const?
Student(const char* n){set(n); }
void set(const char* n)
{ strncpy_s(name, NAME_SIZE, n, NAME_SIZE);
name[NAME_SIZE - 1] = '\0';
cout << " Name is: " << name << "\n\n";
}
};
int main()
{ Student student("Jennifer Haynes");
//Why I don't need to use pointer below?
char Ar1[NAME_SIZE] = "John", Ar2[NAME_SIZE] = "Paul";
cout << Ar1 << ", " << Ar2 << "\n\n";
strncpy_s(Ar1, NAME_SIZE, Ar2, NAME_SIZE);
cout << Ar1 << ", " << Ar2 << "\n\n";
return 0;
}
Also, why I have to use const in line 11 and 12?
thanks