Question

C++ Complex Class!

Create a class called Complex for performing arith

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

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

class Complex
{
public:
Complex();
Complex(float real, float imaginary);
Complex(const Complex &c); // Copy constructor


const Complex operator+(const Complex &c) const;
const Complex operator-(const Complex &c) const;
const Complex operator*(const Complex &c) const;

/* Friends */
friend std::ostream& operator<<(std::ostream &os, const Complex &c);

virtual ~Complex();
protected:
private:
/* Member variables */
float real, imaginary;
};

#endif // COMPLEX_H

#include "Complex.h"

Complex::Complex()
{
real = imaginary = 0;

}

Complex::Complex(float real, float imaginary = 0)
{
this->real = real;
this->imaginary = imaginary;

}

Complex::Complex(const Complex &c)
{
real = c.real;
imaginary = c.imaginary;

}

const Complex Complex::operator+(const Complex &c) const
{
float new_real, new_imaginary;
new_real = real + c.real;
new_imaginary = imaginary + c.imaginary;

Complex sum(new_real, new_imaginary);
return sum;
}

const Complex Complex::operator-(const Complex &c) const
{
// More compact than above, but harder to read
return Complex(real - c.real, imaginary - c.imaginary);
}

const Complex Complex::operator*(const Complex &c) const
{
// Again, compact but hard to read.
return Complex(real * c.real - imaginary * c.imaginary, imaginary * c.real + real * c.imaginary);
}

Complex::~Complex()
{
//dtor
}
std::ostream& operator<<(std::ostream &os, const Complex &c)
{
os << c.real << " + " << c.imaginary << "i";
return os;
}

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

using namespace std;

int main()
{
Complex c1;
Complex c2(3, 4);
Complex c3(2, 3);

std::cout << "c1: " << c1 << std::endl;
std::cout << "c2: " << c2 << std::endl;
std::cout << "c3: " << c3 << std::endl;

std::cout << "c1 + c2: " << c1 + c2 << std::endl;
std::cout << "c1 - c3: " << c1 - c3 << std::endl;
std::cout << "c3 * c2: " << c3 * c2 << std::endl;
return 0;
}

OUTPUT:

c2: 3 4i c3: 2 3i c1+c2: 3 + 4i c1 - c3: -2+ -3i Process returned 0 〈0x0〉 execution time : 0.023 s Press any key to continue

Add a comment
Know the answer?
Add Answer to:
C++ Complex Class! Create a class called Complex for performing arithmetic with complex numbers. Write a...
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
  • C++ Create a class called Complex for performing arithmetic with complex numbers. Write a program to...

    C++ Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + j imaginaryPart Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that perform the following tasks: Adding two Complex...

  • Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form:...

    Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form: realPart + imaginaryPart * i where i is √-1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should contain default values of (1,1) i.e. 1 for the real part and 1 for the imaginary part. Provide public member functions that perform the following...

  • Object Oriented Programming Please write the code in C++ Please answer the question in text form...

    Object Oriented Programming Please write the code in C++ Please answer the question in text form 9.5 (complex Class) Create a class called complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form where i is V-I Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in...

  • (Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integers variables to represent the private data of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case are no initializers are provided and should store the fraction in reduced form. For example, the fraction 3/6 would be...

  • (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to h and would be stored in the object as 1 in the numerator...

  • Create a class for working with complex numbers. Only 2 private float members are needed, the...

    Create a class for working with complex numbers. Only 2 private float members are needed, the real part of the complex number and the imaginary part of the complex number. The following methods should be in your class: a. A default constructor that uses default arguments in case no initializers are included in the main. b. Add two complex numbers and store the sum. c. Subtract two complex numbers and store the difference. d. Multiply two complex numbers and store...

  • C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the r...

    C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the real part of the complex number and   is called the imaginary part of the complex number. The addition of two complex numbers will generate a new complex number. The addition is done by adding the real parts together (the result's real part) and adding the...

  • C++ CLASS FOR DEFINING COMPLEX NUMBERS (READ BELOW) Write a C++ defining a class for complex...

    C++ CLASS FOR DEFINING COMPLEX NUMBERS (READ BELOW) Write a C++ defining a class for complex numbers. A complex number is a number of the form: a + b ∗ i , where, for our purposes, a and b are numbers of type double, and i is a number that represents the quantity √ −1. You should represent a complex number here as two values of type double. You should name the variables real and imaginary. You can call the...

  • C++ // Need to create a class which contains and operates on complex numbers. A complex...

    C++ // Need to create a class which contains and operates on complex numbers. A complex number is defined as “a number that can be expressed in the form a + bi, where a and b are real numbers, and i is imaginary” a is called the real part and b is called the imaginary part. The class MyComplexClass will store the complex number as separate real part and imaginary part values. MyComplexClass Private Member Variables *One double for the...

  • C++...please help follow exact steps output needs to be the exact same Create a class called...

    C++...please help follow exact steps output needs to be the exact same Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the elass the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced...

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