Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Homework HelpRSS Feeds

Program that provides example of dynamic constructor

Posted By: Bethany Hughes     Category: C++ Programming     Views: 31461

Write a program that provides example of dynamic constructor.

Code for Program that provides example of dynamic constructor in C++ Programming

# include <iostream.h>
# include <conio.h>
# include <string.h>
class str
{
    char *name;
    int len;
    public:
        str()
        {
            len=0;
            name=newchar[len+1];
        }
        str(char *s)
        {
            len=strlen(s);
            name=newchar[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=newchar[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();
}
  
Share: 


Didn't find what you were looking for? Find more on Program that provides example of dynamic constructor Or get search suggestion and latest updates.

Bethany Hughes
Bethany Hughes author of Program that provides example of dynamic constructor is from London, United Kingdom.
 
View All Articles

 

Other Interesting Articles in C++ Programming:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Ajay Kumar from India Comment on: Feb 23
ajay kumar from patna
i have a question plz describe it ..
dynamic constructor with using integer pointer variable
i will be grateful to u
if u cleared my doubt

Sachin Singh from India Comment on: May 27
i have a question plz describe it in few wrds it is...

why do these pointers or dynamic variables (here char *name) declared in private(by default) works only in private (when written) and not when written in public only????

and also state plzz what are other conditions where we have to declare these variables in private only n not in public.

i will be grateful to u
if u cleared my doubt

View All Comments