Question:
Need to create three classes.
- Number
- Fraction
- Integer
Requirements
The first class should support “display”, “==”, and “+”.
"Display" : This operation displays the Number itself in its original form. "+" : This operation adds the number itself with another number and returns a third number whose numeric value is equal to the sum of the numeric values of the former two.
The Fraction class, which is represented by its numerator and denominator (both are integers).
For a Fraction, it needs to be displayed in its original form. That is, 2/4 has to be displayed as “2/4”, not “1/2” or “0.5”.
Solution
Number.h
#pragma once
template<class T>
class Number
{
virtual T& operator= (const T &) = 0;
virtual const T operator+ (const T &) = 0;
virtual void display() = 0;
};
Fraction.h
#pragma once
#include "Number.h"
class Fraction : public Number<Fraction>
{
private:
int _numerator;
int _denominator;
public:
void display();
Fraction();
Fraction(int num, int den);
Fraction& operator= (const Fraction &);
const Fraction operator+ (const Fraction &);
int Fraction::gcdCalculate(int val1, int val2);
int Fraction::lcmCalculate(const int val1, const int val2);
~Fraction();
};
Fraction.cpp
#include "Fraction.h"
#include <iostream>
using namespace std;
// constructor
Fraction::Fraction()
{
_numerator = 0;
_denominator = 1;
}
// parameterised constructor setting the denominator and numerator values
Fraction::Fraction(int num, int den)
{
_numerator = num;
_denominator = den;
}
// display the fraction value
void Fraction::display()
{
std::cout << this->_numerator << "/";
std::cout << this->_denominator << endl;
}
// "=" operator overloading
Fraction& Fraction::operator=(const Fraction &num)
{
_numerator = num._numerator;
_denominator = num._denominator;
return *this;
}
// "+" operator overloading
const Fraction Fraction::operator+(const Fraction &numberTwo)
{
Fraction outputFraction;
int lcm = lcmCalculate(this->_denominator, numberTwo._denominator);
int multiplier1 = 0;
if (this->_denominator)
multiplier1 = lcm / this->_denominator;
int multiplier2 = 0;
if (numberTwo._denominator)
multiplier2 = lcm / numberTwo._denominator;
outputFraction._numerator = this->_numerator * multiplier1 + numberTwo._numerator * multiplier2;
outputFraction._denominator = lcm;
return outputFraction;
}
// LCM Calculation
int Fraction::lcmCalculate(const int val1, const int val2)
{
int temp = gcdCalculate(val1, val2);
return temp ? (val1 / temp * val2) : 0;
}
// GCD Calculation
int Fraction::gcdCalculate(int val1, int val2)
{
for (;;)
{
if (val1 == 0) return val2;
val2 %= val1;
if (val2 == 0) return val1;
val1 %= val2;
}
}
// destructor
Fraction::~Fraction()
{}
Integer.h
#pragma once
#include "Number.h"
class Integer : public Number<Integer>
{
private:
int intValue;
public:
void display();
Integer();
Integer(int num);
Integer& operator= (const Integer &);
const Integer operator+ (const Integer &);
~Integer();
};
Integer.cpp
#include "Integer.h"
#include "Number.h"
#include <iostream>
using namespace std;
// constructor
Integer::Integer()
{
intValue = 0;
}
// parameterized constructor
Integer::Integer(int num)
{
intValue = num;
}
// display the int value
void Integer::display()
{
std::cout << this->intValue << endl;
}
// operator "=" overloading
Integer& Integer::operator=(const Integer &secondNumber)
{
intValue = secondNumber.intValue;
return *this;
}
// operator "+" overloading
const Integer Integer::operator+(const Integer &secondNumber)
{
Integer temp;
temp.intValue = this->intValue + secondNumber.intValue;
return temp;
}
// destructor
Integer::~Integer(){}
Main.cpp
#include "Integer.h"
#include <iostream>
#include "Fraction.h"
using namespace std;
int main()
{
Integer sumOfInteger;
Integer int1(30);
Integer int2(40);
sumOfInteger = int1 + int2;
sumOfInteger.display();
Fraction sumOfFraction;
Fraction fracOne(2,4);
Fraction fracTwo(2,4);
sumOfFraction = fracOne + fracTwo;
sumOfFraction.display();
return 0;
}
Can someone please review and help me in improving my overall coding skills and standards.
nominator
print (e.g. std::cout << _numerator;) \$\endgroup\$