C++ w/Operator Overloading & Inheritance Failing to Execute
Nov 30, 2010 at 5:53pm UTC
Hello, I've been banging my head on this for a while now. I have two classes, RationalNumber and Term. The RationalNumber class allows for the creation of an object of "numerator/denominator". I created the Term class to utilize the RationalNumber class and add an exponent value. The final object should look like "n/dx^2". I just got my code to compile, but when I run it through the it fails without any errors. The only thing that I found was that it breaks at the first function in Term.cpp. Thank you in advance!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include <iostream>
using namespace std;
class RationalNumber {
public :
//initializing Constructors/functions
RationalNumber();
RationalNumber(int , int );
void initRational();
void setNumerator(int );
int getNumerator();
void setDenominator(int );
int getDenominator();
int getGCD();
void simplifyRational(int );
void print();
//Overloaded operators
RationalNumber operator +(RationalNumber) const ;
RationalNumber operator -(RationalNumber) const ;
RationalNumber operator *(RationalNumber) const ;
RationalNumber operator /(RationalNumber) const ;
friend ostream &operator << (ostream &output, const RationalNumber &fraction) {
output << fraction.numerator << "/" << fraction.denominator;
return output;
};
private :
//declaring private variables
int numerator;
int denominator;
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
#include "RationalNumber.h"
#include <iostream>
using namespace std;
//Empty Constructor
RationalNumber::RationalNumber() {
};
//Rational Constructor
RationalNumber::RationalNumber(int num, int den) {
if (num != 0)
if (den !=0)
numerator = num;
denominator = den;
}
void RationalNumber::initRational() {
char slash;
cout << "\nPlease enter a rational number in the form of a/b: " ;
cin >> numerator >> slash >> denominator;
if (denominator == 0) {
cout << "\nCannot divide by 0, please enter another denominator: " ;
cin >> denominator;
}
}
//sets the numerator for the rational
void RationalNumber::setNumerator(int num) {
numerator = num;
}
//obtains the value of the numerator
int RationalNumber::getNumerator() {
return numerator;
}
//sets the denominator for the rational
void RationalNumber::setDenominator(int den) {
denominator = den;
}
//obtains the value of the denominator
int RationalNumber::getDenominator() {
return denominator;
}
//Obtains the greatest commin divisor
int RationalNumber::getGCD() {
int temp;
int num = numerator;
int den = denominator;
while (den != 0) {
temp = den;
den = num % den;
num = temp;
}
return num;
}
void RationalNumber::simplifyRational(int gcd) {
numerator = numerator / gcd;
denominator = denominator / gcd;
}
//Display Rational number
void RationalNumber::print() {
cout << numerator << "/" << denominator << endl;
}
RationalNumber RationalNumber::operator +(RationalNumber rationalptr) const {
RationalNumber ptr;
ptr.numerator = numerator * rationalptr.denominator + denominator * rationalptr.numerator;
ptr.denominator = denominator * rationalptr.denominator;
return (ptr);
}
RationalNumber RationalNumber::operator -(RationalNumber rationalptr) const {
RationalNumber ptr;
ptr.numerator = numerator * rationalptr.denominator - denominator * rationalptr.numerator;
ptr.denominator = denominator * rationalptr.denominator;
return (ptr);
}
RationalNumber RationalNumber::operator *(RationalNumber rationalptr) const {
RationalNumber ptr;
ptr.numerator = numerator * rationalptr.numerator;
ptr.denominator = denominator * rationalptr.denominator;
return (ptr);
}
RationalNumber RationalNumber::operator /(RationalNumber rationalptr) const {
RationalNumber ptr;
ptr.numerator = numerator * rationalptr.denominator;
ptr.denominator = denominator * rationalptr.numerator;
return (ptr);
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include "RationalNumber.h"
#include <iostream>
#include <vector>
using namespace std;
class Term : public RationalNumber {
public :
Term();
void initExponent();
void setExponent(int );
int getExponent();
void prepTerm();
void displayTerm();
virtual void print();
private :
int exponent;
vector<RationalNumber> rational_vector;
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include "Term.h"
#include <iostream>
using namespace std;
Term::Term() {
rational_vector.reserve(3);
}
void Term::initExponent() {
cout << "Enter the exponent: " ;
cin >> exponent;
}
void Term::setExponent(int exp) {
exponent = exp;
}
int Term::getExponent() {
return exponent;
}
void Term::prepTerm() {
RationalNumber rational;
rational.initRational();
rational_vector.push_back(rational);
cout << "Enter the exponent: " ;
cin >> exponent;
}
void Term::print() {
cout << "x^" << getExponent();
}
void Term::displayTerm() {
vector<RationalNumber>::iterator it;
for (it=rational_vector.begin(); it!=rational_vector.end(); ++it) {
cout << ((RationalNumber)*it) << endl;
}
}
Nov 30, 2010 at 6:03pm UTC
The program seems to fail when I try and create a default Term object.
Topic archived. No new replies allowed.