I have a question concerning using strings in classes. First, what's the difference between #include <string> and #include <cstring> . Second, when using string in a class, is it necessary to have a pointer for whatever reason? (I'm not entirely, if not at all, sure why they are being used)
In the textbook I have, they have something like this:
<cstring> contains functions related to c strings. std::strcpy, std::strlen, std::strcat etc.
<string> contains the class std::string and some other functions related to std::string.
when using string in a class, is it necessary to have a pointer for whatever reason?
This depends on what kind of string you are using. A c string is a null terminated array of characters. Often a pointer is used to point to the first character in the c array. You could use a fixed length array instead of a pointer but that could waste space and be problematic if the array is too short to store a string.
When using std::string there is often no need to use a pointer. Using std::string is generally easier to use than c strings.
Oh ok, I have this program called Microsoft Visual C++ and I was using the std::string in a class I was writing, but I have been getting an error as I typed the class out.
#include <iostream>
#include <string>
#include "Employee.h"
usingnamespace std;
int main()
{
Employee employee1;
Employee employee2;
Employee employee3;
string emp1, emp2, emp3;
int id1, id2, id3;
string depart1, depart2, depart3;
string pos1, pos2, pos3;
cout << " What are the three employee's names?" << endl;
cin >> emp1 >> emp2 >> emp3;
employee1.setName(emp1);
employee2.setName(emp2);
employee3.setName(emp3);
cout << "\n What are the three employee's ID Numbers?" << endl;
cin >> id1 >> id2 >> id3;
employee1.setIdNumber(id1);
employee2.setIdNumber(id2);
employee3.setIdNumber(id3);
cout << "\n What department do the three employees work in/ "<< endl;
cin >> depart1 >> depart2 >> depart3;
employee1.setDepartment(depart1);
employee2.setDepartment(depart2);
employee3.setDepartment(depart3);
cout << "\n What are the three employees' positions? " << endl;
cin >> pos1 >> pos2 >> pos3;
employee1.setPosition(pos1);
employee2.setPosition(pos2);
employee3.setPosition(pos3);
cout << "\n Here is the data that you have entered..." << endl;
cout << "------------------------------------------------------------" << endl;
cout << "NAME ID NUMBER DEPARTMENT POSITION " << endl;
cout << "------------------------------------------------------------" << endl;
cout << employee1.getName() << " " << employee1.getIdNumber()
<< employee1.getDepartment() << " " << employee1.getPosition() << endl;
cout << employee2.getName() << " " << employee2.getIdNumber()
<< employee2.getDepartment() << " " << employee2.getPosition() << endl;
cout << employee3.getName() << " " << employee3.getIdNumber()
<< employee3.getDepartment() << " " << employee3.getPosition() << endl;
cout << "------------------------------------------------------------" << endl;
return 0;
}
Apologies for the double post, program too long to have in one whole reply box. But yeah, I don't know what's wrong with the program, the compiler is saying somethign about the 'string' in the class or something