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

    C++ program for overloading unary ++ operator using friend function

    Want a C++ program solution for overloading unary operator? Searching for the solution online? Check out this page and get the detailed solution along with explanation on this Ask Expert page.

    Following is the question asked in MSBTE(Maharashtra State Board of Technical Education, Mumbai) Diploma in Computer Engineering question paper for Winter 2017 examination.
    This question is asked for 4 marks and hence requires some explanation along with program code. Please let me know the solution.
    Q) Write a program which overload unary ++ operator by using friend function. [4 Marks]
  • Answers

    1 Answers found.
  • Operator overloading is a new concept provided by Object Oriented Programming. As earlier stated in many answers, though class is user defined data type but it behaves like built in data type. The operations which can be applied on built in data type can be easily applied on class objects. Operator overloading means to extend the meaning of existing operator. In other words operator overloading is nothing but to apply the required operator on class object. We can overload existing operators only. Operator overloading does not mean that we are defining a new operator. Also while overloading a certain operator we can not change the basic meaning, operator precedence of the operator. The task of operator overloading is done by using a special function which is generally called as operator op function.
    General syntax of operator function is
    return data type operator op (argument list);
    here operator is a keyword and op is the name of the operator to be overloaded.
    Operator overloading is done in two ways 1) using simple member function 2) using friend function.
    For unary operator overloading using simple member function it requires nothing as a parameter to function.
    For unary operator overloading using friend function it requires one object of concern class as a parameter to function.
    Below is the program which shows overloading of unary ++ operator using friend function.

    Please add iostream.h and conio,h header files before declaration of class.
    class sample
    int inch;
    sample(int x)
    cout<<"\nValue of entered inches is "< }
    friend void operator++(sample t)
    cout<<"\nValue of inches after applying ++ operator is "< }
    void main(void)
    int in;
    cout<<"\nEnter the required inch value\n";
    class sample ob(in);

    In above program when the statement ob++ appears, it tells the compiler the equivalent statement as
    operator ++(ob). So that call to operator ++ function get executed. As we know when object of class is passed as parameter to the function then inside the function members of that object are accessed using dot(.) operator. So that inside the operator ++ function member inch is accessed as t.inch.

    Hope you will be satisfied by this answer.

  • Sign In to post your comments