#pragma once
class Heystring
{
//non member operator overloading
friend std::ostream &operator<<(std::ostream,const Heystring &rhs);
friend std::istream &operator>>(std::istream,Heystring &rhs);
private:
char *str;
public:
//no args constructor
Heystring();
//args constructor
Heystring(const char *s);
//copy constructor
Heystring(const Heystring &source);
//move constructor
Heystring(Heystring &&source);
//destructor
~Heystring();
//getters&setterss
const char *get_str() const
{
return str;
}
int get_len()
{
return strlen(str);
}
void display() const
{
std::cout<<str<<" "<<strlen(str)<<std::endl;
}
};
Heystring::Heystring()
{
str=nullptr;
str=new char[1];
str='\0';
}
Heystring::Heystring(const char *s)
{
if(s==nullptr)
{
str=nullptr;
str=new char[1];
str='\0';
}
else
{
str=nullptr;
str=new char[strlen(s)+1];
strcpy(str,s);
}
}
Heystring::Heystring(const Heystring &source)
{
str=nullptr;
str=new char[strlen(source.str)+1];
strcpy(str,source.str);
}
Heystring::Heystring(Heystring &&source)
{
str=source.str;
source.str=nullptr;
std::cout<<"Move assignment called"<<std::endl;
}
Heystring::~Heystring()
{
delete [] str;
}
std::ostream &operator<<(std::ostream &os,const Heystring &rhs)
{
os<<rhs.str;
return os;
}
std::istream &operator>>(std::istream &in,Heystring &rhs)
{
char *buff =new char[1000];
in>>buff;
rhs=Heystring{buff};
delete [] buff;
return in;
}