This is a C++ programming question. Please provide the correct, workable code. Use the following three programs to help solve the problem. Provide comments throughout the code.
Problem to solve:
Code 1: Complex.h
#pragma once
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
private:
double real;
double imag;
public:
// initialize the complex number to 0.0
Complex() : real(0.0), imag(0.0) {}
// initialize the complex number at declaration or
new
Complex(double r, double i) : real(r), imag(i)
{}
// this is actually the same as the default copy
constructor provided by the compiler
Complex(const Complex &c) : real(c.real),
imag(c.imag) {}
void setReal(double r) {real = r;}
void setImag(double i) {imag = i;}
void setComplex(const Complex &c) {real = c.real;
imag = c.imag;}
double getReal() const {return real;}
double getImag() const {return imag;}
Complex getComplex() const {return *this;}
Complex add(const Complex &) const;
Complex sub(const Complex &) const;
Complex mul(const Complex &) const;
Complex div(const Complex &) const;
void print() const;
void print(const Complex &) const;
};
#endif
Code 2: Complex.cpp
#include <iostream>
using namespace std;
#include "Complex.h"
Complex Complex::add(const Complex &c) const {
Complex result;
result.real = real + c.real;
result.imag = imag + c.imag;
return result;
}
Complex Complex::sub(const Complex &c) const {
Complex result;
result.real = real - c.real;
result.imag = imag - c.imag;
return result;
}
Complex Complex::mul(const Complex &c) const {
Complex result;
result.real = real * c.real - imag * c.imag;
result.imag = imag * c.real + real * c.imag;
return result;
}
Complex Complex::div(const Complex &c) const {
double t = c.real * c.real + c.imag * c.imag;
Complex result;
result.real = (real * c.real + imag * c.imag) /
t;
result.imag = (imag * c.real - real * c.imag) /
t;
return result;
}
void Complex::print() const {
cout << "( " << real << ", "
<< imag << " )\n";
}
void Complex::print(const Complex &c) const {
c.print();
}
Code 3: Application.cpp
#include <iostream>
#include "Complex.h"
using namespace std;
void main() {
Complex a(4.0, 6.0), b(3.0, 5.0), *c = new
Complex(1.0, 1.0);
Complex d(a); d.print();
a.add(b).add(*c).print();
c->print(a);
}
Screenshot of program code:-
//Complex.h
Screenshot of output:-
Program code to copy:-
//Complex.h
#pragma once
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
private:
double real;
double imag;
public:
// initialize the complex number to 0.0
Complex() : real(0.0), imag(0.0) {}
// initialize the complex number at declaration
or new
Complex(double r, double i) : real(r), imag(i)
{}
// this is actually the same as the default copy
constructor provided by the compiler
Complex(const Complex &c) : real(c.real),
imag(c.imag) {}
void setReal(double r) {real = r;}
void setImag(double i) {imag = i;}
void setComplex(const Complex &c) {real =
c.real; imag = c.imag;}
double getReal() const {return real;}
double getImag() const {return imag;}
Complex getComplex() const {return *this;}
//Overloaded functions prototype
Complex operator+(const Complex &)
const;
Complex operator-(const Complex &)
const;
Complex operator*(const Complex &)
const;
Complex operator/(const Complex &)
const;
//Print functions prototype
void print() const;
void print(const Complex &) const;
};
#endif
//Complex.cpp
#include <iostream>
using namespace std;
#include "Complex.h"
//addition(+) operator function overloading
Complex Complex::operator+(const Complex &c) const {
Complex result;
result.real = real + c.real;
result.imag = imag + c.imag;
return result;
}
//subtraction(-) operator function overloading
Complex Complex::operator-(const Complex &c) const {
Complex result;
result.real = real - c.real;
result.imag = imag - c.imag;
return result;
}
//multiplication(*) operator function overloading
Complex Complex::operator*(const Complex &c) const {
Complex result;
result.real = real * c.real - imag * c.imag;
result.imag = imag * c.real + real * c.imag;
return result;
}
//division(/) operator function overloading
Complex Complex::operator/(const Complex &c) const {
double t = c.real * c.real + c.imag * c.imag;
Complex result;
result.real = (real * c.real + imag * c.imag) / t;
result.imag = (imag * c.real - real * c.imag) / t;
return result;
}
//Functions to print the result
void Complex::print() const {
cout << "( " << real << ", " << imag
<< " )\n";
}
void Complex::print(const Complex &c) const {
c.print();
}
//Application.cpp
#include <iostream>
#include "Complex.h"
using namespace std;
int main() {
Complex a(1.0, 2.0), b(3.0, 4.0), c;
//testing copy constructor
Complex d(a);
cout << "Copy constructor result: ";
d.print();
//Testing overloaded + operator
c = a + b;
cout << "Complex number addition result: ";
c.print();
//Testing overloaded - operator
c = a - b;
cout << "Complex number subtraction result: ";
c.print();
//Testing overloaded * operator
c = a * b;
cout << "Complex number multiplication result: ";
c.print();
//Testing overloaded / operator
c = a / b;
cout << "Complex number division result: ";
c.print();
return 0;
}
This is a C++ programming question. Please provide the correct, workable code. Use the following three...
Please implement the following problem in basic C++ code and include detailed comments so that I am able to understand the processes for the solution. Thanks in advance. // personType.h #include <string> using namespace std; class personType { public: virtual void print() const; void setName(string first, string last); string getFirstName() const; string getLastName() const; personType(string first = "", string last = ""); protected: string firstName; string lastName; }; // personTypeImp.cpp #include <iostream> #include <string> #include "personType.h" using namespace std; void...
what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...
Given two complex numbers, find the sum of the complex numbers using operator overloading. Write an operator overloading function ProblemSolution operator + (ProblemSolution const &P) which adds two ProblemSolution objects and returns a new ProblemSolution object. Input 12 -10 -34 38 where, Each row is a complex number. First element is real part and the second element is imaginary part of a complex number. Output -22 28 Two complex numbers are 12-10i and -34+38i. Sum of complex numbers are =...
I cannot get the C++ code below to compile correctly. Any assistance would be greatly appreciated! ***main.cpp driver file*** #include #include #include "vector.h" using namespace std; int main() { vectorInfo v1(3, -1); vectorInfo v2(2, 3); cout << "v1 : "; v1.print(); cout << "v2 : "; v2.print(); cout << "v1 magnitude : " << v1.magnitude() << endl; cout << "v1 direction : " << v1.direction() << " radians" << endl; cout...
C++: questions are in fish.h. Write the code in fish.cpp and main.cpp #ifndef FISH_H #define FISH_H #include <vector> class Fish { public: // (1 point) // Write code to initialize edible to is_edible, age to 0, size to 1. Fish (bool is_edible); // (1 point) // Print the vital stats (size and age) of the fish. void Print(); // (1 point) // If fish is at least the age of reproduce_age, reproduce with probability // of reproduce_probability. If reproduce, return...
Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public: static string weekDays[7]; void print() const; string nextDay() const; string prevDay() const; void addDay(int nDays); void setDay(string d); string getDay() const; dayType(); dayType(string d); private: string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...
2. Enter, compile, and run Program 11.1. he same name as the ave a return type nction as a member Chapter 11 Program 11.1 of this function the parameters include <iostream ing namespace std. *onging to the declaration section class Complex private: 1 double realPart; I notice the colon after the keyword pri eters, real ssigns the he func- ouble imaginary Part; maginaryPart. // function prototypes data // data member the key as also public: 11 again, notice the colon...
2 The following code has some errors. Please point out and correct them (there might be more than 1 errors). Note: the use of const data member increment and member function print. // This code has some errors. #include using namespace std; class Increment { public: Increment( int c = 0, int i = 0); void addIncrement() const { count -= increment }; void print() const; private: int count; const int increment; }; Increment::Increment(int c, int i) { cout =...
Consider the following C++code snippet and what is the output of this program? # include<iostream> using namespace std; void arraySome (int[), int, int); int main () const int arraysize = 10; int a[arraysize]-1,2,3,4,5, 6,7,8,9,10 cout << "The values in the array are:" << endl; arraySome (a, 0, arraySize) cout<< endl; system ("pause") return 0; void arraySome (int b[], int current, int size) if (current< size) arraySome (b, current+1, size); cout << b[current] <<""; a) Print the array values b) Double...
This is a simple C++ class using inheritance, I have trouble getting the program to work. I would also like to add ENUM function to the class TeachingAssistant(Derived class) which is a subclass of Student(Derived Class) which is also a subclass of CourseMember(Base Class). The TeachingAssistant class uses an enum (a user-defined data type) to keep track of the specific role the TA has: enum ta_role {LAB_ASSISTANT, LECTURE_ASSISTANT, BOTH}; You may assume for initialization purposes that the default role is...