I can't write any code for you as this is clearly homework, but I'll try to explain what you should be thinking about and help out with the obvious errors.
A class allows your to create your own type, we call them user defined types. What does that mean? It means you can create new types and have them interact seamlessly with built in types or other user defined types. It allows you to write code like this:
1 2 3 4 5
|
int i = 7; // using a built in type
cout << i << endl;
MyString s = ".jpg"; // user defined type
cout << s << endl;
|
Let's take a look at your code. You've stared with the definition:
1 2 3 4
|
class MyString {
private:
char s[500];
int len;
|
You want to create your own type caled MyString that is implemented by having a fixed length buffer
char s[500]
and the length
int len
.
There a few things to remember about your class. You must decide on the invariants, the things that are always true about your class.
1. the string is held as a null terminated sequence of chars (a C string)
2. the maximum range of lengths for your string is therefore [0 .. 499].
3. len tracks the length of the string
You can then infer a few more invariants:
4. if len is not zero, s[len - 1] is null
5. s[499] should always be null, this means that unused chars in s should be set to null
That should be enough. Why bother with invariants? The help you recognise what state the class should always be in.
So back to your constructors. Constructors are functions that the language calls when you create an instance of your class. It's your opprotunity to set the member variables so that they conform with the class invariants.
In your case, you have 3; the default constructor, initialisation from a C string and a copy constructor. These are called when you write code like this respectively:
1 2 3
|
MyString emptystr;
MyString name = "Nathan Heathman";
MyString dupname = name;
|
So, when you call the default constructor, what should len be? Does it matter what s contains?
When the initialisation from a C string is called what should len be? What should s contain at the end?
And similarly, what should the copy constructor do?
Let's look at your code.
1 2 3 4 5 6 7 8 9 10 11
|
MyString::MyString(const char src[]){
private:
char *src;
int len;
public:
// Default constructor.
MyString()
{
str = 0;
len = 0;
}
|
The syntax here is wrong. Your constructor definitions should look like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
MyString::MyString() // default
{
// do stuff
}
MyString::MyString(const char* src) // don't use const char[], that's really an incomplete type
{
// do stuff
}
MyString::MyString(const MyString& src) // copy constructor
{
// do stuff
}
|
You cannot use private, public as you have. Remember the class inviants, what should the data members contain at the end of each constructor?
1 2 3 4 5 6 7 8
|
// Destructor.
~MyString()
{
if (len != 0)
delete [] str;
str = 0;
len = 0;
}
|
Your destructor is just plain wrong. s is a char[500], so you have no business calling new or delete on it. As the class doesn't allocate any memory and doesn't need to reset anything at the end, your destructor shouldn't contain any code. Remember your invariants! Also, as your constructor doesn't do anything, and the language generates a default for you, you can ignore the destructor all together and use the default destructor.