Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Design a class for complex number and write a program in C++ to overload binary operator + for adding two complex numbers.

QUESTION: Design a Class for complex number and write a program in C++ to overload binary
operator + for adding two complex numbers.

ANSWER:

#include class Complex { private: int real, imag; public: Complex(int r = 0, int i =0) { real = r; imag = i; } // This is automatically called when '+' is used with // between two Complex objects Complex operator + (Complex const &obj) { Complex res; res.real = real + obj.real; res.imag = imag + obj.imag; return res; } void print() { cout

RESULT:

12+ i9


This post first appeared on LOVE TO LEARN AND FUN, please read the originial post: here

Share the post

Design a class for complex number and write a program in C++ to overload binary operator + for adding two complex numbers.

×

Subscribe to Love To Learn And Fun

Get updates delivered right to your inbox!

Thank you for your subscription

×