Skip to content

Instantly share code, notes, and snippets.

@danvas
Created November 22, 2011 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danvas/1384720 to your computer and use it in GitHub Desktop.
Save danvas/1384720 to your computer and use it in GitHub Desktop.
Member-function definitions for class Complex.
// Assignment 8: Complex.cpp
// Member-function definitions for class Complex.
// Author: Daniel Vasquez
// Date: Nov. 24th, 2011
#include "Complex.h"
// Default constructor
Complex::Complex( )
{
realPart = 1.0;
imaginaryPart = 0.0;
}
// Constructor with 2 parameters
Complex::Complex(double real = 1.0, double imaginary = 0.0)
{
setReal( real );
setImaginary( imaginary );
}
// Displays the real and imaginary numbers
// in the format ( real , imaginary )
void Complex::display(void)
{
cout << "( " << fixed << setprecision(4)
<< realPart << " , "
<< imaginaryPart << " )";
}
// Returns the value for the real number
double Complex::getReal(void)
{
return realPart;
}
// Assigns real to realPart
void Complex::setReal( double real )
{
realPart = real;
}
// Returns the value for the imaginary number
double Complex::getImaginary(void)
{
return imaginaryPart;
}
// Assigns imaginary to imaginaryPart
void Complex::setImaginary( double imaginary )
{
imaginaryPart = imaginary;
}
// Implementation for plusEq() memer function
Complex & Complex::plusEq( Complex & op ) // Adds the data members of one object
{ // to those of a second object.
realPart += op.realPart; // Similar operation as the += operator.
imaginaryPart += op.imaginaryPart;
return (*this);
}
// Implementation for minusEq() memer function
Complex & Complex::minusEq( Complex & op ) // Subtracts the data members of one object
{ // from those of the second object.
realPart -= op.realPart; // Similar operation as the -= operator.
imaginaryPart -= op.imaginaryPart;
return (*this);
}
// Assignment 8: Complex.h
// Declaration of class Complex.
// Member functions are defined in Complex.cpp.
// Author: Daniel Vasquez
// Date: Nov. 24th, 2011
#include <iostream>
#include <iomanip>
using namespace std;
class Complex
{
public:
Complex(); // Default constructor
Complex(const double, const double); // Constructor with 2 parameters
void display(void); // Displays the real and imaginary numbers
// in the format ( real , imaginary )
double getReal(void); // Returns the value for the real number
void setReal( double ); // Assigns passed value to the real number
double getImaginary( void ); // Returns the value for the imaginary number
void setImaginary( double ); // Assigns passed value to the imaginary number
Complex & plusEq( Complex & op ); // Adds the data members of one object
// to those of a second object.
// Similar operation as the += operator.
Complex & minusEq( Complex & op ); // Subtracts the data members of one object
// from those of a second object.
// Similar operation as the -= operator.
private:
// Declarations of real and imaginary numbers
double realPart;
double imaginaryPart;
};
// Assign 8 Main Program - used to "test drive" the Complex class
// File Name: MainProg.cpp
// Author: Bob Langelaan
// Date: March 24th, 2007
#include <iostream>
using std::cout;
using std::endl;
#include "Complex.h" // include definition of class Complex from Complex.h
int main()
{
// Test constructor -NOTE: your Complex class should have only
// one constructor. Your constructor should have default arguments
// for the real and imaginary parts of the complex object.
Complex A; // create a Complex object using default arguments
cout << "The default object is: ";
A.display();
Complex B(-2.45, 1.0); // create a Complex object supplying values to the ctor
cout << "\n\nThe non-default object is: ";
B.display();
Complex C(25.777,-35.668); // create another Complex object supplying values to the ctor
cout << "\n\nThe second non-default object is: ";
C.display();
// Test plusEq()
cout << "\n\n- Test plusEq()";
A = Complex(-25.44,-3.543); //Assign new values to Complex objects
B = Complex(30.3,-34.876);
// Output "before" values
cout << "\nA = ";
A.display();
cout << "\nB = ";
B.display();
// NOTE: Equivalent to: C = A += B;
C = A.plusEq(B);
// Output "after" values
cout << "\n\nA = ";
A.display();
cout << "\nB = ";
B.display();
cout << "\nC = ";
C.display();
// Test minusEq()
cout << "\n\n- Test minusEq()";
A = Complex(4.65,3.789); //Assign new values to Complex objects
B = Complex(6.78,9.222);
// Output "before" values
cout << "\nA = ";
A.display();
cout << "\nB = ";
B.display();
// NOTE: Equivalent to: C = A -= B;
C = A.minusEq(B);
// Output "after" values
cout << "\n\nA = ";
A.display();
cout << "\nB = ";
B.display();
cout << "\nC = ";
C.display();
cout << '\n' << endl;
return 0;
} // end main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment