C++ Exercise 7

Mannan Ul Haq
0

Objective:

  • Operator Overloading

Activity 1:

Write a class called Fraction that represents a fraction (i.e., a rational number). Overload the +, -, *, and / operators to allow addition, subtraction, multiplication, and division of two fractions. The class should also implement a method called simplify() that simplifies the fraction to lowest terms. The class should have the following attributes: numerator (the numerator of the fraction) and denominator (the denominator of the fraction).

Solution:

#include <iostream>
using namespace std;

class Fraction
{
private:
    int numerator;
    int denominator;

public:

    Fraction(int n = 0, int d = 1)
    {
        if (d == 0)
        {
            cout << "Error: Division by zero" << endl;
            exit(1);
        }
        numerator = n;
        denominator = d;
    }
    int gcd(int a, int b)
    {
        if (b == 0)
        {
            return a;
        }
        return gcd(b, a % b);
    }
    void simplify()
    {
        int g = gcd(numerator, denominator);
        numerator /= g;
        denominator /= g;
    }
    Fraction operator+(const Fraction& f)
    {
        int n = numerator * f.denominator + f.numerator * denominator;
        int d = denominator * f.denominator;
        return Fraction(n, d);
    }

    Fraction operator-(const Fraction& f)
    {
        int n = numerator * f.denominator - f.numerator * denominator;
        int d = denominator * f.denominator;
        return Fraction(n, d);
    }

    Fraction operator*(const Fraction& f)
    {
        int n = numerator * f.numerator;
        int d = denominator * f.denominator;
        return Fraction(n, d);
    }

    Fraction operator/(const Fraction& f)
    {
        if (f.numerator == 0)
        {
            cout << "Error: Division by zero" << endl;
            exit(1);
        }
        int n = numerator * f.denominator;
        int d = denominator * f.numerator;
        return Fraction(n, d);
    }
    void print()
    {
        cout << numerator << "/" << denominator << endl;
    }
};

int main()
{
    cout << "f1(1, 2) and f2(2, 3)." << endl;
    Fraction f1(1, 2);
    Fraction f2(2, 3);
    Fraction f3 = f1 + f2;
    Fraction f4 = f1 - f2;
    Fraction f5 = f1 * f2;
    Fraction f6 = f1 / f2;
    f3.simplify();
    f4.simplify();
    f5.simplify();
    f6.simplify();
    f3.print();
    f4.print();
    f5.print();
    f6.print();

    return 0;
}



Activity 2:

Write a class called ComplexNumber that represents a complex number. Overload the ++ and -- operators to allow incrementing and decrementing the real and imaginary parts of the complex number. The class should have the following attributes: real (the real part of the complex number) and imag (the imaginary part of the complex number).


Solution:

#include <iostream>
using namespace std;

class ComplexNumber
{
public:
    double real;
    double imag;
    ComplexNumber(double r, double i)
    {
        real = r;
        imag = i;
    }
    ComplexNumber operator++()
    {
        real++;
        imag++;
        return *this;
    }
    ComplexNumber operator--()
    {
        real--;
        imag--;
        return *this;
    }
};

int main()
{
    ComplexNumber c(1, 2);
    ++c;
    cout << c.real << " " << c.imag << endl;
    --c;
    cout << c.real << " " << c.imag << endl;

    return 0;
}



Activity 3: 

Write a class called String that represents a string. Overload the + operator to allow concatenation of two strings. The class should also implement a method called reverse() that reverses the string. The class should have the following attribute: str (the string).

Solution:

#include <iostream>
#include <string>
using namespace std;

class String
{
public:
    string str;

    String()
    {
        str = "";
    }

    String(string s)
    {
        str = s;
    }

    String operator+(String s)
    {
        return String(str + s.str);
    }

    void reverse()
    {
        int n = str.length();
        for (int i = 0; i < n / 2; i++)
        {
            swap(str[i], str[n - i - 1]);
        }
    }
};

int main()
{
    cout << "s1(Hello) and s2(World)." << endl;
    String s1("Hello");
    String s2("World");

    String s3 = s1 + s2;
    cout << s3.str << endl;

    s3.reverse();
    cout << s3.str << endl;

    return 0;
}


Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !