Lab P11 β€” Demonstrate operator overloading.

NBC351 Β· Program 11⏱ 8 min read❓ 5 MCQs

Aim

Aim: Demonstrate operator overloading for complex numbers.

Theory

Demonstrate operator overloading for complex numbers.

C++ Program

#include 
using namespace std;
class Complex {
    int r,i;
public:
    Complex(int a=0,int b=0):r(a),i(b){}
    Complex operator+(Complex c){ return Complex(r+c.r,i+c.i); }
    void show(){ cout<

Expected Output

4+6i

Explanation

  • operator+ defined for Complex.
  • a+b calls operator+.

Viva / Important Questions

Short Questions

  1. What is operator overloading?
  2. Which operators cannot be overloaded?

Long Questions

  1. Explain the program in detail.
  2. Discuss key concepts demonstrated.

MCQ Practice

1. Operator overloading is:

  • Compile-time polymorphism
  • Run-time
  • Inheritance
  • None
Answer: Compile-time polymorphism.

2. Cannot overload:

  • ::
  • .
  • .*
  • sizeof
Answer: ::.

3. Overloaded operator must have:

  • At least one user-defined operand
  • Both built-in
  • None
  • Return type
Answer: At least one user-defined operand.

4. Friend functions can overload:

  • Yes
  • No
  • Only +
  • Only -
Answer: Yes.

5. Improves:

  • Readability
  • Speed
  • Memory
  • None
Answer: Readability.

Quick Revision

  • Overload operators for classes.
  • Cannot overload :: . .* sizeof.