help for concatenation of string and object.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sivakumar Kotamraju
    New Member
    • May 2007
    • 6

    help for concatenation of string and object.

    Hi,
    this is the code to print s.

    [code=cpp]main() {
    sample s1(10,20,30);
    char *s;
    s="Hello"+s1;
    }

    friend char * operator +(char *s,sample &b) {
    char *s1;
    s1 = new char[50];
    char *s2;
    s2 = new char[10];
    strcpy(s1,s);
    strcat(s1,"Widt h");
    sprintf(s2,"%g" ,b.width);
    strcat(s1,s2);
    strcat(s1,"Heig ht");
    sprintf(s2,"%g" ,b.height);
    strcat(s1,s2);
    strcat(s1,"dept h");
    sprintf(s2,"%g" ,b.depth);
    strcat(s1,s2);
    delete [] s2;

    return s1;
    delete []s1;
    }
    [/code]

    Could anybody help me for optimization?
    Last edited by AdrianH; May 31 '07, 12:54 PM. Reason: Please use [code=cpp][/code] tags for readability!!
  • AdrianH
    Recognized Expert Top Contributor
    • Feb 2007
    • 1251

    #2
    Originally posted by Sivakumar Kotamraju
    Hi,
    this is the code to print s.

    [code=cpp]main() {
    sample s1(10,20,30);
    char *s;
    s="Hello"+s1;
    }

    friend char * operator +(char *s,sample &b) {
    char *s1;
    s1 = new char[50];
    char *s2;
    s2 = new char[10];
    strcpy(s1,s);
    strcat(s1,"Widt h");
    sprintf(s2,"%g" ,b.width);
    strcat(s1,s2);
    strcat(s1,"Heig ht");
    sprintf(s2,"%g" ,b.height);
    strcat(s1,s2);
    strcat(s1,"dept h");
    sprintf(s2,"%g" ,b.depth);
    strcat(s1,s2);
    delete [] s2;

    return s1;
    delete []s1;
    }
    [/code]

    Could anybody help me for optimization?
    Optimisation? How can you optimise something that doesn't work? The friend function is inside of a class? I'm pretty sure that friend injection is a deprecated feature in C++. If not, then what is it a friend of?

    Other than that, it looks like quite a mess. What are you attempting to do, and what problems are you encountering?


    Adrian

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Yes, and may I ask why you are not using a string and a strignstream object. All this works has been done for you. Why re-invent the wheel?

      [code=cpp]
      stringstream str;
      str << " Hello" << 10 << 20 << 30;

      string result;
      str >> result;
      [/code]

      Comment

      • AdrianH
        Recognized Expert Top Contributor
        • Feb 2007
        • 1251

        #4
        Originally posted by weaknessforcats
        Yes, and may I ask why you are not using a string and a strignstream object. All this works has been done for you. Why re-invent the wheel?
        I assumed it was part of the assignment.


        Adrian

        Comment

        • Sivakumar Kotamraju
          New Member
          • May 2007
          • 6

          #5
          Originally posted by AdrianH
          Optimisation? How can you optimise something that doesn't work? The friend function is inside of a class? I'm pretty sure that friend injection is a deprecated feature in C++. If not, then what is it a friend of?

          Other than that, it looks like quite a mess. What are you attempting to do, and what problems are you encountering?


          Adrian
          I used friend function because i need to make the statement char*s="Hello"+ s1;
          work. thats why i overloaded + using friend functions because the parameters iam passing are different.

          I also overloaded << to output the object s1. My aim is to see how string class is implemented.and to test the overloading concepts etc..

          Comment

          • Sivakumar Kotamraju
            New Member
            • May 2007
            • 6

            #6
            Originally posted by weaknessforcats
            Yes, and may I ask why you are not using a string and a strignstream object. All this works has been done for you. Why re-invent the wheel?

            [code=cpp]
            stringstream str;
            str << " Hello" << 10 << 20 << 30;

            string result;
            str >> result;
            [/code]

            I want to see how string class is implemented internally. I implemented using string class.In the code above you are taking primitive int type,but my aim is to see for objects.

            Comment

            • AdrianH
              Recognized Expert Top Contributor
              • Feb 2007
              • 1251

              #7
              Originally posted by Sivakumar Kotamraju
              I used friend function because i need to make the statement char*s="Hello"+ s1;
              work. thats why i overloaded + using friend functions because the parameters iam passing are different.

              I also overloaded << to output the object s1. My aim is to see how string class is implemented.and to test the overloading concepts etc..
              Yes, but what I am saying is that you cannot declare a friend outside of a class and you’re not supposed to declare a friend with its body inside of one (well if I understand friend injection correctly, your case could work under the new standard, but you will start getting into trouble when you start doing this using templated classes so be careful, see this article for more info).


              Adrian

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by Sivakumar Kotamraju
                I want to see how string class is implemented internally. I implemented using string class.In the code above you are taking primitive int type,but my aim is to see for objects.
                Why?

                Just write an operator << for your classes and you are done.

                Comment

                • AdrianH
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1251

                  #9
                  Originally posted by weaknessforcats
                  Why?

                  Just write an operator << for your classes and you are done.
                  Yes, and by doing it with ostream as your left hand side (first) parameter, it would work for stringstream too since it inheirits from ostream.


                  Adrian

                  Comment

                  Working...