The String Class

This is a discussion on The String Class within the C++ Programming forums, part of the General Programming Boards category; Hello. I have been trying to create a program to concatenate two strings using operator overloading for a class STRING. ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    79

    The String Class

    Hello. I have been trying to create a program to concatenate two strings using operator overloading for a class STRING. The output I get consists of random characters. Please tell me why this should happen.

    Code:
    /*
    Program to create a class string, which has a provision to concatenate two strings.
    */
    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    #include<stdio.h>
    
    void nullify(char *,int);
    
    class STRING
    {
    	private:
    		char *p;
    		int len,allocated;
    	public:
    		STRING();
    		STRING(char *);
    		friend STRING operator+(STRING,STRING);
    		void getstring();
    		void showstring();
    		~STRING();
    };
    
    STRING::STRING()
    {
    	len=0;
    	p=new char[1]; nullify(p,1);
    	allocated=1;
    }
    
    STRING::STRING(char *str)
    {
    	if(allocated==1)
    		delete p;
    	len=strlen(str);
    	p=new char[len+1]; nullify(p,len+1); allocated=1;
    	strcpy(p,str);
    }
    
    STRING operator+(STRING s1,STRING s2)
    {
    	STRING sum;
    	char *temp=new char[s1.len+s2.len+1]; nullify(temp,s1.len+s2.len+1);
    	strcpy(temp,s1.p); strcat(temp,s2.p);
    	sum=STRING(temp);
    	delete temp;
    	return(sum);
    }
    
    void STRING::getstring()
    {
    	if(allocated==1)
    		delete p;
    	p=new char[256]; nullify(p,256);
    	gets(p);
    }
    
    void STRING::showstring()
    {
    	cout<<p;
    }
    
    STRING::~STRING()
    {
    	if(allocated==1)
    		delete p;
    }
    
    int main()
    {
    	clrscr();
    	STRING s1,s2,s3;
    	cout<<"Enter the first string:"; s1.getstring();
    	cout<<"Enter the second string:"; s2.getstring();
    	s3=s1+s2;
    	cout<<"The concatenated string is:"; s3.showstring();
    	cout<<endl<<endl<<"PRESS A KEY..."; getch();
    	return(0);
    }
    
    void nullify(char *str,int limit)
    {
    	int ctr;
    	for(ctr=0; ctr<limit; ++ctr)
    		str[ctr]=char(0);
    }
    Compiler : Borland C++ 5.5

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You haven't defined operator= for this class. When you try to use it you get a shallow copy of the members. Since you return a local variable, the memory for it is released and s3's p points to released memory.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    In other words, don't make it a friend function; make it a member function of STRING.

    Also, make nullify a method of STRING.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    148
    You use the wrong delete.
    new <-> delete
    new[] <-> delete []

    You also need a (user defined) copy-constructor.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. class object manipulation
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2004, 10:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21