string class

Pages: 123
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

#ifndef MYSTRING_H
#define MYSTRING_H

class MyString	
{
private:
         char *pData;		//pointer to simple C-style representation of the string
				//(i.e., sequence of characters terminated by null)
				//pData is only a pointer. You must allocate space for 
				//the actual character data
         int            length;	//length of the string

         // …			//possible other private data

public:
     MyString();	//constructor --- create empty string
     MyString(char *cString); //constructor --- create a string whose data is a copy of
				//cString
     ~MyString();	//destructor -- don't forget to free space allocated by the constructor
			//i.e., the space allocated for the character data

     MyString(MyString const& s);  //override the default copy constructor --- why?
				        //important -- think about it -- possible test question
     MyString operator = (MyString const& s);  //override default assignment operator
     void Put();		//output string
     void Reverse();	//reverse the string
     
     MyString operator + (MyString const& s);   //concatenation operator
     // …					       //other useful member functions 
						       //as you wish
};

#endif   // MYSTRING_H


K so were supposed to use this and do the function definitions to create a c string style class, and a driver to test the class. My problem is im jus unsure where to point the char* pData to a char arrary. Should i do it in the driver, or im jus having trouble, im not sure where or how to point the pointer pData to a char array?

do i do this, and if so how(syntax) or where(driver or constructor)

pData = new char[]

thanks for all the help in advance.
You already have a pretty good idea.
pData = new char[sizehere];
Your destructor should take care to delete that array.
I'm not sure how you'd go about manipulating the array though and that's the problem with c-strings - size. Size is always the problem, because, unlike std::string, the length is invariable.
I hope you don't intend to actually use c-strings but if you must I think you already have the basics. What exactly is your current problem?
yea im jus not sure like where to access that array. but since its private data i wouldnt be able to put that line in the main.cpp file, so where would i put that, or would i jus put that in the private data? Im jus not sure how to access the char array with the pointer, and in the main.cpp how would i access that char array since pData is private? thanks.
You should have a function to return the actual cstring and then you can call that function to access the string. I would also suggest either overloading that one or creating another one to modify the string, and to create a constructor that takes a cstring to initialize from.
If you don't know how to manipulate arrays using pointers, review the documentation section for the tutorial on pointers.
ok thanks a bunch hopefully i figure it out.
im still having trouble with this ill post like what were supposed to do..

you are to develop your own string class. This will give you a chance to develop and work with a C++ class, define constructors and destructors, define member functions outside of the class body, develop a copy constructor and assignment operator (and understand why!), work with C-Style strings and pointers, dynamically allocate memory and free it when done.

I think i can figure out the rest im just not sure what to do with pData, and where to allocate space for the actual string data? any help would be really appreciated. the header file were given is jus in the first post, thanks.
That's where you put it. In your constructor that takes a char* or std::string, you allocate a new array containing that data and attach it to the pointer. That's the purpose of the pointer. It identifies the dynamic memory.
If DMA confounds you to that extent you can check the tutorial on C++ for the dynamic memory chapter.
k so 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
#ifndef MYSTRING_H
#define MYSTRING_H

class MyString	
{
private:
	char *pData;	
	int length;
	char arr[200];

public:
     MyString();	
     MyString(char *cString); 						
	 ~MyString();	
	 MyString(MyString const& s);  
     MyString operator = (MyString const& s);  
	 void Read();
     void Put();		
	 void Reverse();
     MyString operator + (MyString const& s);    
};


MyString::MyString()
{
	length = 0;
	arr = 0;
	pData = arr;
}


No.
Since you don't seem to have read the article I'll give you some example code to work with. Try to avoid copypasting my example but I'm sure you'll understand afterwards.
First get rid of that member arr.
Now here's how you'd initialize the pointer:
char* pData = new char[yoursizehere];
See, pData is a pointer that handles a block of memory on the heap.
Now because it's been a while since I worked with <cstring> and c-style strings, I can't remember how to assign it appropriately. But that's how you handle DMA.
Read the tutorial. I personally guarantee there's a section in there that covers your question.

EDIT: Here you are. http://www.cplusplus.com/doc/tutorial/dynamic/
Last edited on
ok i think i understand the concept and stuff, and how to access the char array that the pointer points to.. this is what i got ... my read function isnt working

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

MyString::MyString()
{
	length = 0;
	pData = new char[100];
}



void MyString::Read()
{
	string s1;

	cout << "Enter the string, and press enter." << endl;
	cin >> s1;

	*pData = s1;

	while (*pData != 0)
	{
		length++;
		(*pData)++;
	}
}


this is error i get,

error C2440: '=' : cannot convert from 'std::string' to 'char *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

and im guessing i have to make that string a char array
Last edited on
The reason is exactly as it says. You cannot convert std::string to char[]. However, if you wish to use std::string you can still do so. string has a member function, c_str(), which returns the c-string equivalent, complete with null termination. Try assigning that instead.
Last edited on
i still get the same error twice, which not sure why, it makes a char array..?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void MyString::Read()
{
	string s1;

	cout << "Enter the string, and press enter." << endl;
	cin >> s1;

	*pData = s1.c_str(); 

	while (*pData != 0)
	{
		length++;
		(*pData)++;
	}
}
Don't dereference pData. Even though it's actually a pointer, treat it as though it were an array.
yea i already did that, still get the same error, lol programming is fun when everything works but it can be pain in the but sometimes lol.
Hmmm... in that case I'm honestly not sure. I haven't worked with C-strings in a while. Try checking out the <cstring> library, I believe there is a strcpy() for this purpose.
EDIT: Yep, here it is: http://www.cplusplus.com/reference/clibrary/cstring/strcpy/
Try that and then tell us what happens.
Last edited on
hmm yea i wish my prof actually taught us it would be nice. thanks for your timethough . ill give that shot
Concentrate on the constructors and the destructor before moving on to the other methods. The necessary private members were given and the supplied comments explain what you need to do.
yea , im having trouble with the class constructor,

MyString::MyString(char * cString)
{


}


mainly the arguments
That should probably be fine. Just do the same thing as the std::string constructor, except strcpy() immediately instead of strcpy()ing from the string's .c_str(). Are you having trouble with strcpy or what?
You'll have to allocate memory to hold a copy of the string. In order to allocate memory with new (or malloc if you cannot use C++), you'll need to know how much to allocate. This function will be useful:
http://www.cplusplus.com/reference/clibrary/cstring/strlen/

Remember that a C string is null terminated and that you'll have to allocate an additional char for the null.

Then you'll have to copy the string into your allocated space. This function should be of interest:
http://www.cplusplus.com/reference/clibrary/cstring/strcpy/

Subsequently, the destructor will delete the allocated memory.
Pages: 123