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

This problem is intended to be solved in a closed-lab session with a teaching as

ID: 3804860 • Letter: T

Question

This problem is intended to be solved in a closed-lab session with a teaching assistant or instructor present. The problem is divided into six parts: 1. Lab Objectives 2. Description of the Problem 3. Sample Output 4. Program Template (Fig. L 9.3-Fig. L9.5) 5. Problem-Solving Tips 6. Follow-Up Questions and Activities The program template represents a complete working C+ program, with one or more key lines of code replaced with comments. Read the problem description and examine the sample output then study the template code. Using the problem-solving tips as a guide, replace the comments with C++ code. Compile and execute the program. Compare your output with the sample output provided. Then answer the follow-up questions. The source code for the template is available from the Companion Website for C++ How to Program, Seventh Edition at www.pearsonhighered.com/deitel/. Lab Objectives This lab was designed to reinforce programming concepts from Chapter 9 of C++ How To Program, S th Edi- even 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

Explanation / Answer

//Complex.h

#ifndef COMPLEX_H
#define COMPLEX_H

class Complex
{
   private:
       double realPart;
       double imaginaryPart;
   public:
       Complex();
       Complex(double real,double imag);
       Complex add(const Complex &right);
       Complex subtract(const Complex &right);
       void printComplex();
       void setComplexNumber(double rp,double ip);

};
#endif

//Complex.cpp

#include <iostream>
using namespace std;

#include "Complex.h"
Complex::Complex()
{
   realPart=0;
   imaginaryPart=0;
}
Complex::Complex(double r,double i)
{
   realPart=r;
   imaginaryPart=i;
}
Complex Complex::add(const Complex &right)
{
   double r=realPart+right.realPart;
   double i=imaginaryPart+right.imaginaryPart;
   Complex n(r,i);
   return n;
}
Complex Complex::subtract(const Complex &right)
{
   double r=realPart-right.realPart;
   double i=imaginaryPart-right.imaginaryPart;
   Complex n(r,i);
   return n;
}
void Complex::printComplex()
{
   cout<<"("<<realPart<<", "<<imaginaryPart<<")";
}
void Complex::setComplexNumber(double rp,double ip)
{
   realPart=rp;
   imaginaryPart=ip;

}

//ComplexTest.cpp

#include <iostream>
using namespace std;

#include "Complex.h"

int main()
{
   Complex a(1,7),b(9,2),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;
}

Output :

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote