Question

In: Computer Science

Complex numbers using overload constructor and private parameters in C++ I don't know why my code...

Complex numbers using overload constructor and private parameters in C++

I don't know why my code does not compile......

please DO NOT CHANGE THE MAIN....

complexDriver.cpp

#include <iostream>
#include "complex.h"

using namespace std;
int main( ) {
  
// Ex) complex(4.0, 3.0) means 4.0+3.0i.
complex c1, c2( 1.2, 4.9 ), c3( 2.2, 1.0 ), c4( -7.0, 9.6 ), c5(8.1, -4.3),
c6(0.0, -7.1), c7(6.4), c8(0.0, 1.0), c9(0.0, 4.1), c10(0.0, -1.0), c11;

cout << "c1 = " << c1 << endl;
cout << "c2 = " << c2 << endl;
cout << "c3 = " << c3 << endl;
cout << "c4 = " << c4 << endl;
cout << "c5 = " << c5 << endl;
cout << "c6 = " << c6 << endl;
cout << "c7 = " << c7 << endl;
cout << "c8 = " << c8 << endl;
cout << "c9 = " << c9 << endl;
cout << "c10 = " << c10 << endl;
cout << "c11 = " << c11 << endl;

}

complex.h

#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using std::istream;
using std::ostream;

class complex{
// Stream I/O
friend ostream& operator<<(ostream &, const complex &);
friend istream& operator>>(istream&, complex&);

public:
  
double getReal() const; // getter
void setReal(double x); // setter
double getImaginary() const;
void setImaginary(double y);

// Ex) complex(4.0, 3.0) means 4.0+3.0i.
complex(double x= 0.0, double y= 0.0); // default constructor


private:
double Real;
double Imaginary;

};

#endif // COMPLEX_H

complex.cpp

#include <iostream>
#include "complex.h"

using std::cout;
using std::endl;

double complex::getReal() const{ // getter
return Real;
}
void complex::setReal(double x){ // setter
Real = x;
}
double complex::getImaginary() const{
return Imaginary;
}
void complex::setImaginary(double y){
Imaginary = y;
}

// X= real part, Y = imaginary part. real number = X+Yi
// Ex) complex(4.0, 3.0) means 4.0+3.0i.
complex::complex(double x, double y){ // default constructor
setReal(x);
setImaginary(y);
}

// Stream I/O
ostream& operator<<(ostream &out, const complex &c){
  
out<< c.x<<"+"<< c.y<< "i";
return out;
}

Solutions

Expert Solution

complexDriver.cpp

#include <iostream>
#include "complex.h"
#include "complex.cpp"   /// CHANGEE ---- CHANGE --- CHANGE

using namespace std;
int main( ) {

// Ex) complex(4.0, 3.0) means 4.0+3.0i.
  complex c1, c2( 1.2, 4.9 ), c3( 2.2, 1.0 ), c4( -7.0, 9.6 ), c5(8.1, -4.3),
          c6(0.0, -7.1), c7(6.4), c8(0.0, 1.0), c9(0.0, 4.1), c10(0.0, -1.0), c11;

  cout << "c1 = " << c1 << endl;
  cout << "c2 = " << c2 << endl;
  cout << "c3 = " << c3 << endl;
  cout << "c4 = " << c4 << endl;
  cout << "c5 = " << c5 << endl;
  cout << "c6 = " << c6 << endl;
  cout << "c7 = " << c7 << endl;
  cout << "c8 = " << c8 << endl;
  cout << "c9 = " << c9 << endl;
  cout << "c10 = " << c10 << endl;
  cout << "c11 = " << c11 << endl;

}

complex.h

#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using std::istream;
using std::ostream;

class complex {
// Stream I/O
        friend ostream& operator<<(ostream &, const complex &);
        friend istream& operator>>(istream&, complex&);

public:

        double getReal() const; // getter
        void setReal(double x); // setter
        double getImaginary() const;
        void setImaginary(double y);

// Ex) complex(4.0, 3.0) means 4.0+3.0i.
        complex(double x = 0.0, double y = 0.0); // default constructor


private:
        double Real;
        double Imaginary;

};

#endif // COMPLEX_H

complex.cpp

#include <iostream>
#include "complex.h"

using std::cout;
using std::endl;

double complex::getReal() const { // getter
        return Real;
}
void complex::setReal(double x) { // setter
        Real = x;
}
double complex::getImaginary() const {
        return Imaginary;
}
void complex::setImaginary(double y) {
        Imaginary = y;
}

// X= real part, Y = imaginary part. real number = X+Yi
// Ex) complex(4.0, 3.0) means 4.0+3.0i.
complex::complex(double x, double y) { // default constructor
        setReal(x);
        setImaginary(y);
}

// Stream I/O
ostream& operator<<(ostream &out, const complex &c) {

        out << c.Real << "+" << c.Imaginary << "i";   //CHANGE --- CHANGE ---- CHANGE
        return out;
}

OUTPUT:

PROBLEMS IN YOUR CODE:

1.) You did not include complex.cpp in your complexdriver file

2.) In operator overloading function of the complex class in complex.cpp file

COMBINED FILE

#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using std::istream;
using std::ostream;
using namespace std;

class complex {
// Stream I/O
        friend ostream& operator<<(ostream &, const complex &);
        friend istream& operator>>(istream&, complex&);

public:

        double getReal() const; // getter
        void setReal(double x); // setter
        double getImaginary() const;
        void setImaginary(double y);

// Ex) complex(4.0, 3.0) means 4.0+3.0i.
        complex(double x = 0.0, double y = 0.0); // default constructor


private:
        double Real;
        double Imaginary;

};

#endif // COMPLEX_H


double complex::getReal() const { // getter
        return Real;
}
void complex::setReal(double x) { // setter
        Real = x;
}
double complex::getImaginary() const {
        return Imaginary;
}
void complex::setImaginary(double y) {
        Imaginary = y;
}

// X= real part, Y = imaginary part. real number = X+Yi
// Ex) complex(4.0, 3.0) means 4.0+3.0i.
complex::complex(double x, double y) { // default constructor
        setReal(x);
        setImaginary(y);
}

// Stream I/O
ostream& operator<<(ostream &out, const complex &c) {

        out << c.Real << "+" << c.Imaginary << "i";   //CHANGE --- CHANGE ---- CHANGE
        return out;
}

/// CHANGEE ---- CHANGE --- CHANGE

int main( ) {

// Ex) complex(4.0, 3.0) means 4.0+3.0i.
        complex c1, c2( 1.2, 4.9 ), c3( 2.2, 1.0 ), c4( -7.0, 9.6 ), c5(8.1, -4.3),
                c6(0.0, -7.1), c7(6.4), c8(0.0, 1.0), c9(0.0, 4.1), c10(0.0, -1.0), c11;

        cout << "c1 = " << c1 << endl;
        cout << "c2 = " << c2 << endl;
        cout << "c3 = " << c3 << endl;
        cout << "c4 = " << c4 << endl;
        cout << "c5 = " << c5 << endl;
        cout << "c6 = " << c6 << endl;
        cout << "c7 = " << c7 << endl;
        cout << "c8 = " << c8 << endl;
        cout << "c9 = " << c9 << endl;
        cout << "c10 = " << c10 << endl;
        cout << "c11 = " << c11 << endl;

}



Related Solutions

I don't know why my java code is not running this error code pops up -->...
I don't know why my java code is not running this error code pops up --> Error: Could not find or load main class DieRoll Caused by: java.lang.ClassNotFoundException: DieRoll. Code below:    import java.util.Random;    import java.util.Scanner;    public class Assignment {    public static void combineStrings() {    String school = "Harvard";    String city = "Boston, MA";    int stringLen;       String upper, changeChar, combine;       stringLen = school.length();    System.out.println(school+" contains "+stringLen+" characters." );   ...
PHP Language I have XAMPP and I don't know how to display my PHP code. For...
PHP Language I have XAMPP and I don't know how to display my PHP code. For instance, when I create a basic HTML webpage I just open the code into a web browser and it shows me a basic web page. When I try to show a PHP calculation it doesn't show. What are the steps in displaying my PHP doc?
I don't understand why this method will not return true, even when my parameters for boolean...
I don't understand why this method will not return true, even when my parameters for boolean results are met, can anyone help shed some light on my mistake? import java.util.*; public class PasswordChecker{    public static void main (String[]args){ Scanner scan = new Scanner(System.in); System.out.println("Please enter a password that is 8 characters in length."); System.out.println("The password must have at least 3 uppercase letters,"); System.out.println("3 numeric digits, as well as 2 lowercase letters,"); System.out.println("and contain no special characters."); System.out.println(); System.out.println("Enter a...
I don't know how to build my last code for TestPairOfDice...below the question is in bold....
I don't know how to build my last code for TestPairOfDice...below the question is in bold. I'm including my code for the previous problems which are all needed for number 6 1. Implement a method named surface that accepts 3 integer parameters named width, length, and height as user input. It will return the total surface area (6 sides) of the rectangular box it represents. The formula for Surface Area is 2(length * width) + 2(length * height) + 2(height...
IMPORTANT: I know the answer is "C". However, I don't know why. Could you please explain...
IMPORTANT: I know the answer is "C". However, I don't know why. Could you please explain why? Thank you A linear total cost curve that passes through the origin implies that a.         average cost is constant and marginal cost is variable. b.         average cost is variable and marginal cost is constant.             c.         average and marginal costs are constant and equal.             d.         you need more information to answer question.
//Trying to get this code with JavaScript. I could partition the subarrays, but I don't know...
//Trying to get this code with JavaScript. I could partition the subarrays, but I don't know how to check for unique elements Given an array of integers check if it is possible to partition the array into some number of subsequences of length k each, such that: Each element in the array occurs in exactly one subsequence For each subsequence, all numbers are distinct. Elements in the array having the same value must be in different subsequences If it is...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using namespace std; const int CWIDTH = 26; int main() {    int choice;    double convertFoC, converCtoF;    double starting, endvalue, incrementvalue;    const int CWIDTH = 13;    do {       cin >> choice;    switch (choice)    {        cin >> starting;    if (starting == 28){       cout << "Invalid range. Try again.";    }    while (!(cin >> starting)){       string  garbage;       cin.clear();       getline(cin, garbage);       cout << "Invalid data Type, must be a number....
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
I'm getting an error message with this code and I don't know how to fix it...
I'm getting an error message with this code and I don't know how to fix it The ones highlighted give me error message both having to deal Scanner input string being converted to an int. I tried changing the input variable to inputText because the user will input a number and not a character or any words. So what can I do to deal with this import java.util.Scanner; public class Project4 { /** * @param args the command line arguments...
I want to know the details interpretation of this code for my interview preperation on this...
I want to know the details interpretation of this code for my interview preperation on this code. This is A* algorithm in c++ #include<bits/stdc++.h> using namespace std; #define DISCONNECTED -1 int num_of_node,num_of_edge,graph[30][30],pathCost[30]; void init_Heuristic(); struct Node{     int from,to;     int cost; }; struct CompareNode{     bool operator()(Node& n1, Node& n2){         if(n1.cost > n2.cost)return true;         return false;     } }; map<int,int>Heuristic; priority_queue<Node, vector<Node>, CompareNode> PQ; vector<Node>path; void AStarSearch(int source,int destination){     init_Heuristic();     for(int i = 1; i...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT