1.
Write a class to represent a complex number which has member functions to do the following
a. Set and show the value of the complex number
b. Add, subtract and multiply two complex numbers
c. Multiplying the complex number with a scalar value
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
// Constructor to initialize complex number
Complex(float r = 0, float i = 0) : real(r), imag(i) {}
// Function to set the value of the complex number
void setValue(float r, float i) {
real = r;
imag = i;
// Function to show the value of the complex number
void showValue() const {
cout << "Complex Number: " << real << " + " << imag << "i" << endl;
// Function to add two complex numbers
Complex add(const Complex& c) const {
return Complex(real + c.real, imag + c.imag);
}
// Function to subtract two complex numbers
Complex subtract(const Complex& c) const {
return Complex(real - c.real, imag - c.imag);
// Function to multiply two complex numbers
Complex multiply(const Complex& c) const {
float r = real * c.real - imag * c.imag;
float i = real * c.imag + imag * c.real;
return Complex(r, i);
// Function to multiply the complex number with a scalar value
Complex multiplyScalar(float scalar) const {
return Complex(real * scalar, imag * scalar);
};
int main() {
Complex c1(3, 4), c2(1, 2);
// Showing the values of the complex numbers
cout << "Complex number c1: ";
c1.showValue();
cout << "Complex number c2: ";
c2.showValue();
// Adding complex numbers
Complex resultAdd = c1.add(c2);
cout << "Addition result: ";
resultAdd.showValue();
// Subtracting complex numbers
Complex resultSub = c1.subtract(c2);
cout << "Subtraction result: ";
resultSub.showValue();
// Multiplying complex numbers
Complex resultMul = c1.multiply(c2);
cout << "Multiplication result: ";
resultMul.showValue();
// Multiplying complex number with a scalar
float scalar = 2;
Complex resultScalar = c1.multiplyScalar(scalar);
cout << "Multiplying c1 by scalar " << scalar << ": ";
resultScalar.showValue();
return 0;
}
3. Design and implement a class that represents a Harmonic Progression (HP). Implement
functions to do the following:
a. Generate the HP up to a specified number of terms
b. Calculate the sum of the HP to n terms and to infinity
c. Generate the nth term of the HP d. Generate the corresponding Arithmetic Progression.
(Design and implement a class that encapsulates an AP, and allow the HP class to use its
facilities by implementing friend functions.)
2. Write a Point class that represents a 2-d point in a plane.
Write member functions to
a. Set and show the value of a point
b. Find the distance between two points
c. Check whether two points are equal or not
#include <iostream>
#include <cmath>
using namespace std;
class Point {
private:
float x, y;
public:
// Constructor to initialize the point
Point(float xVal = 0, float yVal = 0) : x(xVal), y(yVal) {}
// Function to set the value of the point
void setValue(float xVal, float yVal) {
x = xVal;
y = yVal;
// Function to show the value of the point
void showValue() const {
cout << "(" << x << ", " << y << ")";
// Function to calculate the distance between two points
float distance(const Point& p) const {
float dx = x - p.x;
float dy = y - p.y;
return sqrt(dx * dx + dy * dy); // Using the distance formula
// Function to check whether two points are equal
bool isEqual(const Point& p) const {
return (x == p.x && y == p.y);
};
int main() {
// Create two Point objects
Point p1(3, 4), p2(6, 8);
// Show the values of the points
cout << "Point p1: ";
p1.showValue();
cout << endl;
cout << "Point p2: ";
p2.showValue();
cout << endl;
// Calculate and show the distance between the points
cout << "Distance between p1 and p2: " << p1.distance(p2) << endl;
// Check if the points are equal
if (p1.isEqual(p2)) {
cout << "The points are equal." << endl;
} else {
cout << "The points are not equal." << endl;
return 0;