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:
using System;
class Fraction
{
private int numerator;
private int denominator;
public Fraction(int n = 0, int d = 1)
{
if (d == 0)
{
Console.WriteLine("Error: Division by zero");
return;
}
numerator = n;
denominator = d;
}
private int GCD(int a, int b)
{
return b == 0 ? a : GCD(b, a % b);
}
public void Simplify()
{
int g = GCD(numerator, denominator);
numerator /= g;
denominator /= g;
}
public static Fraction operator +(Fraction a, Fraction b)
{
int n = a.numerator * b.denominator + b.numerator * a.denominator;
int d = a.denominator * b.denominator;
return new Fraction(n, d);
}
public static Fraction operator -(Fraction a, Fraction b)
{
int n = a.numerator * b.denominator - b.numerator * a.denominator;
int d = a.denominator * b.denominator;
return new Fraction(n, d);
}
public static Fraction operator *(Fraction a, Fraction b)
{
int n = a.numerator * b.numerator;
int d = a.denominator * b.denominator;
return new Fraction(n, d);
}
public static Fraction operator /(Fraction a, Fraction b)
{
if (b.numerator == 0)
{
Console.WriteLine("Error: Division by zero");
return new Fraction();
}
int n = a.numerator * b.denominator;
int d = a.denominator * b.numerator;
return new Fraction(n, d);
}
public void Print()
{
Console.WriteLine($"{numerator}/{denominator}");
}
}
class Program
{
static void Main()
{
Console.WriteLine("f1(1, 2) and f2(2, 3).");
Fraction f1 = new Fraction(1, 2);
Fraction f2 = new Fraction(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();
}
}
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:
using System;
class ComplexNumber
{
public double real;
public double imag;
public ComplexNumber(double r, double i)
{
real = r;
imag = i;
}
public static ComplexNumber operator ++(ComplexNumber a)
{
a.real++;
a.imag++;
return a;
}
public static ComplexNumber operator --(ComplexNumber a)
{
a.real--;
a.imag--;
return a;
}
}
class Program
{
static void Main()
{
ComplexNumber c = new ComplexNumber(1, 2);
c++;
Console.WriteLine($"{c.real} {c.imag}");
c--;
Console.WriteLine($"{c.real} {c.imag}");
}
}
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:
using System;
class String
{
public string str;
public String()
{
str = "";
}
public String(string s)
{
str = s;
}
public static String operator +(String s1, String s2)
{
return new String(s1.str + s2.str);
}
public void Reverse()
{
char[] charArray = str.ToCharArray();
Array.Reverse(charArray);
str = new string(charArray);
}
}
class Program
{
static void Main()
{
Console.WriteLine("s1(Hello) and s2(World).");
String s1 = new String("Hello");
String s2 = new String("World");
String s3 = s1 + s2;
Console.WriteLine(s3.str);
s3.Reverse();
Console.WriteLine(s3.str);
}
}