CS 202 Lab: Computer Science II Summer 2018 Quiz 5 Name: Section: 1. Write a cla
ID: 3907344 • Letter: C
Question
CS 202 Lab: Computer Science II Summer 2018 Quiz 5 Name: Section: 1. Write a class Complex.Number (declaration and implementation) that has the following private data members: a) an int m_real b) an int m imaginary c) a static int s count (used to track how many objects of type ComplexNumber exist). and the following public member functions: a) a Default ctor that leaves m_real and m imaginary uninitialized. b) A Copy ctor that copies from one ComplexNumber object to the newly instantiated one. c) An assignment operator that assigns from one ComplexNumber object to the calling object d) An operator+ function that sums 2 complex numbers (resulting Complex Number is the sum of real parts and the sum of imaginary parts). c) A static method to get the s_count data that are private (should be named GetCount). and also uses an insertion operatorExplanation / Answer
#include "complexnumber.h"
Complex::Complex(){ }
Complex::Complex(Complex &a){
m_real = a.m_real;
m_imaginary = a.m_imaginary;
}
void Complex::setReal(int a){
m_real = a;
}
void Complex::setImaginary(int a){
m_imaginary = a;
}
Complex Complex::operator = (Complex c2){
Complex temp;
temp.m_real = c2.m_real;
temp.m_imaginary = c2.m_imaginary;
return temp;
}
Complex Complex::operator - (Complex c2){
Complex temp;
temp.m_real = m_real - c2.m_real;
temp.m_imaginary = m_imaginary - c2.m_imaginary;
return temp;
}
Complex Complex::operator + (Complex c2){
Complex temp;
temp.m_real = m_real + c2.m_real;
temp.m_imaginary = m_imaginary + c2.m_imaginary;
return temp;
}
ostream & operator << (ostream &out, const Complex &c){
out << c.m_real;
out << "+i" << c.m_imaginary << endl;
return out;
}