Need help with classes lab


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Need help with classes lab

  1. #1
    Join Date
    Mar 2007
    Posts
    28

    Need help with classes lab

    Hey everyone. :) I'm working on a lab for school and I am a bit confused. Just to warn you, I am a complete noob to programming, so this thread is probably filled with noobish questions. Ok so here's the lab:

    Refer to the MyString Class files for the .h and .cpp files add the following member functions to the class:

    int getStrLen() const; return the current length of a MyString object

    void strCat(const MyString &s); concatenate MyString object s to the end of the current MyString object

    int strCmp(const MyString &s) const; compares the s MyString object to the current MyString object and returns:
    0 if current MyString object == s
    int <0 if current MyString object < s
    int >0 if current MyString object > s

    MyString operator +(const MyString &s)const; returns the concatenation of the s MyString object to the current MyString object


    Write a driver main function to test the new member functions you have added to the MyString Class.


    The functions he has listed for me to add to the classes are all built-in functions in the string library, so why would we have to add code for it in the header and cpp files in the MyString class?

    Plus I don't understand the operator overload he has listed and the explanation for what it does next to it. Why would that return the concatenation of the s MyString object to the current MyString object?

    Here are the header and cpp files for the MyString Class I am supposed to modify:

    Header File:
    Code:
    #ifndef myString_h
    #define myString_h
    
    #include <iostream>
    
    using namespace std;
    
    class MyString
    {
    private:
    
    	char *str;
    
    	int  len;
    
    	static int numStrs;
    
    public:
    	MyString();
    	MyString(const char *s);
    	MyString(const MyString &s, int n);
    
    	~MyString();
    
    	MyString(const MyString &st);
    
    	MyString &operator =(const MyString &st);
    
    	friend ostream & operator <<(ostream &outs, const MyString &st);
    };
    
    
    #endif
    Source File (cpp):
    Code:
    #include <iostream>
    #include <cstring>
    
    #include "myString.h"
    
    using namespace std;
    
    int MyString::numStrs = 0;
    
    MyString::MyString()
    {
    	len = 0;
    
    	str = new char[1];
    
    	str[0] = '\0';
    
    	cout << "\nString #" << ++numStrs << ";\""
    		 << str << "\" default string created.\n";
    }
    
    MyString::MyString(const char *s)
    {
    	len = strlen(s);
    	str = new char[len+1];
    
    	strcpy(str, s);
    
    	cout << "\nString #" << ++numStrs << ";\""
    		 << str << "\" string created from user string.\n";
    
    }
    
    MyString::MyString(const MyString &s, int n)
    {
    	len = strlen(s.str);
    
    
    	if(n <= len)
    		len = n;
    	else
    		len--;
    
    	str = new char[len + 1];
    
    	strncpy(str, s.str, len);
    	str[len] = '\0';
    
    
    	cout << "\nstring #" << ++numStrs << ": \""
    		 << str << "\" string created from substring.\n\n";
    }
    
    MyString::~MyString()
    {
    	numStrs--;
    	cout << "\n\"" << str << "\" object deleted;"
    		 << numStrs << " strings left.\n";
    
    	delete [] str;
    }
    
    MyString::MyString(const MyString &st)
    {
    	numStrs++;
    	len = st.len;
    	str = new char[len + 1];
    	strcpy(str, st.str);
    
    	cout << "\nstring #" << numStrs << ": \""
    		 << str << "\" created in copy constructor\n\n";
    }
    
    MyString &MyString::operator =(const MyString &st)
    {
    	if (this == &st)
    		return *this;
    	else
    	{
    		delete []str;
    		len = st.len;
    		str = new char[len + 1];
    		strcpy(str, st.str);
    		return *this;
    	}
    }
    
    ostream & operator <<(ostream &outs, const MyString &st)
    {
    	outs << st.str;
    	return outs;
    }
    I don't want anyone to do the lab for me. I just want help in understanding what my teacher wants me to do. I thank anyone who can help me. :)

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    There are many occasions in which you build a class on top of another class and use the "low level" methods of the lower class to perform the functions of the "upper" class. You are using char* for your basic data structure; you are implementing or adapting the standard library's methods for char* to be "string" like.

    Your method which overloads the "+" operator will concatenate "this" [the MyString object which appears before the "+"] with the MyString object which follows the "+".
    It is part of your assignment to make sure that this operation occurs when two MyString objects surround a "+" by defining this function.

  3. #3
    Join Date
    Mar 2007
    Posts
    28
    Hmmm... ok I think I got it.

    Now when I tried to just compile the original code (MyString .h & .cpp files) with no modifications whatsoever, it keeps giving me one error:

    The error is: 'str' cannot access private member declared in 'MyString'
    see declaration of 'str'

    This error takes me to these two segments of code:

    (1) outs << st.str; // from the MyString cpp file

    (2) char *str; // from the MyString h file


    I'm not sure why this is so, because the overloaded operator in the header file has the keyword friend in front of it. Therefore it should have access to the private data, correct, or is there something different you have to do for pointers? :confused:

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    Are you suing Visual C++ 6.0 per chance? In that case, you need to upgrade it or use a different compiler. 6.0 is a can of worms.
    Danny Kalev

  5. #5
    Join Date
    Mar 2007
    Posts
    28
    Danny
    Are you suing Visual C++ 6.0 per chance? In that case, you need to upgrade it or use a different compiler. 6.0 is a can of worms.



    Yes, but I think we have to use that compiler. I may be able to work with VS 2005, but I will have to ask my teacher.

    Thanks for your help you guys. :)

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    Well, try to use a different compiler for the time being, and explain to your teacher that 6.0 is hopelessly buggy, particularly with respect to namespaces and friend declatations (and at least about two more hundred cardinal issues). VS2005 rocks, btw!
    Danny Kalev

  7. #7
    Join Date
    Mar 2007
    Posts
    28
    Ok, well I've been working really hard on this all day, and I am still having problems with my code. Every single time it executes, it crashes at the end.
    My teacher said it has something to do with my pointer and not having enough memory. I know it is because of my concatenations, but I don't know why. Below is my modified MyString header file, modified MyString cpp file, and my driverMyString file (to test the code). If anyone could help me it would be greatly appreciated. :)

    MyString.h:
    Code:
    #ifndef myString_h
    #define myString_h
    
    #include <iostream>
    
    using namespace std;
    
    class MyString
    {
    private:
    
    	char *str;
    
    	int len;
    
    	static int numStrs;
    
    public:
    	MyString();
    	MyString(const char *s);
    	MyString(const MyString &s, int n);
    
    	~MyString();
    
    	MyString(const MyString &st);
    
    	MyString& operator =(const MyString &st);
    
    	friend ostream & operator <<(ostream &outs, const MyString &st);
    
    //
    // Added new method prototypes
    //
    	MyString operator + (const MyString &s) const;
    
    	int getStrLen() const;
    	
    	int strCmp(const MyString &s);
    
    	void strCat(const MyString &s);
    	
    };
    
    
    #endif

    MyString.cpp:
    Code:
    #include <iostream>
    #include <cstring>
    
    #include "myString.h"
    
    using namespace std;
    
    int MyString::numStrs = 0;
    
    MyString::MyString()
    {
    	len = 0;
    
    	str = new char[1];
    
    	str[0] = '\0';
    
    	cout << "\nString #" << ++numStrs << ";\""
    		 << str << "\" default string created.\n";
    }
    
    MyString::MyString(const char *s)
    {
    	len = strlen(s);
    	str = new char[len+1];
    
    	strcpy(str, s);
    
    	cout << "\nString #" << ++numStrs << ";\""
    		 << str << "\" string created from user string.\n";
    
    }
    
    MyString::MyString(const MyString &s, int n)
    {
    	len = strlen(s.str);
    
    
    	if(n <= len)
    		len = n;
    	else
    		len--;
    
    	str = new char[len + 1];
    
    	strncpy(str, s.str, len);
    	str[len] = '\0';
    
    
    	cout << "\nstring #" << ++numStrs << ": \""
    		 << str << "\" string created from substring.\n\n";
    }
    
    MyString::~MyString()
    {
    	numStrs--;
    	cout << "\n\"" << str << "\" object deleted;"
    		 << numStrs << " strings left.\n";
    
    	delete [] str;
    }
    
    MyString::MyString(const MyString &st)
    {
    	numStrs++;
    	len = st.len;
    	str = new char[len + 1];
    	strcpy(str, st.str);
    
    	cout << "\nstring #" << numStrs << ": \""
    		 << str << "\" created in copy constructor\n\n";
    }
    
    MyString &MyString::operator =(const MyString &st)
    {
    	if (this == &st)
    		return *this;
    	else
    	{
    		delete []str;
    		len = st.len;
    		str = new char[len + 1];
    		strcpy(str, st.str);
    		return *this;
    	}
    }
    
    ostream & operator <<(ostream &outs, const MyString &st)
    {
    	outs << st.str;
    	return outs;
    }
    
    //
    // Modified code starts here
    //
    
    //******************************************************************
    //
    // Function name: getStrLen()
    //
    // Purpose: Counts the number of characters in str not including 
    //            null
    //
    // Input parameters: none 
    //
    // Output parameters: none
    //
    // Return Value: strlen(str) - returns length of string object 
    //
    //*******************************************************************
    int MyString::getStrLen() const
    {
    
    	return (strlen(str));
    
    } // end of getStrLen()
    
    
    //******************************************************************
    //
    // Function name: strCmp (const MyString &s)
    //
    // Purpose: Compares the values of two strings
    //
    // Input parameters: s MyString argument
    //
    // Output parameters: none
    //
    // Return Value: strcmp (str, s.str) - returns int compare value
    //
    //*******************************************************************
    int MyString :: strCmp (const MyString &s)
    {
    
    	return strcmp(str, s.str);
    
    } // end of strCmp (const MyString &s)
    
    
    //******************************************************************
    //
    // Function name: strCat(const MyString &s)
    //
    // Purpose: Concatenates one string to the end of another 
    //
    // Input parameters: s MyString argument
    //
    // Output parameters: none
    //
    // Return Value: none
    //
    //*******************************************************************
    void MyString :: strCat(const MyString &s)
    {
    	strcat(str, s.str);
    	
    } // end of strCat (const MyString &s)
    
    
    //******************************************************************
    //
    // Function name: operator +(const MyString &s) const
    //
    // Purpose: Concatenates one string to the end of another 
    //
    // Input parameters: s MyString argument
    //
    // Output parameters: none
    //
    // Return Value: *this - returns contcatenation of str 
    //
    //*******************************************************************
    MyString MyString :: operator +(const MyString &s) const
    {
    	
    	strcat(str, s.str);
    
    	return *this;
    }// end of operator + (const MyString &s) const
    
    //
    // Modified code ends here
    File To Test Code: driverString.cpp:
    Code:
    #include <iostream>
    
    #include "myString.cpp"
    
    using namespace std;
    
    int main ()
    {
    	MyString st1 ("Hello"),
    		     st2 ("Goodbye"),
    			 st3;
    
    //
    // Testing the getStrLen method 
    //
    	cout << "Using getStrLen()" 
    		 << "-----------------" << endl;
    
    	cout << st2 << endl << "# of characters: " << st2.getStrLen();
    
    
    //
    // Testing the concatenation method
    //
    	cout << endl << "Using strCat()" 
    		 << "--------------" << endl;
    
    
    	st2.strCat(st1);
    
    	cout << st2 << endl;
    
    //
    // Testing the overloaded operator +
    //
    	cout << "Using overloaded operator + ..." 
    		 << "-------------------------------" << endl;
    	
    	st3 = st1 + st2;
    
    	cout << st3 << endl;
    
    //
    // Testing the comparison method 
    //
    	cout << "Using strCmp()" 
    		 << "--------------" << endl;
    
    	st2.strCmp(st1);
    
    	int compare;
    
    	compare = st1.strCmp(st2);
    
    	if(compare == 0)
    		cout << "The two objects are the same";
    	else if(compare < 0)
    		cout << st1 << " is less than " << st2;
    	else
    		cout << st1 << " is greater than " << st2;
    
    	return 0;
    }// end of main
    I have no clue how to fix my overloaded operator ( + ) method or
    my strCat(const MyString &s) method. Please Help!

    Note: I can only change the code inside the methods and nothing else.

  8. #8
    Join Date
    Nov 2003
    Posts
    4,118
    I see two problems here. First of all, you should keep the string's size as a private data member instead of calling strlen(). This way, each string object always knows what its size is and can report it. Secondly, make sure that strncat writes the resulting string to a temporary buffer that is st1.len()+str2.len()+1 bytesl long. Then, delete [] str1.s and assign to str1 the temp buffer's address.
    Danny Kalev

  9. #9
    Join Date
    Mar 2007
    Posts
    28
    Hmmm.. ok I think I got it. Thanks again Danny. :)

Similar Threads

  1. Loading classes at runtime
    By Shayne in forum Java
    Replies: 3
    Last Post: 05-12-2007, 08:15 AM
  2. Gradebook program
    By [gx]Shadow in forum Java
    Replies: 5
    Last Post: 10-25-2006, 10:20 PM
  3. java classes in windows and linux
    By DaveMere in forum Java
    Replies: 2
    Last Post: 08-09-2005, 01:11 PM
  4. Hide Public Classes in UserControl
    By RobDog888 in forum .NET
    Replies: 1
    Last Post: 04-15-2005, 05:43 PM
  5. COM+ SOAP 2.0 and Abstract Classes
    By Constant in forum VB Classic
    Replies: 0
    Last Post: 07-05-2002, 03:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center