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

C++ Complex Number Arithmetic Class

The document outlines the implementation of a C++ program that defines a Complex class for arithmetic operations on complex numbers, including addition, subtraction, and display functionality. It also describes operator overloading for string concatenation and extends the Complex class to support input/output operations and comparisons. Additionally, it presents a hierarchy of shape classes for both 2D and 3D shapes, demonstrating inheritance and polymorphism.

Uploaded by

Kashish Dua
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 views14 pages

C++ Complex Number Arithmetic Class

The document outlines the implementation of a C++ program that defines a Complex class for arithmetic operations on complex numbers, including addition, subtraction, and display functionality. It also describes operator overloading for string concatenation and extends the Complex class to support input/output operations and comparisons. Additionally, it presents a hierarchy of shape classes for both 2D and 3D shapes, demonstrating inheritance and polymorphism.

Uploaded by

Kashish Dua
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

EXP 9.

Create a class called Complex for performing arithmetic with complex numbers.
Write a driver program in C++
to test yourclass. Complex numbers have the form realPart + imaginaryPart * i
Provide public member functions for each of the following:
a) Addition of two Complex numbers: The real parts are added together and the
imaginary parts are added together.

b) Subtraction of two Complex numbers: The real part of the right operand is
subtracted from the real part of the left operand and the imaginary part of the
right operand is subtracted from the imaginary part of the left operand.

c) Printing Complex numbers in the form (a, b) where a is the real part and b is
the imaginary part.
#include <iostream>
class Complex {
private:
double realPart;
double imaginaryPart;
public:
// Constructor
Complex(double real = 0.0, double imag = 0.0) : realPart(real), imaginaryPart(imag) {}

// Function for addition of two Complex numbers


Complex add(const Complex& other) const {
return Complex(realPart + [Link], imaginaryPart + [Link]);
}

// Function for subtraction of two Complex numbers


Complex subtract(const Complex& other) const {
return Complex(realPart - [Link], imaginaryPart - [Link]);
}

// Function to display the complex number


void display() const {
std::cout << realPart;
if (imaginaryPart >= 0) {
std::cout << " + " << imaginaryPart << "i";
} else {
std::cout << " - " << -imaginaryPart << "i";
}
std::cout << std::endl;
}
};

int main() {
// Create Complex number objects
Complex c1(3.0, 4.0);
Complex c2(1.5, -2.5);
Complex c3; // Uses default constructor (0, 0)

std::cout << "Complex Number 1: ";


[Link]();

std::cout << "Complex Number 2: ";


[Link]();

std::cout << "Complex Number 3 (default): ";


[Link]();

// Test addition
Complex sum = [Link](c2);
std::cout << "Sum of C1 and C2: ";
[Link]();

// Test subtraction
Complex difference = [Link](c2);
std::cout << "Difference of C1 and C2: ";
[Link]();

return 0;
}
RUN
Output
Complex Number 1: 3 + 4i
Complex Number 2: 1.5 - 2.5i
Complex Number 3 (default): 0 + 0i
Sum of C1 and C2: 4.5 + 1.5i
Difference of C1 and C2: 1.5 + 6.5i

EXP. 9
#include <iostream.h>
class Complex {
private:
double realPart;
double imaginaryPart;

public:
// Constructor
Complex(double real = 0.0, double imag = 0.0) : realPart(real), imaginaryPart(imag) {}

// Function for addition of two Complex numbers


Complex add(const Complex& other) const {
return Complex(realPart + [Link], imaginaryPart + [Link]);
}

// Function for subtraction of two Complex numbers


Complex subtract(const Complex& other) const {
return Complex(realPart - [Link], imaginaryPart - [Link]);
}

// Function to display the complex number


void display() const {
cout << realPart;
if (imaginaryPart >= 0) {
cout << " + " << imaginaryPart << "i";
} else {
cout << " - " << -imaginaryPart << "i";
}
cout <<endl;
}
};

int main() {
// Create Complex number objects
Complex c1(3.0, 4.0);
Complex c2(1.5, -2.5);
Complex c3; // Uses default constructor (0, 0)

cout << "Complex Number 1: ";


[Link]();

cout << "Complex Number 2: ";


[Link]();

cout << "Complex Number 3 (default): ";


[Link]();

// Test addition
Complex sum = [Link](c2);
cout << "Sum of C1 and C2: ";
[Link]();

// Test subtraction
Complex difference = [Link](c2);
cout << "Difference of C1 and C2: ";
[Link]();

return 0;
}
Complex Number 1: 3 + 4i
Complex Number 2: 1.5 - 2.5i
Complex Number 3 (default): 0 + 0i
Sum of C1 and C2: 4.5 + 1.5i
Difference of C1 and C2: 1.5 + 6.5i

EXP 10. Implement overloading of operator+ in c++ to allow


operations such as string1 = string2 + string3

#include <iostream>
#include <string>
class MyString {
public:
std::string data;

MyString(const std::string& str = "") : data(str) {}

// Overload operator+ as a member function


MyString operator+(const MyString& other) const {
return MyString(this->data + [Link]);
}

// For demonstration, allow printing


friend std::ostream& operator<<(std::ostream& os, const MyString& ms) {
os << [Link];
return os;
}
};

int main() {
MyString s1("Operator ");
MyString s2(" Overloading ");
MyString s3 = s1 + s2; // Calls [Link]+(s2)
std::cout << s3 << std::endl; // Output: Hello World
MyString s4("in C++");
MyString s5 = s3 + s4; // Concatenate s3 and s4
std::cout << s5 << std::endl; // Output: Hello WorldC++

return 0;
}

RUN
Output
Operator Overloading
Operator Overloading in C++

EXP.11
Consider class Complex in problem 9, a) Modify the class to enable input and output of
complex numbers through the overloaded >> and << operators, respectively b) Overload the
multiplication operator to enable multiplication of two complex numbers as in algebra. c)
Overload the == and != operators to allow comparisons of complex numbers

#include <iostream>
class Complex {
private:
double realPart;
double imaginaryPart;

public:
// Constructor
Complex(double real = 0.0, double imag = 0.0)
{
realPart=real;
imaginaryPart=imag;
}

// Overload the addition operator (+)


Complex operator+( Complex& obj)
{
return Complex(realPart + [Link], imaginaryPart + [Link]);
}

// Overload the subtraction operator (-)


Complex operator-(Complex& other)
{
return Complex(realPart - [Link], imaginaryPart - [Link]);
}

// Overload the multiplication operator (*)


Complex operator*(Complex& o)
{
// (a + bi) * (c + di) = (ac - bd) + (ad + bc)i
double newReal = (realPart * [Link]) - (imaginaryPart * [Link]);
double newImaginary = (realPart * [Link]) + (imaginaryPart * [Link]);
return Complex(newReal, newImaginary);
}

// Overload the equality operator (==)


bool operator==(Complex& o)
{
return (realPart == [Link]) && (imaginaryPart == [Link]);
}

// Overload the inequality operator (!=)


bool operator!=(Complex& o)
{
return !(*this == o); // Reuse the == operator
}

// Friend function to overload the output stream operator (<<)


friend std::ostream& operator<<(std::ostream& os, Complex& c)
{
os << "(" << [Link] << ", " << [Link] << ")";
return os;
}

// Friend function to overload the input stream operator (>>)


friend std::istream& operator>>(std::istream& is, Complex& c)
{
std::cout << "Enter real part: ";
is >> [Link];
std::cout << "Enter imaginary part: ";
is >> [Link];
return is;
}
};

int main() {
Complex c1(3.0, 4.0);
Complex c2(1.5, 2.5);
Complex c3; // Default constructor (0, 0)
Complex c4;

std::cout << "Initial Complex Numbers:" << std::endl;


std::cout << "c1 = " << c1 << std::endl;
std::cout << "c2 = " << c2 << std::endl;
std::cout << "c3 = " << c3 << std::endl;

// Test addition
Complex sum = c1 + c2;
std::cout << "\nAddition (c1 + c2) = " << sum << std::endl;

// Test subtraction
Complex difference = c1 - c2;
std::cout << "Subtraction (c1 - c2) = " << difference << std::endl;

// Test multiplication
Complex product = c1 * c2;
std::cout << "Multiplication (c1 * c2) = " << product << std::endl;

// Test input operator


std::cout << "\nEnter values for c4:" << std::endl;
std::cin >> c4;
std::cout << "c4 = " << c4 << std::endl;

// Test equality and inequality operators


Complex c5(3.0, 4.0);
std::cout << "\nComparison:" << std::endl;
std::cout << "c1 == c5: " << (c1 == c5 ? "True" : "False") << std::endl;
std::cout << "c1 != c2: " << (c1 != c2 ? "True" : "False") << std::endl;
std::cout << "c1 == c2: " << (c1 == c2 ? "True" : "False") << std::endl;
return 0;
}
RUN
Output
Initial Complex Numbers:
c1 = (3, 4)
c2 = (1.5, 2.5)
c3 = (0, 0)

Addition (c1 + c2) = (4.5, 6.5)


Subtraction (c1 - c2) = (1.5, 1.5)
Multiplication (c1 * c2) = (-5.5, 13.5)

Enter values for c4:


Enter real part: 5
Enter imaginary part: 3
c4 = (5, 3)
Comparison:
c1 == c5: True
c1 != c2: True
c1 == c2: False

RUN
Output
Initial Complex Numbers:
c1 = (3, 4)
c2 = (1.5, 2.5)
c3 = (0, 0)

Addition (c1 + c2) = (4.5, 6.5)


Subtraction (c1 - c2) = (1.5, 1.5)
Multiplication (c1 * c2) = (-5.5, 13.5)

Enter values for c4:


Enter real part: 5
Enter imaginary part: 2i
c4 = (5, 2)
Comparison:
c1 == c5: True
c1 != c2: True
c1 == c2: False

EXP. 12
Write a program in c++ to develop hierarchy of inheritance for the properties of shapes and
their relevant functions. e.g. Shapes→2D/3D, 2D→ellipse→circle|rectangle→square and
expand it for 3D accordingly.

#include <iostream>
#include <cmath>
// Base class for all shapes
class Shape {
public:
virtual void displayInfo() const = 0; // Pure virtual function
virtual ~Shape() {} // Virtual destructor for proper cleanup
};

// Intermediate base class for 2D shapes


class Shape2D : public Shape {
public:
virtual double calculateArea() const = 0; // Pure virtual function
void displayInfo() const override {
std::cout << "This is a 2D Shape." << std::endl;
}
};

// Intermediate base class for 3D shapes


class Shape3D : public Shape {
public:
virtual double calculateVolume() const = 0; // Pure virtual function
virtual double calculateSurfaceArea() const = 0; // Pure virtual function
void displayInfo() const override {
std::cout << "This is a 3D Shape." << std::endl;
}
};

// Derived 2D shapes
class Ellipse : public Shape2D {
protected:
double majorAxis;
double minorAxis;
public:
Ellipse(double ma, double mi) : majorAxis(ma), minorAxis(mi) {}
double calculateArea() const override {
return M_PI * majorAxis * minorAxis;
}
void displayInfo() const override {
std::cout << "Ellipse (Major Axis: " << majorAxis << ", Minor Axis: " << minorAxis << ")" <<
std::endl;
std::cout << "Area: " << calculateArea() << std::endl;
}
};

class Circle : public Ellipse { // Circle inherits from Ellipse


public:
Circle(double radius) : Ellipse(radius, radius) {}
void displayInfo() const override {
std::cout << "Circle (Radius: " << majorAxis << ")" << std::endl;
std::cout << "Area: " << calculateArea() << std::endl;
}
};

class Rectangle : public Shape2D {


protected:
double length;
double width;
public:
Rectangle(double l, double w) : length(l), width(w) {}
double calculateArea() const override {
return length * width;
}
void displayInfo() const override {
std::cout << "Rectangle (Length: " << length << ", Width: " << width << ")" << std::endl;
std::cout << "Area: " << calculateArea() << std::endl;
}
};

class Square : public Rectangle { // Square inherits from Rectangle


public:
Square(double side) : Rectangle(side, side) {}
void displayInfo() const override {
std::cout << "Square (Side: " << length << ")" << std::endl;
std::cout << "Area: " << calculateArea() << std::endl;
}
};

// Derived 3D shapes
class Sphere : public Shape3D {
protected:
double radius;
public:
Sphere(double r) : radius(r) {}
double calculateVolume() const override {
return (4.0 / 3.0) * M_PI * radius * radius * radius;
}
double calculateSurfaceArea() const override {
return 4.0 * M_PI * radius * radius;
}
void displayInfo() const override {
std::cout << "Sphere (Radius: " << radius << ")" << std::endl;
std::cout << "Volume: " << calculateVolume() << std::endl;
std::cout << "Surface Area: " << calculateSurfaceArea() << std::endl;
}
};

class Cube : public Shape3D {


protected:
double side;
public:
Cube(double s) : side(s) {}
double calculateVolume() const override {
return side * side * side;
}
double calculateSurfaceArea() const override {
return 6.0 * side * side;
}
void displayInfo() const override {
std::cout << "Cube (Side: " << side << ")" << std::endl;
std::cout << "Volume: " << calculateVolume() << std::endl;
std::cout << "Surface Area: " << calculateSurfaceArea() << std::endl;
}
};
int main() {
Circle circle(5.0);
Square square(4.0);
Sphere sphere(3.0);
Cube cube(2.0);
Ellipse ellipse(6.0, 3.0);
Rectangle rectangle(7.0, 2.0);

[Link]();
std::cout << std::endl;
[Link]();
std::cout << std::endl;
[Link]();
std::cout << std::endl;
[Link]();
std::cout << std::endl;
[Link]();
std::cout << std::endl;
[Link]();
std::cout << std::endl;

// Polymorphism example
Shape* shapes[] = {&circle, &square, &sphere, &cube, &ellipse, &rectangle};
for (Shape* s : shapes) {
s->displayInfo();
std::cout << std::endl;
}
return 0;
}
RUN
Output
Circle (Radius: 5)
Area: 78.5398

Square (Side: 4)
Area: 16
Sphere (Radius: 3)
Volume: 113.097
Surface Area: 113.097

Cube (Side: 2)
Volume: 8
Surface Area: 24

Ellipse (Major Axis: 6, Minor Axis: 3)


Area: 56.5487

Rectangle (Length: 7, Width: 2)


Area: 14

Circle (Radius: 5)
Area: 78.5398

Square (Side: 4)
Area: 16

Sphere (Radius: 3)
Volume: 113.097
Surface Area: 113.097

Cube (Side: 2)
Volume: 8
Surface Area: 24

Ellipse (Major Axis: 6, Minor Axis: 3)


Area: 56.5487

Rectangle (Length: 7, Width: 2)


Area: 14

EXP-13
Write a simple function template for predicate function isEqualTo that compares its two
arguments with the equality operator (==) and returns true if they are equal and false if
they are not equal. Use this function template in a program that calls isEqualTo only with a
variety of built-in types. Now write a separate version of the program that calls isEqualTo
with a user defined class type, but does not overload the equality operator.

#include <iostream>
#include <string>
// Function template for isEqualTo
template <typename T>
bool isEqualTo(const T& a, const T& b) {
return a == b;
}

// User-defined class without overloaded equality operator


class MyClass {
public:
int value;
std::string name;

MyClass(int v, const std::string& n) : value(v), name(n) {}

// No overloaded operator== here


};

int main() {
// Calling isEqualTo with built-in types
std::cout << "--- Built-in Types ---" << std::endl;
int i1 = 5, i2 = 5, i3 = 10;
std::cout << "isEqualTo(int 5, int 5): " << std::boolalpha << isEqualTo(i1, i2) << std::endl;
std::cout << "isEqualTo(int 5, int 10): " << std::boolalpha << isEqualTo(i1, i3) << std::endl;

double d1 = 3.14, d2 = 3.14, d3 = 2.71;


std::cout << "isEqualTo(double 3.14, double 3.14): " << std::boolalpha << isEqualTo(d1, d2) <<
std::endl;
std::cout << "isEqualTo(double 3.14, double 2.71): " << std::boolalpha << isEqualTo(d1, d3) <<
std::endl;

char c1 = 'A', c2 = 'A', c3 = 'B';


std::cout << "isEqualTo(char 'A', char 'A'): " << std::boolalpha << isEqualTo(c1, c2) <<
std::endl;
std::cout << "isEqualTo(char 'A', char 'B'): " << std::boolalpha << isEqualTo(c1, c3) <<
std::endl;

std::string s1 = "hello", s2 = "hello", s3 = "world";


std::cout << "isEqualTo(string \"hello\", string \"hello\"): " << std::boolalpha << isEqualTo(s1,
s2) << std::endl;
std::cout << "isEqualTo(string \"hello\", string \"world\"): " << std::boolalpha << isEqualTo(s1,
s3) << std::endl;

std::cout << "\n--- User-Defined Class (without overloaded operator==) ---" << std::endl;
MyClass obj1(10, "First");
MyClass obj2(10, "First");
MyClass obj3(20, "Second");

// Attempting to call isEqualTo with MyClass objects without overloaded operator==

std::cout << "Attempting to call isEqualTo with MyClass objects (without overloaded
operator==) would result in a compilation error." << std::endl;
std::cout << "This is because the default '==' operator for user-defined types (without an
explicit overload) compares memory addresses, not member values." << std::endl;

return 0;
}
RUN
Output
--- Built-in Types ---
isEqualTo(int 5, int 5): true
isEqualTo(int 5, int 10): false
isEqualTo(double 3.14, double 3.14): true
isEqualTo(double 3.14, double 2.71): false
isEqualTo(char 'A', char 'A'): true
isEqualTo(char 'A', char 'B'): false
isEqualTo(string "hello", string "hello"): true
isEqualTo(string "hello", string "world"): false

--- User-Defined Class (without overloaded operator==) ---


Attempting to call isEqualTo with MyClass objects (without overloaded operator==) would
result in a compilation error.
This is because the default '==' operator for user-defined types (without an explicit overload)
compares memory addresses, not member values.

You might also like