Use the friend function to rewrite the operator overloading of operator +

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter11: Inheritance And Composition
Section: Chapter Questions
Problem 8PE
icon
Related questions
icon
Concept explainers
Question

Use the friend function to rewrite the operator overloading of operator + 

 

#include<iostream>
using namespace std;
class Complex {
private:
int real, imag;
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) {
public:
}
Complex res;
res.real = real + obj.real;
res.imag imag + obj.imag;
return res;
}
void print () { cout << real << " + i" << imag << '\n'; }
};
int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2;
c3.print();
Transcribed Image Text:#include<iostream> using namespace std; class Complex { private: int real, imag; 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) { public: } Complex res; res.real = real + obj.real; res.imag imag + obj.imag; return res; } void print () { cout << real << " + i" << imag << '\n'; } }; int main() { Complex c1(10, 5), c2(2, 4); Complex c3 = c1 + c2; c3.print();
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Operators
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning