Вы находитесь на странице: 1из 2

Write a program to copy one string to another using copy constructor

#include <iostream>
#include <cstring>
using namespace std;
class String
{
char *name;
int length;
public:
String()
{length=0;
name= new char[length+1];
}
String(char *s)
{
length=strlen(s);
name=new char[length+1];
strcpy(name, s);
}
String (String& s)
{
length = s.length;
name=new char[length+1];
strcpy(name,s.name);
}
void display()
{
cout<<name;

}
};
int main()
{
String s1("Kavitha"),s2(s1);
s1.display();
s2.display();
return 0;
}

Вам также может понравиться