[Dev-C++] Operator overloading - String concatenation
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Abhijeet S <abh...@ms...> - 2004-08-20 07:22:12
|
Hi guys,
this is the program I have written.. this works fine on Turbo C++ except
that in the end it gives a null pointer assignment after giving the output.
in DevC++ it just accepts the input and crashes.. can anyone tell me why.. I
tried using the while loop structure too but same error also tried finding
out the length of the sting and then using that in the for loop
anyways.. I guess I'm going in the wrong direction so can anyone help me out
?
#include <iostream>
#include <conio.h>
using namespace std;
class strin
{
private:
char *str;
public:
void getstr()
{
cout<<"Enter the string : ";
cin>>str;
}
void putstr()
{
cout<<"String is : "<<str<<endl;
}
strin operator +(strin s2)
{
int i,j;
strin temp;
for(i=0;str[i]!='\0';i++)
temp.str[i] = str[i];
for(j=0;s2.str[j]!='\0';j++)
temp.str[i++] = s2.str[j];
temp.str[i] = '\0';
return temp;
}
};
int main()
{
strin s1,s2,s3;
s1.getstr();
s1.putstr();
s2.getstr();
s2.putstr();
s3 = s1 + s2;
s3.putstr();
getch();
}
_________________________________________________________________
Apply to 50,000 jobs now. http://go.msnserver.com/IN/46921.asp Post your CV
on naukri.com today.
|