Skip to main content
Code Review

Return to Question

Question Protected by Mast
added 12 characters in body; edited tags
Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479

Question:

Question: Need to create three classes.

SOLUTION:

Solution

Question: Need to create three classes.

SOLUTION:

Question:

Need to create three classes.

Solution

edited title
Link
Unbreakable
  • 607
  • 1
  • 9
  • 16

Add fraction and integertwo fractions or two integers using template class and overloading

Source Link
Unbreakable
  • 607
  • 1
  • 9
  • 16

Add fraction and integer using template class and overloading

Question: Need to create three classes.

  1. Number
  2. Fraction
  3. Integer

Requirements

  1. The first class should support "display", "==", and "+".

  2. "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.

  3. The Fraction class, which is represented by its numerator and denominator (both are integers).

  4. 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.

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /