CrazyEngineers
  • Need the full working code

    Updated: Oct 25, 2024
    Views: 955
    the out put must be like this

    mohit + bhalla = mohit bhalla
    abcpqrabc - pqr= abcabc;


    we have to use operator overloading and copy constructor;
    okies


    if any one have time than please hlp me
    i need it
    {for eg in main i have char c1,c2,c3;
    c1=mohit;
    c2=bhalla;
    than c3=c1+c2;
    c3.print();
    }out put mst be mohitbhalla
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • yamrajbhalla

    MemberMar 28, 2009

    any hlp plzzzz
    Are you sure? This action cannot be undone.
    Cancel
  • shalini_goel14

    MemberMar 28, 2009

    yamrajbhalla
    any hlp plzzzz
    Please specify in which language you need? 😐
    Are you sure? This action cannot be undone.
    Cancel
  • Raviteja.g

    MemberMar 28, 2009

    if you want the code in java here it is
    import java.io.*;
    class add
    {
    public static void main(String args[])
    {
    String c1="mohit";
    String c2="bhalla";
    String c3=c1+c2;
    System.out.println(c3);

    }
    }
    Are you sure? This action cannot be undone.
    Cancel
  • yamrajbhalla

    MemberMar 28, 2009

    #include<iostream.h>
    #include<conio.h>
    #include<string.h>


    class b
    {
    char nm[30];

    public:
    b()
    {strcpy(nm,"unknown");}

    b(char nm[])
    {
    strcpy(this->nm,nm);
    }

    void setname(char m[])
    {
    strcpy(nm,m);
    }

    void print()
    {
    cout<<nm;
    }

    b operator +(b c)
    {
    b t;
    char nem[50];

    for(int i=0;nm!='\0';i++)

    { nem=nm;


    for(int j=0;c.nm[j]!='\0';j++)
    {nem[i+j+1]=c.nm[j];

    } }
    strcpy(t.nm,nem);





    return t;
    }
    };


    void main()
    {
    b b1,b2,b3;
    clrscr();

    b1.setname("mohit");
    b2.setname("bhalla");
    b3=b1+b2;
    Are you sure? This action cannot be undone.
    Cancel
  • yamrajbhalla

    MemberMar 28, 2009

    b1.print();cout<<" + ";b2.print();cout<<" = ";b3.print();
    getch();
    }



    in c++
    Are you sure? This action cannot be undone.
    Cancel
  • yamrajbhalla

    MemberMar 28, 2009

    the above code is not wrking good its shhowing mohitbhallaa
    and if i change the second sstring even that its showing me mohitbhallaa
    Are you sure? This action cannot be undone.
    Cancel
  • yamrajbhalla

    MemberMar 28, 2009

    i did it thanks aa lott
    but
    now
    the main problem


    for overloading with - operator

    if my frst string s1 = mohitbhalla and
    second string s2 =bhalla

    than
    string s3=mohit;
    Are you sure? This action cannot be undone.
    Cancel
  • yamrajbhalla

    MemberMar 29, 2009

    please rply ppl
    i need it and i still nt on it
    Are you sure? This action cannot be undone.
    Cancel
  • pradeep_agrawal

    MemberMar 30, 2009

    Its always a good practice to first try to solve the problem and then get help if stuck somewhere. We will be more than happy to help in that case.

    Could you please post code for overloading the '-' operator for the problem statement and the problem you are facing in getting the desired result.

    Note: The logic to solve the issue can be to parse the string s1 for string s2 and if s2 is found then remove s2 from s1 by moving the characters after s2 to the index starting from s2 location.

    -Pradeep
    Are you sure? This action cannot be undone.
    Cancel
  • yamrajbhalla

    MemberMar 31, 2009

    not getting the desired results

    string s1=cooperhead;
    string 2 = head;
    s3=s2-s1;
    but
    output is
    cooperhead

    if u have logic than reply please



    b operator -(b c)
     { b t;
       char tm[50];
       for(int i=0;i<=(strlen(nm)+1);i++)
         { for(int j=0;j<=(strlen(c.nm)+1);j++)
          if (nm[i]!=c.nm[j])
              tm[i]=nm[i];
    
          }
        strcpy(t.nm,tm);
        return t;
    Are you sure? This action cannot be undone.
    Cancel
  • pradeep_agrawal

    MemberMar 31, 2009

    If found multiple issue with the below code:

    b operator -(b c)
     { b t;
       char tm[50];
       for(int i=0;i<=(strlen(nm)+1);i++)
         { for(int j=0;j<=(strlen(c.nm)+1);j++)
          if (nm[i]!=c.nm[j])
              tm[i]=nm[i];
    
          }
        strcpy(t.nm,tm);
        return t;
    
    The issues are:
    - You are running a loop from 0 to (strlen of string + 1). The (strlen + 1) represent some garbage character. The loop should be run for 0 to i (and j) < (strlen of string).
    - After above changes the problem will stuck due to inner loop always starting checking from 0. This will assign the whole string of s1 to temprary array tm (except the characters present in s2), e.g., if s2 is "cooperhead" and s2 is "head", the updated code will result in a string "coopr", i.e., an extra e will get deleted. For this the inner loop should be modified to run from "i to < strlen" and ignore characters in the s1 only if the complete string s2 is found and not if any character of s2 is found.
    - After above changes the strcpy function may throw error because the string array "tm" is not ended by end of string character '/0'. You should do that before the stcpy statement as "tm = '/0';"

    Try above changes and I feel that should make your code working.

    Let us know if you face issue even after above changes.

    A query on the problem statement
    1. Will s2 always appear at end of s1 like "cooperhead" has "head" at the end. If this is the problem statement then the problem can be more easily solved by checking if s2 is present at end of s1 and if yes the just copy s1 to tm and do "tm[strlen(s1) - stelen(s2)] = '/0';"

    2. Or can s2 be anywhere in s1, e.g, s1 is "cooperhead-head2"? If this is the problem statement then the approach you are currently following should be followed. But then depending on what should be removed on running the program as
    - all instances of "head"?
    - or only first instance of "head"?
    - or only last instance of "head"?
    the code will change.

    -Pradeep
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register