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

    C++ program using overloading of unary ++ operator

    Want a solution to a programming problem? Searching for detailed answer for this C++ program? Find the c++ solution to your problem from our experts here.

    Following is the question asked in MSBTE(Maharashtra State Board of Technical Education, Mumbai) Diploma in Computer Engineering question paper for Summer 2018 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 using C++ for overloading of ++ unary operator for inch to feet conversion.
    12 inchs = 1 feet. [ 4 Marks ]
    Expect quality response from our experts.
  • Answers

    1 Answers found.
  • Operator overloading is a unique feature provided by Object Oriented Programming. As class is user defined data type but it behaves like built in data type. In the same regard operator overloading allows user to apply in built operators on objects with a great ease. When we apply operator on object then it is expected that what operation/s should be be done with object data. This task is performed by one function which is generally called as operator op () and here in this example it is named as operator ++(). This operator function is a special function written by programmer to decide what action to be taken on object data when concern operator is applied to object.
    Operator overloading is performed in two ways
    1) using simple member function
    2) using friend function
    In the given code operator overloading is done using simple member function. Also parameterised constructor is used to fetch the value for inch variable from user.

    Below is the program code for required problem.
    Please include iostream.h and conio.h header files.

    class convert
    float inch, feet;
    convert(float in)
    operator ++()
    feet = inch /12;
    cout<<"\nvalue in inch is "< cout<<"\nconverted value in feets is "< }
    void main(void)
    float x;
    cout<<"\nEnter value in inch\n";
    class convert s(x);

  • Sign In to post your comments