implementing constructor....Need help

#include < iostream >
#include < fstream >
#include < cstring >

using namespace std;

// This is class specification for MyString
class MyString {
private:
char s[500];
int len;

public:
// Default constructor.
MyString();
// Transfers the content of the C String src to this MyString object.
MyString(const char src[]);
// copy constructor
MyString(const MyString & src);

// Transfers the content of MyString src, in the range of [index, index+length-1], to a new MyString object and returns it.
// In case src does not have length characters, whatever it has is transfered.
MyString substr(int index, int length);

// Returns -1 if c is not found in this object; otherwise, returns the first index from the lower end where s[index]==c.
int find(char c);

int length();

// Returns from inFile a word that is made of non-space, printable characters.
// This function will discard all spaces appearing before the actual word.

// This is equivalent to the old getWord() function!

friend istream & operator >> (istream & inFile, MyString & word);

// Returns a line of characters from inFile until a newline is seen.
// The newline character '\n' will be discarded.
friend void getline(istream & inFile, MyString & line);

// Outputs MyString s to outFile.
// When cout is passed in as the first argument, s is printed on the console.

// This is equivalent to the old print() function.
friend ostream & operator << (ostream & outFile, MyString & s);
};

int main(){

// reads in three words from console.
MyString word1;
MyString word2;
MyString word3;

// Our own >> is called.
cin>> word1>> word2>> word3;

// Our own << is called.
cout << word1 << ' ';
cout << word2 << ' ';
cout << word3 << '\n';

// reads in two lines from a file.
MyString line1;
MyString line2;

ifstream inFile;
inFile.open("my.txt");

getline(inFile, line1);
getline(inFile, line2);

// This is C++'s << operator.
cout << "The first two lines in my.txt are: \n";
// This is our << operator.
cout << line1 << '\n';
cout << line2 << '\n';

// retrieves the first name by calling our own find() and substr().
MyString name;
MyString firstName;

name = "John James Johnson";
int i = name.find(' ');
firstName = name.substr(0, i);

cout << "The first name of John James Johnson is ";
cout << firstName << '.' << '\n';

return 0;
}

MyString::MyString(){
len = 0;
}
________________________________________________
MyString::MyString(const char src[]){
private:
char *src;
int len;
public:
// Default constructor.
MyString()
{
str = 0;
len = 0;
}

// Convert and copy constructors.
MyString(char *);
MyString(MyString &);

// Destructor.
~MyString()
{
if (len != 0)
delete [] str;
str = 0;
len = 0;
}
}
_____________________________________________________
// This is a copy constructor,
// which can be used to create a new MyString using the content of the MyString src.
MyString::MyString(const MyString & src){
int i=0;
while (i < src.len){
s[i] = src.s[i];
i = i+1;
}
len = src.len;
}

MyString MyString::substr(int index, int length){
MyString newString;

int i=0;
while (i < length && index+i < len){
newString.s[i] = s[index+i];
i = i+1;
}
newString.len = i;

return newString;
}

int MyString::find(char c){
int i=0;
while (i < len){
if (s[i] == c)
return i;
i = i+1;
}
return -1;
}

int MyString::length()
{
return len;
}

// IO functions
// This implements the >> operator declared in the class MyString above!
istream & operator >> (istream & inFile, MyString & word){

// bites off all non-printable and whitespace characters!
char c = inFile.peek(); // peeks, but does not consume, the next character in inFile.
while (c!=-1 && (!isprint(c) || isspace(c))){
inFile.get();

c = inFile.peek();
}

int i=0;
c = inFile.peek();
while (isprint(c) && !isspace(c)){
c = inFile.get();
word.s[i] = c;
i = i+1;

c = inFile.peek();
}

word.len = i;

return inFile;
}

void getline(istream & inFile, MyString & line){
int i=0;
char c = inFile.get();
while (c!='\n' && inFile){
line.s[i] = c;
i=i+1;
c = inFile.get();
}
line.len = i;
}


// This implements the << operator declared in the class MyString above!
ostream & operator << (ostream & outFile, MyString & s){
int i=0;
while (i < s.len){
outFile << s.s[i];
i = i+1;
}

return outFile;
}






Need help with the program in between the two lines there we are suppose to be implementing an constructor
Please put your code [code]inside of code tags[/code] to make it easier to read.

It looks like you have private inside a function definition???
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.
Thank you very much for the assistance I believe I understand but if i have any problem I will make sure to come back.
Topic archived. No new replies allowed.