Question

In: Computer Science

This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of...

This class should include .cpp file, .h file and driver.cpp (using the language c++)!

Overview of complex Class

The complex class presents the complex number X+Yi, where X and Y are real numbers and i^2 is -1. Typically, X is called a real part and Y is an imaginary part of the complex number. For instance, complex(4.0, 3.0) means 4.0+3.0i. The complex class you will design should have the following features.

Constructor

Only one constructor with default value for Real = 0.0, Imaginary = 0.0; But it should construct the complex numbers with, 1) no argument, 2) 1 argument, 3) 2 arguments. Please refer the rat2.h file and rat2.cpp.

Data members

The complex has two members which should be Real values. For example, x+yi, Real = x, Imaginary = y.

Member functions : Provide the following four member functions:

getReal

returns the real part of the complex.

getImaginary

returns the imaginary part of this complex number.

setReal

sets the real part of the complex.

setImaginary

sets the imaginary part of this complex number.

Math operators

The class must implement binary arithmetic operations such as addition, subtraction, multiplication, and division. You should be able to use operators (+, -, *, /).

Addtion (+)

Add two objects. For example, (a+bi) + (c+di) = (a+c) + (b+d)i

Subtraction(-)

Perform complex minus operation

Multiplication(*)

Multiply the left-hand-side object by the right-hand-side and return complex object.

Division(/)

Divide the left-hand-side object by the right-hand-side object. You have to know the division of complex numbers. Divide by zero error should be handled in this method. That is, print an error message, and return the original one. Since it is an exception error, should not affect the following tasks. (Same for /= method)

Conjugate

The complex conjugate of the complex number z = x + yi is defined to be x − yi. This function returns the conjugate of the input complex.

Comparison

The class must implement ==. !=.

Assignment

The class must implement +=, -=, *= and /=. (Divide by zero error should be handled in /= method.)

Stream I/O

The class must implement the << and >> operators:

Input

Take two values as a real and imaginary value.

Output

The format will be: X+Yi, where X and Y are a Real and Imaginary value respectively. Of course, if either X or Y is 0, it should not be displayed. However, if both X and Y are 0, the output should be 0. Also note that if X is 1, it should be printed out as 1, and that if Y is 1, its output should be i. Wrong examples: 1+0 i, 0+2i, 1i, etc..In the case of Y be negative, it should not have “+” between the two. For example, print 2-3i, instead of 2+-3i.

Solutions

Expert Solution

The code is written on Codeblocks. You can create your own main function

In the code written the sample main function is given and all the member function are commented properly.

ouput snippets is attached.code for the program is given below

Please paste it in some c++ support eidtor to see all the code flow for better understanding

#include <iostream>
using namespace std;
class Complex{
private:
float real;//real part of complex no
float img;//imaginary part of complex no
public:
Complex(){ //default constructor
real=0;
img=0;
}
Complex(double r){ //constructor with one argument with sets only the real value
real=r;
}
Complex(double r,double i){ //constructor with tow arguments
real=r;
img=i;
}
double getReal(){ //getter function to get the real value
return real;
}
double getImaginary(){ //getter function to get the imaginary value
return img;
}
void setReal(double r){ //setter function to set the real value;
real=r;
}
void setImaginary(double i){ //setter function to set the imaginary value
img=i;
}
Complex conjugate(Complex c){ //function to calculate the conjugate of the complex no
Complex res;
res.real=c.real;
res.img=c.img;
return res;
}
friend Complex operator +(Complex c1,Complex c2){ //function to calculate the sum on 2 complex no using +
Complex res;
res.real=c1.real+c2.real;
res.img=c1.img+c2.img;
return res;
}
friend Complex operator -(Complex c1, Complex c2) { //function to calculate the difference on 2 complex no using -
Complex res;
res.real = c1.real - c2.real;
res.img = c1.img - c2.img;
return res;
}
friend Complex operator *(Complex c1,Complex c2){ //function to calculate the multiplciation on 2 complex no using *
Complex res;
res.real=c1.real*c2.real-c1.img*c2.img;
res.img=c1.real*c2.img+c2.real*c1.img;
return res;
}
friend Complex operator /(Complex c1,Complex c2){ //function to calculate the division on 2 complex no using /
Complex c3=c2.conjugate(c2);
Complex n=c1*c3;
double d=c2.real*c2.real+c2.img*c2.img;
Complex res;
if(d!=0){
res.real=n.real/d;
res.img=n.img/d;
return res; // result complex no returned if denominator is not zero
}
else{
cout<<"Denominator should not be zero";
}
return c1; //if denominator is zero it will return the numerator
}
friend bool operator ==(Complex c1,Complex c2){ //function to check if complex no are equal using ==
if(c1.real==c2.real&&c1.img==c2.img)return true;
return false;
}
friend bool operator !=(Complex c1,Complex c2){ //function to check if complex no are not equal using !=
if(c1.real==c2.real&&c1.img==c2.img)return false;
return true;
}
friend Complex operator +=(Complex c1,Complex c2){ //function to calculate sum and assign to first complex no
Complex res=c1+c2;
return res;
}
friend Complex operator -=(Complex c1,Complex c2){ //function to calculate difference and assign to first complex no
Complex res=c1-c2;
return res;
}
friend Complex operator *=(Complex c1,Complex c2){ //function to calculate multipllication and assign to first complex no
Complex res=c1*c2;
return res;
}
friend Complex operator /=(Complex c1,Complex c2){ //function to calculate division and assign to first complex no
Complex res=c1/c2;
return res;
}
friend ostream &operator<<( ostream &output, const Complex &c ) { //output function to output complex no using <<
if(c.real==0&&c.img==0){
output<<0;
}
else if(c.real>0){
output<<c.real;
}
if(c.img>0){
if(c.real==0.0&&c.img==1){
output<<"i";
}
else if(c.real==0.0&&c.img!=1){
output<< c.img<<"i";
}
else{
output<<"+"<<c.img<<"i";
}
}
else {
if(c.img==-1){
output<<"-i";
}
else output<<c.img<<"i";
}
return output;
}
friend istream &operator>>( istream &input, Complex &c ) { //input function to take input on complex no using >>
input >>c.real>>c.img;
return input;
}

};
int main(){ // main code
Complex c1,c2(4,5);
cin>>c1;
Complex c3;
c3=c1+c2;
cout<<c3<<endl;
c3=c1*c2;
cout<<c3<<endl;
c3=c1/c2;
cout<<c3<<endl;
if(c1==c2)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
c1+=c2;
cout<<c1;
cout<<endl;
cout<<c1.getReal()<<endl;
cout<<c1.getImaginary();
c1.setReal(4.5);
c1.setImaginary(5.4);
c1=c1.conjugate(c1);
cout<<c1;
return 0;
}


Related Solutions

Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; //...
Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; // PLEASE PUT YOUR FUNCTIONS BELOW THIS LINE // END OF FUNCTIONS void printArray(int array[], int count) {    cout << endl << "--------------------" << endl;    for(int i=1; i<=count; i++)    {        if(i % 3 == 0)        cout << " " << array[i-1] << endl;        else        cout << " " << array[i-1];    }    cout...
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full...
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full specification of the class is: A data member counter of type int. An data member named counterID of type int. A static int data member named nCounters which is initialized to 0. A constructor that takes an int argument and assigns its value to counter. It also adds one to the static variable nCounters and assigns the (new) value of nCounters to counterID. A...
C++ Download Lab10.cpp . In this file, the definition of the class personType has given. Think...
C++ Download Lab10.cpp . In this file, the definition of the class personType has given. Think of the personType as the base class. Lab10.cpp is provided below: #include <iostream> #include <string> using namespace std; // Base class personType class personType { public: void print()const; //Function to output the first name and last name //in the form firstName lastName. void setName(string first, string last); string getFirstName()const; string getLastName()const; personType(string first = "", string last = ""); //Constructor //Sets firstName and lastName...
answer in c++ First – take your Box02.cpp file and rename the file Box03.cpp Did you...
answer in c++ First – take your Box02.cpp file and rename the file Box03.cpp Did you ever wonder how an object gets instantiated (created)? What really happens when you coded Box b1; or Date d1; or Coord c1; I know you have lost sleep over this. So here is the answer………. When an object is created, a constructor is run that creates the data items, gets a copy of the methods, and may or may not initialize the data items....
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
Would you make separated this code by one .h file and two .c file? **********code************* #include...
Would you make separated this code by one .h file and two .c file? **********code************* #include <stdlib.h> #include <stdbool.h> #include <stdio.h> #include<time.h> // Prints out the rules of the game of "craps". void print_game_rule(void) { printf("Rules of the game of CRAPS\n"); printf("--------------------------\n"); printf("A player rolls two dice.Each die has six faces.\n"); printf("These faces contain 1, 2, 3, 4, 5, and 6 spots.\n"); printf("After the dice have come to rest, the sum of the spots\n on the two upward faces is...
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
IN C PROGRAMMING LINUX how to use makefile to include .a static library and .h file...
IN C PROGRAMMING LINUX how to use makefile to include .a static library and .h file from another directory in C? I have a header file myheader.h and a static library libmylib.a file in directory1. In directory2, I'm writing a program which uses them. Suppose I have main.c in directory2 which uses myheader.h and libmylib.a. How do I create a Makefile to compile and link them? LIBB = -L/../directory1/libmylib.a HEADER = -L/../directory1/myheader.h main: main.o gcc $(HEADER) $(LIBB) main.o: main.c gcc...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course,...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course, and a simple program to test it, according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT