Page 158 - DCAP404 _Object Oriented Programming
P. 158

Unit 7: Operator Overloading




                         str::str()                                                             Notes
                         {
                                name  =  new  char[1];
                         }
                         str::str(char  *a)
                         {
                                int  i  =  strlen(a);

                                name  =  new  char[i  +  1];
                                strcpy(name,  a);
                         }
                         str  str::operator  +  (str  s)
                         {
                                int  i  =  strlen(name)  +  strlen(s.name);
                                str  tmp;

                                tmp.name  =  new  char[i+1];
                                strcpy(tmp.name,  name);
                                strcat(tmp.name,  s.name);
                                return  tmp;
                         }
                         void  str::get()
                         {
                         cin  >>  name;
                         }

                         void  str::show()
                         {
                         cout  <<  name;
                         }
                         void  main()
                         {
                         clrscr();

                                str  S3;
                                str  S1(“hello”);
                                str  S2(“monty”);
                                S3  =  S1  +  S2;
                                S3.show();
                         }






                                           LOVELY PROFESSIONAL UNIVERSITY                                   151
   153   154   155   156   157   158   159   160   161   162   163