No older revisions available
No older revisions available
~cpp
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class newstring
{
public:
char * ch;
public:
newstring::newstring()
{
ch = '\0';
}
explicit newstring::newstring(int num)
{
ch = new char[num];
for(int i =0; i < num; i++)
ch[i] = '\0';
}
newstring::~newstring()
{
delete [] ch;
}
int newstring::length() const
{
return strlen(ch);
}
newstring::newstring(const char* ch)
{
this->ch = new char[strlen(ch)+1];
strcpy(this->ch,ch);
}
newstring::newstring(const newstring & ns)
{
ch = new char[strlen(ns.ch)+1];
strcpy(ch,ns.ch);
}
operator char*()
{
return ch;
}
void operator+=(const newstring & a)
{
char * temp = new char[strlen(ch)+strlen(a.ch)+1];
strcpy(temp,ch);
strcpy(temp+strlen(ch),a.ch);
delete [] ch;
ch = temp;
}
/*
newstring & newstring::operator=(const char* ch)
{
if(this->ch == ch)
return *this;
delete [] this->ch;
this->ch = new char[strlen(ch)+1];
strcpy(this->ch, ch);
return *this;
}
*/
newstring & newstring::operator=(const newstring ns)
{
if(this->ch == ns.ch)
return *this;
delete [] this->ch;
ch = new char[strlen(ns.ch)+1];
strcpy(this->ch, ns.ch);
return *this;
}
};
bool operator==(const newstring & a, const newstring & b)
{
if(strcmp(a.ch,b.ch) == 0)
return true;
else
return false;
}
newstring & operator+(const newstring & a, const newstring & b)
{
newstring *temp = new newstring(strlen(a.ch)+strlen(b.ch)+1);
strcpy(temp->ch,a.ch);
strcpy(temp->ch+strlen(a.ch),b.ch);
return *temp;
}
ostream & operator<<(ostream & os, const newstring& ns)
{
os << ns.ch;
return os;
}
istream & operator>>(istream & is, newstring & ns)
{
char temp[100];
is >> temp;
delete [] ns.ch;
ns.ch = new char[strlen(temp)+1];
strcpy(ns.ch,temp);
return is;
}
int main()
{
/* newstring s = "123";
cout << s << endl;
newstring s = "123";
cout << s.length() << endl;
const newstring s = "123";
cout << s.length() << endl;
newstring s1 = "123", s2 = "456";
cout << s1 << s2 << endl;
newstring s;
cout << "string?";
cin >> s;
cout << "input string : " << s << endl;
newstring s1 = "123";
newstring s2 = s7;
cout << s2;
newstring s1 = "123", s2;
s2 = s1;
cout << s1 << " " << s2 << endl;
newstring s1, s2;
s1 = "123";
s2 = s1;
cout << s1 << " " << s2 << endl;
newstring s = "12345";
cout << s[2] << endl;
s[2] = 'X';
cout << s << endl;
newstring s(10);
s[0] = 'a';
s[1] = 'A';
s[2] = 'B';
cout << s << endl;
newstring s = 10;
if(s == 10)
cout << "s는 10입니다" << endl;
else
cout << "s는 10아닙니다" << endl;
newstring s1 = "123", s2 = "456";
cout << s1+s2 << endl;
if(s1 == "123")
cout << "s1은 123입니다." << endl;
s1 += s2;
cout << s1 << endl;
*/
newstring s;
s = "123";
char s2[1024];
strcpy(s2,s);
cout << s2 << endl;
return 0;
}