Wednesday, September 29, 2010

(OOPS)PROGRAM USING CONSTRUCTOR, DESTRUCTOR AND ASSIGNMENT OPERATOR OVERLOADING

EX.NO:                 PROGRAM USING CONSTRUCTOR, DESTRUCTOR
DATE:                      AND ASSIGNMENT OPERATOR OVERLOADING

AIM:
                To create two strings and concatinate using default constructors,parametrized constructors,copy constructor,destructor and overloaded assignment operator.

ALGORITHM:

            STEP1: Start the process.

            STEP2: A default constructor is created for string length allocation.

STEP3: A parameterized constructor is created in which parameter name is copied to another variable.

STEP4: An assignment operator is overloaded to copy one object name or value
                          to another.

            STEP5: A copy constructor is created which refers to the address of another
                         Object value.

            STEP6: Concatenation is performed with two strings.

            STEP7: Many objects are created and made to access the different constructors and the overloaded    assignment operator
              

             PROGRAM:

             #include<iostream.h>
             #include<conio.h>
             #include<string.h>
            #include<stdlib.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);
             }
             string(string &s)
             {
             length=strlen(s.name);
             name=new char(length +1);
             strcpy(name,s.name);
             }
             void display(void)
             {
              cout<<"\n"<<name;
             }
              void operator=(string s);
             string operator+(string s);
             ~string()
             {
              free(name);
             }
             };
            void string::operator=(string s)
            {
             length=strlen(s.name);
             name=new char[length +1];
             strcpy(name,s.name);
             }
             string string::operator+(string s)
            {
             string tm(strcat(name,s.name));
             return tm;
             }
             void main()
             {
             clrscr();
             char*first="JOSEPH";
             string name1(first),name2("LOUIS"),name3(name 2);
             string name 4=name1,name 5=name1+name2;
             cout<<"\n in copy constructor";
             name3.display ();
             cout<<"\n\n assignment operator";
             name4.display();
             cout<<"\n Concatinated name";
             name5.display();
             getch();
             }


            RESULT:
                           Thus  the two strings are created and concatinated using default constructor,parametrized constructor,copy constructor,overloading of assignment operator, destructor and output is verified succesfully.