Homemade String Class

#include<iostream>
using namespace std;

class String1 {
private:
char str[80];
public:
// Constructors
String1();
String1(char *);

// Fill a character buffer argument
void GetString(char *);

// Concatenation Operators
String1 operator+(String1 &);
String1 operator+(char *);
friend String1 operator+(char *, String1 &);

// Assignment Operators

// would work without this cuz we do not have
// any pointer members but do anyway for practice
String1 operator=( String1 &);
String1 operator=( char *);

// Console Stream Output
friend ostream &operator<<(ostream &, const String1 &);
};


ostream & operator<<(ostream & stream, sIn &)
{
stream << sIn.str;
return stream;
}

String1 String1::operator+(String1 & sIn)
{
String1 temp(this->str);
strcat(temp.str, sIn.str);
return temp;
}
String1::operator =(String1 & sIn)
{
str = new char[strlen(sIn.str) + 1];
strcpy(str, sIn.str);
String1 temp(this->str);
return temp;// string with no name
}
void String1::GetString(char * strBufPtr)
{
strcpy(strBufPtr, str);
}
String1::String1()
{
str[0] = 0;
}
String1::String1(char * strIn)
{
strcpy(str, strIn);
}

void main()
{
String1 S("Bob");
char buffer[20];
S.GetString(buffer);
cout << buffer;

//String1 S1, S2("Bob"), S3("Jones");
//S1 = S2;
//S1 = S2 + S3("Bob"), S3("Jones");
// String1 S1, S2("Bob");
//cout << S1<< S1;
}
Last edited on
// would work without this cuz we do not have
// any pointer members but do anyway for practice


Um:

char str[80];

Please use code tags...and what issues are you having exactly?
functionality of the class.

class String1 {
private:
char str[80];
public:
// Constructors
String1();
String1(char *);

// Fill a character buffer argument
void GetString(char *);

// Concatenation Operators
String1 operator+(String1 &);
String1 operator+(char *);
friend String1 operator+(char *, String1 &);

// Assignment Operators

// would work without this cuz we do not have
// any pointer members but do anyway for practice
String1 operator=( String1 &);
String1 operator=( char *);

// Console Stream Output
friend ostream &operator<<(ostream &, const String1 &);
};

Be sure to fully test all the functions in main. Main should have lines of the following form, plus more to test the constructor calls, output, etc.

"Dan " + S; // calls friend operator+(char *, String1 &);
S + "Bob"; // calls operator+(char *);
S + S; // calls operator+(String1 &);
S = S3; // calls operator=( String1 &);
S = "Bob"; // calls operator=( char *);
cout << S // calls friend ostream &operator<<(ostream &, const String1 &);

His main issue is that he has got a number in his class name
taking the number out of the class name doesn't change anything.
code tags?
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
#include<iostream>
using namespace std;

class String1 {
private:
char str[80];
public:
// Constructors
String1();
String1(char *);

// Fill a character buffer argument
void GetString(char *); 

// Concatenation Operators
String1 operator+(String1 &);
String1 operator+(char *);	
friend String1 operator+(char *, String1 &);

// Assignment Operators

// would work without this cuz we do not have
// any pointer members but do anyway for practice
String1 operator=( String1 &);
String1 operator=( char *);

// Console Stream Output
friend ostream &operator<<(ostream &, const String1 &);
};


ostream & operator<<(ostream & stream, sIn &)
{
stream << sIn.str;
return stream;
}

String1 String1::operator+(String1 & sIn)
{
String1 temp(this->str);
strcat(temp.str, sIn.str);
return temp;
}
String1::operator =(String1 & sIn)
{
str = new char[strlen(sIn.str) + 1];
strcpy(str, sIn.str);
String1 temp(this->str);
return temp;// string with no name
}
void String1::GetString(char * strBufPtr)
{
strcpy(strBufPtr, str);
}
String1::String1()
{
str[0] = 0;
}
String1::String1(char * strIn)
{
strcpy(str, strIn);
}

void main()
{
String1 S("Bob");
char buffer[20];
S.GetString(buffer);
cout << buffer;

//String1 S1, S2("Bob"), S3("Jones");
//S1 = S2;
//S1 = S2 + S3("Bob"), S3("Jones");
//	String1 S1, S2("Bob");
//cout << S1<< S1;
}
Last edited on
That looks great how do I use code tags?

The problem I am having is: I have most of this written down but I need a shove in the right direction so I can figure out what I am doing wrong.
My post wasn't meant to solve your problem. Instead it should point out that numbers in identifiers are generally or even always a bad idea.
Topic archived. No new replies allowed.