Ex.No:3c Dynamic Constructors

AIM:

         To write a C++ program for string manipulation using dynamic constructors.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Declare the data members and member functions.

STEP 3: Define the dynamic constructor using new keyword.

STEP 4: Define the member function ‘join()’ for manipulating string.

STEP 5: call join function in main function.

STEP 6: Display the strings.

STEP 7: Stop the program.

PROGRAM CODE:

#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
    char *name;
    int length;
    public:
    string()
    {
        length=0;
        name=new char[length+1];        //Dynamic constructor
    }
    string(char *s)
    {
        length=strlen(s);
        name=new char[length+1];
        strcpy(name,s);
    }
    void display()
    {
        cout<<"n  "<<name;
    }
    void join(string &a,string &b);
};
void string::join(string &a,string &b)
{
    length=a.length+b.length;
    delete name;
    name=new char[length+1];
    strcpy(name,a.name);
    strcat(name,b.name);
};
void main()
{
    clrscr();
    string name1(" Object");
    string name2(" Oriented");
    string name3(" Programming Lab");
    string s1,s2;
    s1.join(name1,name2);
    s2.join(s1,name3);
    name1.display();
    name2.display();
    name3.display();
    s1.display();
    s2.display();
    getch();
}
OUTPUT:

dynamic

RESULT:

                 Thus the C++ program for string manipulation using dynamic constructors is written and executed successfully.

Leave a Reply

Your email address will not be published. Required fields are marked *