You must Sign In to post a response.
  • Category: Programming

    Program using C++ to overload binary operator.


    Have a query about writing a C++ program using overload binary operator? Searching for a program for adding two strings? Scroll through this page and get the solution to your question from our ISC experts here.

    Below is the question which was asked in a question paper of BCA part-III(CS_72) Annual Exam 2015 of Nalanda Open University. This question carries 25 marks and hence require brief explanation and program also.

    Q) Write a C++ program to overload + operator , to find S1+S2, where S1 and S2 are two strings.
  • Answers

    2 Answers found.
  • The following example may help you.

    Input: str1 = "Hi ", str2 = "Friends"
    Output: Hi Friends

    For more detail and code etc., you can refer the website for similar query and solution https://www.geeksforgeeks.org/c-program-to-concatenate-two-strings-using-operator-overloading/

    Hope it might help you.

  • Operator overloading means extending the use of existing operator without changing its basic syntax, precedence etc. Here extending means user can use the existing operator on objects of the class with the same ease that user can use it on basic data types. Operator overloading is done by using a special function called as operator op(). We can overload operator by using two methods namely 1) using simple member function 2) using friend function.
    When unary operator is overloaded using simple member function then it does not require any argument. When unary operator is overloaded using friend function then it require one argument and that argument is always object of the class.
    When binary operator is overloaded using simple function it require one argument as object of the class and another argument may be object of the class, any constant value or any variable of built in data type. By rule left hand side operand must be object of the class and it is responsible for calling operator op function. Second argument took responsibility to pass as argument to function. e.g c=a+b; where a,b and c are object of class. Compiler treat this statement c=a+b; as c=a.operator+(b);
    When binary operator is overloaded using friend function then it require two arguments out of one is compulsory object of the class.
    e.g c=a+b; where a,b and c are object of class. Compiler treat this statement c=a+b; as c=operator+(a,b);
    Below is the program which concatenates two string and form resultant string using binary operator overloading using simple member function.
    Please add iostream.h, conio.h and string.h header files before declaration of class.
    //PROGRAM TO OVERLOAD BINARY + OPERATOR
    #include
    #include
    #include
    class string
    {
    private:
    char * str;
    public:
    string(){}
    string(char * p)
    {
    strcpy(str,p);

    }
    string operator+(string t)
    {
    string temp;
    strcpy(temp.str,str);
    strcat(temp.str,t.str);
    return(temp);
    }
    void display(void)
    {
    cout<<"\n"< }
    };
    void main(void)
    {
    class string s1("Sachin ");
    class string s2("Tendulkar");
    class string s3;
    clrscr();
    cout<<"\nFirst string is\n ";
    s1.display();
    cout<<"\nSecond string is\n ";
    s2.display();
    cout<<"\nResultant string is as below\n";
    s3=s1+s2; //s3=s1.operator+(s2)
    s3.display();
    getch();
    }


  • Sign In to post your comments