Using String class to output a word

We learned about object oriented programming. The assignment was to make two classes and have it output specific data. The first class -- Box -- works fine. I'm having trouble with the second class.

The String class needs to get a message from the main, store it in the method Set and then output it with the method Print. Thinking it would be simple like with the Box class, I coded it similarly, but it's not working. I think I screwed up somewhere trying to make the Set method store a word. I'm very bad with strings if that isn't obvious from my code.

Here is the code. I commented out the code that works.

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include<iostream>
using namespace std;
#include<fstream>



/*class Box
{
private:
	int height;
	int width;
	int x;
	int y;
public:
	Box(int, int, int, int);
	Box();
	void Initialize();
	void Move();
	void Resize();
	void Set1(int init_height, int init_width);
	void Set2(int init_x, int init_y);
	void GetPosition();
	void GetDimensions();
};*/

class String
{
private:
	char data;
	int size;
public:
	String(char);
	String();
	void Set(char str1);
	void Print();
};

/*Box::Box(int init_height, int init_width, int init_x, int init_y)
{
	Set1( init_height, init_width);
	Set2( init_x, init_y);
	
}



Box::Box()
{
	Set1(0,0);
	Set2(0,0);

}
void Box::Initialize()
{
	cout << "The height is " << height<< endl;
	cout << "The width is " << width << endl;
	cout << "x is " << x << endl;
	cout << "y is " << y << endl;


}


void Box::Move()
{
	int new_x, new_y;
	cout << "Enter x: ";
	cin >> new_x;
	cout << "Enter y: ";
	cin >> new_y;
	Set2(new_x, new_y);
}

void Box::Resize()
{
	int new_height, new_width;
	cout << "Enter height: ";
	cin >> new_height;
	cout << "Enter width: ";
	cin >> new_width;
	Set1(new_height, new_width);
}

void Box::Set1(int init_height, int init_width)
{
	if( init_height < 0)
		height= 0;
	else
		height= init_height;
	if ( init_width < 0)
		width= 0;
	else
		width= init_width;
}

void Box::Set2(int init_x, int init_y)
{
	if( init_x < 0)
		x= 0;
	else
		x= init_x;
	if ( init_y < 0)
		y= 0;
	else
		y= init_y;
}

void Box::GetPosition()
{
	cout << "The new x is " << x << endl;
	cout << "The new y is " << y << endl;
}

void Box::GetDimensions()
{
	cout << "The new height is " << height << endl;
	cout << "The new width is " << width << endl;

}*/

String::String(char str1)
{
	Set(str1);
}

String::String()
{
	Set(256);
}

void String::Set(char str1)
{
	data= str1;
}

void String::Print();
{
	cout << data << endl;
}



void main()
{	
	/*Box InitBox(100, 200, 50, 75);
	InitBox.Initialize();
	Box NewBox;
	NewBox.Move();
	NewBox.Resize();
	NewBox.GetPosition();
	NewBox.GetDimensions();*/

	String NewString("Hello");
	NewString.Print();
}



Can anyone lead me in the right direction?
chars are individual one-byte characters, not entire strings. I think you meant to use std::string in place of all the chars (I hope they let you use that). :)

-Albatross
Oh thanks! Yes, we're allowed to use them.

So I changed those and this is what I have now.
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

class String
{
private:
	string data;
	int size;
public:
	String(std::string);
	String();
	void Set(std::string str1);
	void Print();
	
};

String::String(std::string str1)
{
	Set(str1);
}

String::String()
{
	Set(str1); 
}

void String::Set(std::string str1)
{
	data= str1;
}

void String::Print()
{
	cout << data << endl;
}



void main()
{	
	
	String NewString("Hello");
	NewString.Print();
}


I'm aware that I'm not sending the string correctly, but I don't know how to do it. A frustrating case of knowing what I want it to do, but not how to tell it to do it.

Also, I'm getting an error while trying to compile:
4 error C2761: 'void String::Print(void)' : member function redeclaration not allowed

Not sure what that means. Is this because of the strings or did I make a mistake somewhere else?


Figured it out. And now it compiles fine. :)
Last edited on
I have to ask, if you can use std::string, why even make this class? It seems to only wrap an std::string object and contain a useless member while restricting the uses of string methods.
Topic archived. No new replies allowed.