Thread: Write a copy constructor for "String" class...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    52

    Post Write a copy constructor for "String" class...

    I want to create a copy constructor for my String class(no string library)..like this :
    String (const String & string1),I want to copy string1 to "String" object in "main" function, ...do I have to examine every character in string1 and copy to String" object in "main" function ? . I don't know how to count length of string1,so what I have to do? Or a overload function like this:
    void operator=(const String &copy) , how to copy "copy" to "String" object.... Don't use "string" library...
    please help me...thanks very much....
    Last edited by zaracattle; 10-11-2006 at 05:08 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your String class like? If you are using say, a std::vector<char> to store the contents of the string, then you do not need to explicitly implement a copy constructor. If you are using a char* instead, then you need to write a copy constructor to copy each character.

    I don't know how to count length of string1
    Is string1 a String object? If so, you may have a size member variable in your String class, and you can then use that. If it is a null terminated string, then use strlen(). However, if you are creating a new String object from a null terminated string, you would likely create a separate constructor for that - it would not be the copy constructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    Quote Originally Posted by laserlight
    If you are using a char* instead, then you need to write a copy constructor to copy each character.
    I'm using it...I don't use vector library....
    this my code :
    Code:
    #ifndef STRING_H
    #define STRING_H
    #include "List.h"
    
    class String{
    public:
    	String();
    	~String();
    	String(const String&);
    	String(const char*);
    	String(List<char> &copy);
    	void operator=(const String &copy);
    	const char* c_str() const;//examine every character...
    	static bool operator==(const String &first, const String &second);
    	bool operator>(const String &first, const String &second);
    	bool operator<(const String &first, const String &second);
    	bool operator>=(const String &first, const String &second);
    	bool operator<=(const String &first, const String &second);
    
    protected:
    	char * entry;//examine every character...
    	int length;
    };
    
    #endif
    "String.cpp"
    Code:
    String::String(const String &string1 ){
    	int i;
    	length = strlen(string1);
    	for (i=0;i<length,i++)
    		entry[i] = string1.c_str()[i];
    	entry[length] = '\0';
    }
    
    void String::operator=(const String &copy){
    	int i;
    	length = strlen(copy);
    	for (i=0;i<length,i++)
    		entry[i] = copy.c_str()[i];
    	entry[length] = '\0';
    }
    
    const char * String ::c_str() const{
    	return (const char*) entry;
    }
    Are these true ?...
    Last edited by zaracattle; 10-11-2006 at 05:51 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    1. To allow for operator chaining, your copy assignment operator should return a String&
    2. All your comparison operators would be better implemented as free functions.
    3. It may be a good idea to have a capacity member variable. Your length member variable would store the number of characters in use, while the capacity would be the number of characters that can be stored before reallocation of memory has to take place.

    Speaking of which, you did not do any memory allocation in your copy constructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    thanks very much for your advice....

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    In copy assignment operator function,it doesn't unserstand "strlen()" function,sp how I have to do??(don't use string library)

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    1. Implement the copy constructor. For the length, just copy the length of the String object to be copied.
    2. Implement a swap function. No strlen() needed here either, since you just swap the lengths and the entry pointers.
    3. Implement the copy assignment operator using the copy constructor and the swap function.

    You will need strlen() functionality for the constructor that takes a const char* argument, but such functionality is easy to write: just count characters until you reach the first null character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    Quote Originally Posted by laserlight
    Implement the copy constructor. For the length, just copy the length of the String object to be copied.
    Sorry...How to "copy the length of the String object to be copied" in a constructor...

    Quote Originally Posted by laserlight
    Implement a swap function.
    why need to have a swap function at here...

    Quote Originally Posted by laserlight
    Implement the copy assignment operator using the copy constructor and the swap function.
    How to call a copy constructor in copy assignment operator ?...

    when I use codeuse string.h library)

    length = strlen(string1.c_str());
    compiler warns that :
    " '=' : conversion from 'size_t' to 'int', possible loss of data "
    How to fix it?...
    thanks for your intersting....

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    This is my full code:
    "String.h"
    Code:
    #ifndef STRING_H
    #define STRING_H
    #include "List.h"
    
    class String{
    public:
    	String();
    	~String();
    	String(const String&);
    	String(const char*);
    	String(List<char> &copy);
    	String& operator=(const String &copy);
    	const char* c_str() const;
    protected:
    	char * entry;
    	int length;
    };
    	static bool operator==(const String &first, const String &second);
        static bool operator>(const String &first, const String &second);
    	static bool operator<(const String &first, const String &second);
    	static bool operator>=(const String &first, const String &second);
    	static bool operator<=(const String &first, const String &second);
    #endif
    "String.cpp"

    Code:
    #ifndef STRING_CPP
    #define STRING_CPP
    #include "String.h"
    #include <string>
    using std::string;
    
    String::String(){
    //	entry = NULL;
    	length = 0;
    }
    
    String::~String(){
    	delete entry;
    }
    
    String::String(const String &string1 ){
    	length = strlen(string1.c_str());
    	entry = new char[length+1];
    	for (int i=0;i<length;i++)
    		entry[i] = string1.c_str()[i];
    	entry[length] = '\0';
    }
    
    String& String::operator=(const String &copy){
    	length = strlen(copy.c_str());
    	entry = new char[length+1];
    	for (int i=0;i<length;i++)
    		entry[i] = copy.c_str()[i];
    	entry[length] = '\0';
    	return (*this);
    }
    
    String::String(const char * CharToString){
    	length = strlen(CharToString);
    	entry = new char[length+1];
    	strcpy_s(entry,length, CharToString);
    }
    
    String::String(List<char> &ListChar){
    	length = ListChar.Size();
    	entry = new char[length+1];
    	for (int i=i;i<length;i++)
    		ListChar.Retrieve(entry[i],i);
    	entry[length] = '\0';
    }
    
    const char * String ::c_str() const{
    	return (const char*) entry;
    }
    
     static bool operator==(const String &first, const String &second){
    	return strcmp(first.c_str(),second.c_str()) == 0;
    }
    
     static bool operator <(const String &first, const String &second){
    	return (strcmp(first.c_str(),second.c_str()) < 0);
    }
    
     static bool operator >(const String &first, const String &second){
    	return (strcmp(first.c_str(),second.c_str()) > 0);
    }
    
    static bool operator <=(const String &first, const String &second){
    	return (strcmp(first.c_str(),second.c_str()) <= 0);
    }
    
    static bool operator >=(const String &first, const String &second){
    	return (strcmp(first.c_str(),second.c_str()) >= 0);
    }
    
    
    #endif
    "UseString.cpp"

    Code:
    #include "stdafx.h"
    #include "List.cpp"
    #include "String.cpp"
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	String stringone="abcde";
    	String stringtwo = "1234";
    	stringone = stringtwo;
    	return 0;
    }
    Compiler informs errors that:

    error LNK2005: "public: __thiscall String::String(void)" (??0String@@QAE@XZ) already defined in UseString.obj

    1>String.obj : error LNK2005: "public: __thiscall String::~String(void)" (??1String@@QAE@XZ) already defined in UseString.obj

    1>String.obj : error LNK2005: "public: __thiscall String::String(class String const &)" (??0String@@QAE@ABV0@@Z) already defined in UseString.obj

    1>String.obj : error LNK2005: "public: class String & __thiscall String:perator=(class String const &)" (??4String@@QAEAAV0@ABV0@@Z) already defined in UseString.obj

    1>String.obj : error LNK2005: "public: __thiscall String::String(char const *)" (??0String@@QAE@PBD@Z) already defined in UseString.obj

    1>String.obj : error LNK2005: "public: __thiscall String::String(class List<char> &)" (??0String@@QAE@AAV?$List@D@@@Z) already defined in UseString.obj

    1>String.obj : error LNK2005: "public: char const * __thiscall String::c_str(void)const " (?c_str@String@@QBEPBDXZ) already defined in UseString.obj

    1>E:\School\UseString\Debug\UseString.exe : fatal error LNK1169: one or more multiply defined symbols found
    Last edited by zaracattle; 10-12-2006 at 01:15 AM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sorry...How to "copy the length of the String object to be copied" in a constructor...
    Code:
    String::String(const String& str) {
        length = str.length;
        // ...
    }
    why need to have a swap function at here...
    Do you forsee that users of your class might want to swap String objects?

    How to call a copy constructor in copy assignment operator ?...
    How to call a copy constructor?

    By the way, your destructor has a mistake. new[] should be paired with delete[].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    Thanks for your helping ..But I encountered some above errors that I din't understand why compiler informed these errors...Please help me...

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    Anyone can help me??
    these are the pictures:

    from my folder :
    http://i9.tinypic.com/2ugnuw6.jpg
    from my compiler:
    http://i9.tinypic.com/2vlseg2.jpg

    Please help me...

  13. #13
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Usually, you shouldn't #include source files, just headers.

    Also, don't post pictures of errors just post them directly to the board.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    sorry, How to post pictures directly to the board??

  15. #15
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    No, you misunderstand - there's no need to post pictures of an error log or output - just copy and paste the error messages directly into your post.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  5. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM