## Transcribed Text

Problem 1:
In the following program, the function add is no longer a member function, but it is a
friend to the class AltMoney and has access to private variable members. In general,
we may want to include the function that does the reading of variables as a member
function or make it a friend so that it reads the dollars and cents and directly assigns
them to the dollars and cents of an object.
#include<iostream>
#include<cstdlib>
using namespace std;
class AltMoney
{
public:
AltMoney();
AltMoney(int d, int c);
friend void add(AltMoney m1, AltMoney m2, AltMoney& sum);
void display_money();
private:
int dollars;
int cents;
};
void read_money(int& d, int& c);
int main()
{
int d, c;
AltMoney sum;
read_money(d, c);
AltMoney m1(d, c);
cout << "The first money is:";
m1.display_money();
cout << endl;
read_money(d, c);
AltMoney m2(d, c);
cout << "The second money is:";
m2.display_money();
cout << endl;
add(m1, m2, sum);
cout << "The sum is:";
sum.display_money();
2
cout << endl;
system("pause");
return 0;
}
AltMoney::AltMoney()
{
dollars = 0;
cents = 0;
}
AltMoney::AltMoney(int d, int c)
{
dollars = d;
cents = c;
}
void AltMoney::display_money()
{
cout << "$" << dollars << ".";
if (cents <= 9)
cout << "0"; //to display a 0 in the left for numbers less than 10
cout << cents << endl;
}
void add(AltMoney m1, AltMoney m2, AltMoney& sum)
{
int extra = 0;
sum.cents = m1.cents + m2.cents;
if (sum.cents >= 100){
sum.cents = sum.cents --‐ 100;
extra = 1;
}
sum.dollars = m1.dollars + m2.dollars + extra;
}
void read_money(int& d, int& c)
{
cout << "Enter dollar \n";
cin >> d;
cout << "Enter cents \n";
cin >> c;
if (d < 0 || c < 0)
{
cout << "Invalid dollars and cents, negative values\n";
exit(1);
}
}
3
Figure 1: The expected result of Problem 1
Modify the above program to include the following changes.
1. Modify function add of type AltMoney. Thus, add function now computes the
sum of dollars and cents and returns it as AltMoney.
friend AltMoney add(AltMoney m1, AltMoney m2);
Note that in the above version of the program, you have passed the object sum
as call_by_reference.
2. Make a member function read_money. Note that if you make read_money a
member function, then you can use it to directly initialize the dollars and cents of
an AltMoney type object directly.
4
(a + bi) + (c + di) = (a + c) + (b + d)i
(a + bi) - (c + di) = (a - c) + (b - d)i
(a + bi) * (c + di) = (ac – bd) + (ad + bc)i
Problem 2:
Define a class for storing and doing arithmetic with complex numbers. A complex
number is often written in the form a + bi, where i2= -1. a is called the real part, and b
the imaginary part. Complex numbers are added, subtracted and multiplied as follows:
Define a class named Complex. Each Complex object has two private data field
members of type double. Use the names real and imag for these fields.
Define the following public member functions for Complex:
//Constructors
Complex();//Constructs a complex object initialized to 0+0i
Complex(double r, double i);//Construcs an initialized complex object
//Input and output
void print(ostream& out); //prints a complex number to a stream in the form a+bi
void get(istream& in); //reads in a complex number from a stream in the form a+bi
//Operations
friend Complex multiply(const Complex& first, const Complex& second); //Returns a new
Complex object, which is the product of the first object with the second object.
friend Complex plus(const Complex& first, const Complex& second); //like multiply
only adds
friend Complex minus(const Complex& first, const Complex& second); //subtracts this
minus second
5
Figure 2: The expected result of Problem 2
Here is the main() function for testing:
int main()
{
Complex a,b,c,d,e;
cout << "Enter the values for a in the form x+yi: ";
a.get(cin);
cout << "Enter the values for b in the form x+yi: ";
b.get(cin);
c = myplus(a, b);
d = myminus(a, b);
e = multiply(a, b);
cout << "a is: ";
a.print(cout);
cout << "b is : ";
b.print(cout);
cout << "a+b is: ";
c.print(cout);
cout << "a--‐ b is: ";
d.print(cout);
cout << "a*b is: ";
e.print(cout);
return 0;
}

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

// ComplexNums.cpp : Defines the entry point for the console application.

//

// This program calculates with complex numbers using addtion, subtraction and multiplication.

#include "stdafx.h"

#include <iostream>

#include <complex>

#include <string>

#include <iomanip>

using namespace std;

//Complex number calculator class

class Complex

{

public:

Complex(); //Constructs a complex object initialized to 0+0i

Complex(double r,double i); //Construcs an initialized complex object

// Input and output

void print(ostream& out); //prints a complex number to a stream in the form a+bi

void get(istream& in); //reads in a complex number from a stream in the form a+bi

//Operations

friend Complex multiply(const Complex& first, const Complex& second);

friend Complex plus(const Complex& first,const Complex& second);

//like multiply only adds

friend Complex minus(const Complex& first,const Complex& second);

//subtracts this minus second

private:

double real,imag;

};

// Beginning of main function

int _tmain(int argc, _TCHAR* argv[])

{...