C++ program : To work with new and delete operation in constructor with class diagram

#include<iostream.h>
#include<conio.h>
#include<string.h>
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);
   }
   void show()
   {
      cout<<name<<"\n";
   }
   void joint(string &a,string &b)
   {
      length=a.length+b.length;
      name=new char[length+1];
      strcpy(name,a.name);
      strcat(name,b.name);
   }
};

int main()
{
   clrscr();
   string s3;
   char s[50];
   cout<<"Enter the first string name:";
   cin>>s;
   string s1(s);
   cout<<"Enter the second string name:";
   cin>>s;
   string s2(s);
   s3.joint(s1,s2);
   s3.show();
   getch();
   return 0;
}

Class diagram



0 comments:

Post a Comment