#include<iostream>
using namespace std;
#include<string.h>
class sample
{
private:
char *name;
public:
sample(){cout<<"\n Default constructor called."<<endl;}
sample(const char *p);
sample(const sample &ref);
sample& operator=(const sample &obj);
sample& operator+(const sample& obj);
~sample();
};
sample::sample(const char* p)
{
cout<<" One argument constructor called."<<endl;
name = new char[strlen(p)+1];
strcpy(name,p);
cout<<"name: "<<name<<endl;
}
sample::sample(const sample &ref)
{
cout<<"copy constructor called."<<endl;
name=new char[strlen(ref.name)+1];
strcpy(name,ref.name);
}
sample& sample::operator=(const sample &ref)
{
cout<<"overloaded = operator called.\n";
//if(this==&ref)
//return *this;
//delete[] name;//Seg fault
name=new char[strlen(ref.name)+1];
strcpy(this->name,ref.name);
return *this;
}
sample& sample::operator+(const sample &obj)
{
cout<<"overloaded + called.\n";
//name=new char[strlen(obj.name)+1];
strcat(name,obj.name);
cout<<"cancatename is: "<<name<<endl;
}
sample::~sample()
{
cout<<"Destructor called.\n";
//delete[] name;
}
int main()
{
sample s1;//def constr.//destr
sample s2("Hello");//1 arg constr.//destr
sample s3(s2);//copy constr.//destr
sample s4=s3;//copy constr.//destr
sample s5;//def constr.//destr
s5=s1;//overloaded = opr.//No destructor
s2+s3;//overloaded + called//No destr
//cout<<s2.name<<endl;//name is private and accessible only by mf
return 0;
}
/*OUTPUT:
Default constructor called.
One argument constructor called.
name: Hello
copy constructor called.
copy constructor called.
Default constructor called.
overloaded = operator called.
overloaded + called.
cancatename is: HelloHello
Destructor called.
Destructor called.
Destructor called.
Destructor called.
Destructor called.
*/
C++ program to implement:Default constructor, One argument constructor, Copy constructor, overloaded assignment operator, destructor and overload + operator to concatenate two strings in a single program.
ReplyDelete#include
using namespace std;
#include
class sample
{
private:
char *name;
public:
//sample(){cout<<"\n Default constructor called."<name,ref.name);
return *this;
}
sample& sample::operator+(const sample &obj)
{
cout<<"overloaded + called.\n";
//name=new char[strlen(obj.name)+1];//else first word only
name=strcat(name,obj.name);
cout<<"cancatename is: "<<name<<endl;
}
sample::sample(const char* p)
{
cout<<" Default argumented constructor called"<<endl;
if(p)
{
cout<<"with 1 arg."<<endl;
name = new char[strlen(p)+1];
strcpy(name,p);
cout<<"name: "<<name<<endl;
}
else
{
cout<<"with 0 arg."<<endl;
name=new char[1];
name='\0';
//cout<<"name: \n"<<name<<endl;//program will exit if you use name.
}
}
int main()
{
sample s1;
sample s2("Hello");
sample s3(s2);
sample s4;
s4=s2;
s2+s3;
//cout<<s2.name<<endl;//name is private and accessible only by mf
return 0;
}
/*OUTPUT:
Default argumented constructor called
with 0 arg.
Default argumented constructor called
with 1 arg.
name: Hello
copy constructor called.
Default argumented constructor called
with 0 arg.
overloaded = operator called.
overloaded + called.
cancatename is: HelloHello
destructor called.
destructor called.
destructor called.
destructor called.
*/