C++ Programming Blog

 
 
 
# include <iostream.h>
# include <conio.h>
# include <string.h>
class str
{
    char *name;
    int len;
    public:
        str()
        {
            len=0;
            name=new char[len+1];
        }
        str(char *s)
        {
            len=strlen(s);
            name=new char[len+1];
            strcpy(name,s);
        }
        void show()
        {
            cout<<\"NAME IS:->\"<<name<<endl;
        }
        void join(str &a,str &b);
};

void str::join(str &a,str &b)
{
    len=a.len+b.len;
    delete new;
    name=new char[len+1];
    strcpy(name,a.name);
    strcat(name,b.name);
};

main()
{
    clrscr();
    char *first=\"HARSHIL\";
    str n1(first),n2(\"NINAD\"),n3(\"PRATIK\"),n4,n5;

    n4.join(n1,n2);
    n5.join(n4,n3);
    n1.show();
    n2.show();
    n3.show();
    n4.show();
    n5.show();
}

 
 
Didn't find what you were looking for? Find more on Program that provides example of dynamic constructor