class objects

I am having difficulty with class objects. I cannot figure out 1. why my program won't actually compare the two strings. 2. why and where I am having a memory dump crash. Any and all help would be helpful since I have completely confused myself. The following is my main.cpp, String.cpp, and the header file in that order.

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
#include <iostream>
using namespace std;

#include "GREEN_String.h"


void main ()
	{
	GREEN_String		Str1;
	GREEN_String		Str2 ("AdrianGreen");
	GREEN_String		Str3 (Str2);
	GREEN_String		Str4 ("isstumped");

	cout << "This is a Test display of the third String: " << endl;
	Str3.Display ();
	cout << "This is a Test display of my programs ability to copy Strings: " << endl;
	Str1.Copy (Str2);
	Str4.Copy (Str4);

	Str1.Display ();
	Str4.Display ();

	cout << "The difference between these two strings is: " << Str1.Compare (Str2) << endl;
	cout << "The difference between these two strings is: " << Str3.Compare (Str1) << endl;

	Str1.Concat (Str3);
	Str3.Concat (Str4);
	cout << "This is a test display of my programs ability to concatenate strings: " << endl;
	Str1.Display ();
	Str3.Display ();

}
 


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
 #include <iostream>

using namespace std;
#include <string.h>
#include "GREEN_String.h"


GREEN_String::GREEN_String ()							// default constructor parameters
	{
	pChar =		new char [1];
	pChar [0]	= '\0';
	MaxChars	= 0;
	NumChars	= 0;
	}

GREEN_String::GREEN_String (const GREEN_String & Str)	//copy constructor parameters
	{
	pChar		= new char [Str.Len () + 1];
	strcpy		(pChar, Str.pChar);
	MaxChars	= Str.NumChars;
	NumChars	= Str.NumChars;
	}

GREEN_String::GREEN_String (const char Chars [])		//length of array and copy parameters 
	{
	int			Length;
	Length		= strlen (Chars);
	pChar		= new char [Length + 1];
	strcpy		(pChar, Chars);
	MaxChars	= Length;
	NumChars	= Length;
	}

GREEN_String::~GREEN_String ()							//deletes the data from memory after use
	{
	delete		[] pChar; 
	}

void GREEN_String::Copy (const GREEN_String & Str)		//copy Method for pointer
	{
	if (this != &Str)
			{
			if (MaxChars < Str.NumChars)
					{
					delete [] pChar;
					MaxChars	= Str.NumChars;
					pChar		= new char [MaxChars + 1];
					}
				else;
			NumChars = Str.NumChars;
			strcpy (pChar, Str.pChar);
			}
		else;
	}

void GREEN_String::Copy (const char Chars[])			//copy method for "C" type 
{
	int			Length;
	Length		= strlen (Chars);
	pChar		= new char [Length + 1];
	strcpy		(pChar, Chars);
	MaxChars	= Length;
	NumChars	= Length;
}

int GREEN_String::Compare (const GREEN_String & Str) const		//Compare method for pointer
{
	int			Difference;
	Difference	= strcmp (pChar,Str.pChar);
	return		Difference;

}

int GREEN_String::Compare (const char Chars[]) 			//Compare Method for "C" type
{
	int			Difference;
	int			Length;
	Length		= strlen	(Chars);
	pChar		= new char	[Length + 1];
	Difference	= strcmp	(pChar, Chars);

	return Difference;
}

void GREEN_String::Concat (const GREEN_String & Str)	//Concatenation Method for pointer
{
	if (this == &Str)
	{
	char * pTemp;
	size_t Size (0);
	while ((Size = cin.get ()) != '\0')
	{
	pTemp [Size++];
	strcat (pChar, pTemp);
	}
	}
	else;
	strcat (pChar, Str.pChar);

}

void GREEN_String::Concat (const char Chars[])		//Concatenation Method for "C" type
{
	int			Length;
	Length		= strlen	(Chars);
	pChar		= new char	[Length + 1];
	strcat	(pChar, Chars);
}


void GREEN_String::Read ()
	{
	const	int		Increment (25);

			size_t	NumCharsRead (0);
			char	c;
			char *	pTemp;

	while ((c = cin.get ()) != '\n')
		{
		pChar [NumCharsRead++] = c;
		if (NumCharsRead > MaxChars)
				{
				pTemp = new char [(MaxChars += Increment) + 1];
				memcpy (pTemp, pChar, MaxChars);
				delete [] pChar;
				pChar = pTemp;
				}
			else;
		}
	pChar [NumCharsRead] = '\0';
	NumChars = NumCharsRead;
	}

void GREEN_String::Display () const						//displays function
	{
	cout << pChar << endl;
	}


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
#ifndef GREEN_STRING_H
#define GREEN_STRING_H

class GREEN_String
{
public:
GREEN_String		();										//  constructor
GREEN_String		(const GREEN_String &);					//	copy constructor
GREEN_String		(const char []);						//	"C" String pointer constructor sets the parameter.
~GREEN_String		();										// destructor
int		Compare		(const GREEN_String &) const;
int		Compare		(const char []);
void	Copy		(const GREEN_String &);
void	Copy		(const char []);
void	Concat		(const GREEN_String &);
void	Concat		(const char []);
void	Display		() const;
void	Read		();
size_t	Len			() const;			
private:
char *	pChar;
size_t	NumChars;											// number of characters currently in array
size_t	MaxChars;											// max number of characters that will fit in the array



};

inline size_t GREEN_String::Len () const
	{
	return NumChars;
	}



#endif 
Last edited on
Last edited on
remove method [int GREEN_String::Compare (const char Chars[]) ]

this method has an error:

pChar = new char [Length + 1];

this will set the pChar of the current GREEN_String's instance to a new char array of length [Length+1]

if pChar was pointing to a valid array before this then the instance loses this previous value upon the assignment statement and also causes a memory leak.

you could rectify this method but would be more efficient to simply remove it and allow the constructor that accepts a char array to cater for using the version of the Compare method that accepts a GREEN_String object.

Last edited on
Hmm, I understand your point "Sik," However, I was told that I hate to cater to both entries and would need two different Methods. To be really honest, I have done classes for less than a week and have really confused myself.

Also, note. My code has been edited. Thank you "ne555."
closed account (D80DSL3A)
I see problems with both of your Concat() functions which would cause a crash.
You need to allocate enough space to pChar for both char arrays to be copied into.

In the 1st version Concat (const GREEN_String & Str) I think you want something like:
1
2
3
4
5
        char * pTemp = pChar;// save pointer to old block
         pChar = new char[NumChars + Str.NumChars + 1];// enough for both strings
         strcp(pChar, pTemp);// copy old block into new block
         delete [] pTemp;	// now you can release the old block
	strcat (pChar, Str.pChar);// add 2nd string to 1st 

Similarly for the 2nd version, Concat (const char Chars[]).
This 2nd version could actually handle both cases. Simply call Concat(Str.pChar); from the 1st version.

What is the line while ((Size = cin.get ()) != '\0') for? There should be no console input there. Is this an effort to debug the crash?
yes, yes it is. I thought it kept erasing and rewriting the array when I concat() the same string.
Topic archived. No new replies allowed.