0% found this document useful (0 votes)
6 views4 pages

CS 200 - Lab - 5

Lab 5

Uploaded by

seemarahat1122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

CS 200 - Lab - 5

Lab 5

Uploaded by

seemarahat1122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Task 1 - A Simple Complex Task [50 Marks]

Create a class “ComplexNumber” with two private float data members. Real component and imaginary
component. Write the following methods.
1. A parameterized constructor.
2. A display method which displays the ComplexNumber in the form of a + bi where a and b are
real numbers and 'i' is an imaginary number called “iota”.

Overload the following operators:


3. The assignment operator (=): Assigns the right object to the left.
4. Is equal to operator (==): Returns true if the two ComplexNumbers are equal, false otherwise.
5. Addition operator (+): Returns a ComplexNumber which contains the sum of two
ComplexNumber objects.
6. Subtraction operator (-): Returns the difference between two ComplexNumber objects.
7. The unary minus operator (-): Returns the negative of a ComplexNumber.
8. Multiplication operator (*): Self-explanatory.

ComplexNumber C1(1, 2);


ComplexNumber C2(3, 4);

ComplexNumber C3 = C1 * C2;
C3.Display();

// Output : -5.000000 + 10.000000i

9. The division operator (/): Returns the result of the division of two ComplexNumbers.

ComplexNumber C1(1, 2), C2(3, 4);


ComplexNumber C3 = C1 / C2;

C3.Display();

// Output : 0.440000 + 0.080000i

Additional Material:
1.

2.
Task 2 - Another Simple Recursive Task [20 Marks]

Write a recursive function PrintArray, which takes two parameters, a pointer to an array, and an int.
PrintArray should print all the elements in the passed array using recursion.

Prototype: PrintArray(int*, int)

Note: This question has binary marking. Call this function in main on int arr[4] = {1, 2, 3, 4} and show it
to your evaluation TA.

int main() for Task 1:


int main()
{
ComplexNumber C1(1, 2);
ComplexNumber C2(3, 4);

ComplexNumber C3 = C1 + C2;
C3.Display();

ComplexNumber C4 = C1 - C2;
C4.Display();

ComplexNumber C5 = C1 * C2;
C5.Display();

ComplexNumber C6 = C1 / C2;
C6.Display();

ComplexNumber C7 = -C1;
C7.Display();

ComplexNumber C8 = C1;
C8.Display();

cout << (C8 == C1) << endl; // Don't forget the brackets
cout << (C1 == C2) << endl;
}
Task 3 - Trace This [30 Marks]
#include <iostream>

using namespace std;

class myClass
{
int a;
int b;

public:
myClass()
{
a = 0;
b = 0;
}
myClass(int c, int d)
{
a = c;
b = d;
}
void operator=(myClass v)
{
a = v.a;
b = v.b;
}
void display()
{
cout << "A : " << a << "\t" << "B : " << b << endl;
}
myClass operator+(myClass var2)
{
myClass var3;
var3.a = a - var2.a;
var3.b = b - var2.b;
return var3;
}
myClass operator-(myClass var2)
{
myClass var3;
var3.a = a * var2.a;
var3.b = b / var2.b;
return var3;
}
myClass operator&(myClass var2)
{
myClass var3;
var3.a = a + b - var2.a;
var3.b = b - a + var2.b;
return var3;
}
myClass operator==(myClass var2)
{
myClass var4;
var4.a = a * var2.b;
var4.b = b * var2.a;
return var4;
}
};

int main()
{
myClass var1(2, 6);
myClass var2(3, 12);
myClass var4 = var1 + var2;
var4.display();
myClass var5 = var4 - var1;
var5.display();
myClass var6 = var1 & var2;
var6.display();
myClass var7 = var1 == var2;
var7.display();
}

You might also like