When using strings in Classes..

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifndef INVENTORY_H
#define INVENTORY_H
#include <cstring>

const int DEFAUL_SIZE = 51;

class InventoryItem
{
private:
   char *desription;
   double cost;
   int units;
public:
   //Constructor #1
   InventoryItem()
   {
      description = new char [DEFAULT_SIZE];
      *description = '\0';
      cost = 0.0;
      units = 0;
   }

   //Constructor #2
   InventoryItem(char *desc)
   {
      description = new char [strlen(desc) + 1];
      strcpy(description, desc);
      cost = 0.0;
      units = 0;
   }
}; 
#endif 


I left out the rest of the program example to show you what I mean.
Last edited on
In the textbook, it uses #include <cstring> but in my class the professor uses #include <string>
<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.
Last edited on
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.

Here it is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
//#include <cstring>
#include <string>

class Employee
{
private:
	string name;
	int idNumber;
	string department;
	string position;
public:
	Employee();
	Employee(string, int);
	Employee(string, int, string, string);
	void setName(string);
	void setIdNumber(int);
	void setDepartment(string);
	void setPosition(string);
	string getName() const;
	int getIdNumber() const;
	string getDepartment() const;
	string getPosition() const;
};

Employee::Employee()
{
	name;
	idNumber = 0;
	department;
	position;
}

Employee::Employee(string n, int id)
{
	name = n;
	idNumber = id;
	department;
	position;
}

Employee::Employee(string n, int id, string depart, string pos)
{
	name = n;
	idNumber = id;
	department = depart;
	position = pos;
}

void Employee::setName(string n)
{
	name = n;
}

void Employee::setIdNumber(int id)
{
	idNumber = id;
}

void Employee::setDepartment(string depart)
{
	department = depart;
}

void Employee::setPosition(string pos)
{
	position = pos;
}
#endif 


It's not finished but can you please tell me what's wrong with it?
Almost everything in the standard library is inside the std namespace. If you write std::string it will work better.
And would that go at the top?
1
2
3
4
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string.
std::string; //Like this?? 
put this

#include <string>
using namespace std;

and use
string variable_name;
Last edited on
I did this. I would get the errors in the public member functions that uses string.
This is my class in full..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
//#include <cstring>
#include <string>
std::string;

class Employee
{
private:
	string name;
	int idNumber;
	string department;
	string position;
public:
	Employee();
	Employee(string, int);
	Employee(string, int, string, string);
	void setName(string);
	void setIdNumber(int);
	void setDepartment(string);
	void setPosition(string);
	string getName() const;
	int getIdNumber() const;
	string getDepartment() const;
	string getPosition() const;
};

Employee::Employee()
{
	name;
	idNumber = 0;
	department;
	position;
}

Employee::Employee(string n, int id)
{
	name = n;
	idNumber = id;
	department;
	position;
}

Employee::Employee(string n, int id, string depart, string pos)
{
	name = n;
	idNumber = id;
	department = depart;
	position = pos;
}

void Employee::setName(string n)
{
	name = n;
}

void Employee::setIdNumber(int id)
{
	idNumber = id;
}

void Employee::setDepartment(string depart)
{
	department = depart;
}

void Employee::setPosition(string pos)
{
	position = pos;
}

string Employee::getName() const
{
	return name;
}

int Employee::getIdNumber() const
{
	return idNumber;
}

string Employee::getDepartment() const
{
	return department;
}

string Employee::getPosition() const
{
	return position;
}
#endif 
And this is the program i wrote, implementing it..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
#include "Employee.h"
using namespace 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
Last edited on
in second file you put using namespace std; , but not in first. In first is std::string;
change it to using namespace std;

And first file are nor .h file. In .h file you put only class, but not it's methods.

And in

1
2
3
4
5
6
7
Employee::Employee()
{
	name;
	idNumber = 0;
	department;
	position;
}


need to give value to name, department and position.
Last edited on
Employee.h file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using namespace std;

class Employee
{
private:
	string name;
	int idNumber;
	string department;
	string position;
	
public:
	Employee();
	Employee(string, int);
	Employee(string, int, string, string);
	void setName(string);
	void setIdNumber(int);
	void setDepartment(string);
	void setPosition(string);
	string getName() const;
	int getIdNumber() const;
	string getDepartment() const;
	string getPosition() const;
};
#endif  


Employee.cc file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <string>
#include "Employee.h"
using namespace std;

Employee::Employee()
{
	name = "";
	idNumber = 0;
	department = "";
	position = "";
}

Employee::Employee(string n, int id)
{
	name = n;
	idNumber = id;
	department = "";
	position = "";
}

Employee::Employee(string n, int id, string depart, string pos)
{
	name = n;
	idNumber = id;
	department = depart;
	position = pos;
}

void Employee::setName(string n)
{
	name = n;
}

void Employee::setIdNumber(int id)
{
	idNumber = id;
}

void Employee::setDepartment(string depart)
{
	department = depart;
}

void Employee::setPosition(string pos)
{
	position = pos;
}

string Employee::getName() const
{
	return name;
}

int Employee::getIdNumber() const
{
	return idNumber;
}

string Employee::getDepartment() const
{
	return department;
}

string Employee::getPosition() const
{
	return position;
}


And main file is good
the compiler is saying somethign about the 'string' in the class or something
If the compiler is complaining, the least you can do is read the message.
If you don't understand it, then post it full.
Topic archived. No new replies allowed.