#include
#include
#include
using namespace std;
class StringClass {
char *p;
int len;
public:
StringClass();
StringClass(char *s, int l);
char *getstring() {
return p;
}
int getlength() {
return len;
}
};
StringClass::StringClass()
{
p = new char [255];
if(!p) {
cout << "Allocation error\n";
exit(1);
}
*p = '\0'; // null string
len = 255;
}
StringClass::StringClass(char *s, int l)
{
if(strlen(s) >= l) {
cout << "Allocating too little memory!\n";
exit(1);
}
p = new char [l];
if(!p) {
cout << "Allocation error\n";
exit(1);
}
strcpy(p, s);
len = l;
}
int main()
{
StringClass stringObject1;
StringClass stringObject2("www.rntsoft.com", 100);
cout << "stringObject1: " << stringObject1.getstring() << " - Length: ";
cout << stringObject1.getlength() << '\n';
cout << "stringObject2: " << stringObject2.getstring() << " - Length: ";
cout << stringObject2.getlength() << '\n';
return 0;
}