Question

Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Operator...

Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file)

Operator Overloading – Chapter 14

  1. Design a class Complex for representing complex numbers and the write overloaded operators for adding, subtracting, multiplying and dividing 2 complex numbers. Also create a function that will print a complex number on the screen. Provide appropriate constructors and destructor.

Over load the operators + (add), - (minus), * (multiply), = , != (comparison), and provide the operators << and >> to printout and read in respectively. See section 14.9 of Liang C++ textbook for help with some operators.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<stdlib.h>

using namespace std;
//body of complex class
class complex
{
int real;
int img;
public:
complex() //default constructor initialize the members to 0
{
real=0; img=0;
}
//parameterized constructor will initialize the members by users given values
complex(int r,int i)
{
real=r;
img=i;
}
//>> operator overloading to input
friend istream & operator >>(istream &p,complex &k)
{
cout<<endl<<"Enter the real and imaginary part of a complex number";
cin>>k.real>>k.img;
return p;
}
//<< operator overloading for output
friend ostream & operator<<(ostream &p,complex &k)
{
p<<k.real <<" +i "<<k.img;
return p;
}
//+ operator overloading to add two complex numbers
complex operator +(complex c);
//- operator overloading for subtraction
complex operator -(complex c);
//* operator overloading for multiplication
complex operator *(complex c);
// '/' operator overloading for division
complex operator /(complex c);
// != operator overloading
bool operator !=(complex c);

// = operator overloading
void operator =(complex c);

};

//!= operator overloading. It will return true if both the omplex numbers are unequal otherwise false.
bool complex:: operator!=(complex c)
{
if(c.real == real && c.img == img)
return false;
else
return true;
}

//addition operator overloading
void complex:: operator=(complex c)
{
real=c.real;
img=c.img;
}

//addition operator overloading
complex complex:: operator+(complex c)
{
complex temp;
temp.real=real+c.real;
temp.img=img+c.img;
return temp;//return the object
}
//- operator overloading
complex complex:: operator-(complex c)
{
complex temp;
temp.real=real-c.real;
temp.img=img-c.img;
return temp; //return the object
}
//* operator overloading
complex complex:: operator*(complex c)
{
complex temp;
temp.real=(real*c.real)-(img*c.img);
temp.img=(real*c.img)+(img*c.real);
return temp; //return the object
}
// ' /' operator overloading
complex complex:: operator/(complex c)
{
float k,a,b;
complex temp;
//logic for division
k= (c.real*c.real)+(c.img*c.img);
a=float ((real*c.real)+(img*c.img))/k;
b=float ((real*c.img)-(img*c.real))/k;
//assign the final values to temp object
temp.real=a;
temp.img=b;
return temp; //return the object
}
//driver program
int main()
{
complex c1(0,0),c2(0,0),c;
cin>>c1; //input first complex number
cin>>c2; //input second complex number
cout<<endl<<"ADDITION"<<endl;
c=c1+c2;//compute addition
cout<<c; //display addition
cout<<endl<<"SUBTRACTION"<<endl;
c=c1-c2;//compute subtraction
cout<<c;//display the result
cout<<endl<<"MULTIPLICATION"<<endl;
c=c1*c2; //compute multiplication
cout<<c;//display the result
cout<<endl<<"DIVISION"<<endl;
c=c1/c2; //compuet the division
cout<<c; //dsiplay the result
//compare two complex number for equality
if(c1!=c2)
cout<<endl<<"Both the complex numbers are un equal";
else
cout<<endl<<"Both the complex numbers are equal";
//assignment operator overloading
c1=c2;
cout<<endl<<"First complex number : ";
cout<<c1;
cout<<endl<<"Second complex number : ";
cout<<c2;

//compare two complex number for equality
if(c1!=c2)
cout<<endl<<"Both the complex numbers are un equal";
else
cout<<endl<<"Both the complex numbers are equal";

}

output

CAUsers ROZYnkRISHNAIDesktop COMPLEXexe Enter the real and imaginary part of a complex number12 4 Enter the real and imaginar

Add a comment
Know the answer?
Add Answer to:
Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Operator...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 file...

    Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Operator Overloading – Chapter 14 Design a class Complex for representing complex numbers and the write overloaded operators for adding, subtracting, multiplying and dividing 2 complex numbers. Also create a function that will print a complex number on the screen. Provide appropriate constructors...

  • Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Template...

    Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Template Classes – Chapter 12 Write an example of a template function that can swap 2 generic elements. Create a C++ class vector using the class diagram shown in Figure 12.2. in Liangs textbook. Do not use the C++ provided header file <vector>...

  • Design a class that contains: we have to make 3 files header file , one .cpp...

    Design a class that contains: we have to make 3 files header file , one .cpp file for function and one more main cpp file 9.3 (The Account class) Design a class named Account that contains: An int data field named id for the account. A double data field named balance for the account. A double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and...

  • CSC 252 Week 4 HW Provide code and full projects neatly and in proper form and in the correct hea...

    CSC 252 Week 4 HW Provide code and full projects neatly and in proper form and in the correct header and cpp files. Code must compile, link and run for any credit.This is Microsoft visual Studio C++ and needs to be done correctly and neatly and I need to be able to copy and paste code to visual studio and this is for a grade so if you don't know the answer don't answer please and thank you. Exception Handling...

  • CSC 252 Week 4 HW Provide code and full projects neatly and in proper form and...

    CSC 252 Week 4 HW Provide code and full projects neatly and in proper form and in the correct header and cpp files. Code must compile, link and run for any credit.This is Microsoft visual Studio C++ and needs to be done correctly and neatly and I need to be able to copy and paste code to visual studio and this is for a grade so if you don't know the answer don't answer please and thank you. Exception Handling...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT