i tried making a string class but my program seems to be repeatedly crashing. why so?
I have the following two files in to my program
1.)Strng.h (in INCLUDE directory)
#ifndef __STRNG_H
#define __STRNG_H
#if !defined(__CONIO_H)
#include <conio.h>
#endif
#if !defined(__STRING_H)
#include <string.h>
#endif
#if !defined(__STDDEF_H)
#include <stddef.h>
#endif
//#if !defined(__IOSTREAM_H)
//#include <iostream.h>
//#endif
#if !defined(__STDIO_H)
#include <stdio.h>
#endif
class String
{
public:
char *string;
public: String(char *str = "")
{
(*this).assignstring(str);
}
~String(){delete string;};
public: void assignstring(char *str)
{
delete string;
string = new char[strlen(str)+1];
if(string != NULL)
strcpy((*this).string,str);
}
public: String operator=(char* str)
{
(*this).assignstring(str);
return *this;
}
public: String operator +(String s)
{
String cat("");
int len = strlen((*this).string)+strlen((s.string))+1;
char *a = new char[len];
if(a != NULL)
{
strcpy(a,(*this).string);
strcat(a,s.string);
cat.assignstring(a);
delete a;
}
else
{
printf("illegal operation");
}
return cat;
}
};
#endif
2.) String2.cpp (in BIN directory)
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <Strng.h>
void main()
{
clrscr();
String str(""),str2(""),str3("");
str = "hello world";
str2 = " howdy world";
str3 = str2+(str+str);
cout<<str3.string;
}