Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab Objectives This lab was designed to reinforce programming concepts from Chap

ID: 3808101 • Letter: L

Question

Lab Objectives This lab was designed to reinforce programming concepts from Chapter 9 of C++ How To Program, Seventh Edi- tion. In this lab, you will practice: Creating new data types by writing class definitions. Defining member functions of programmer-defined classes. Instantiating objects from programmer-defined classes Calling member functions of programmer-defined classes. The follow-up questions and activities will also give you practice: Initializing programmer-defined class data members with class constructors Description of the Problem Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form real Part imaginaryPart where i is 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 a) Adding two complex numbers: The real parts are added together and the imaginary parts are added to- gether.

Explanation / Answer

1) Complex.h

#ifndef COMPLEX_H
#define COMPLEX_H
#include<iostream>

using namespace std;

class Complex
{
private:
double realPart,imaginaryPart;

public:
Complex();
Complex(double real, double imaginary);
Complex add(const Complex &right);
Complex subtract(const Complex &right);
void printComplex();
void setComplexNumber(double rp, double ip);
};

#endif // COMPLEX_H

2) Complex.cpp

#include "Complex.h"

//Default Constructor
Complex::Complex()
{
realPart = 0;
imaginaryPart = 0;
}
//Initializing the members of the class
Complex::Complex(double real, double imaginary)
{
setComplexNumber(real, imaginary);
}
//adding two complex number
Complex Complex::add(const Complex &right)
{

return Complex(this->realPart + right.realPart,this->imaginaryPart + right.imaginaryPart);
}
//substract two complex number
Complex Complex::subtract(const Complex &right)
{
return Complex(this->realPart - right.realPart, this->imaginaryPart - right.imaginaryPart);
}
//set complex numbers
void Complex::setComplexNumber(double rp, double ip)
{
realPart = rp;
imaginaryPart = ip;
}
void Complex::printComplex()
{
cout << "("<<realPart<<", "<<imaginaryPart<<")";
}

3)main.cpp

#include "Complex.h"

int main()
{
Complex a(1,7);
Complex b(9,2);
Complex c;
a.printComplex();
cout<<" + ";
b.printComplex();
cout<<" = ";
c = a.add(b);
c.printComplex();

cout<<" ";

a.setComplexNumber(10, 1);
b.setComplexNumber(11, 5);
a.printComplex();
cout<<" - ";
b.printComplex();
cout<<" = ";
c = a.subtract(b);
c.printComplex();
cout<<endl;
return 0;
}

Please refer below output for reference

(1, 7) + (9, 2) = (10, 9)
(10, 1) - (11, 5) = (-1, -4)

Process returned 0 (0x0) execution time : 0.011 s
Press any key to continue.