May 23, 2013 at 5:08am UTC
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
class Fraction {
public :
Fraction();
Fraction(int );
Fraction(int , int );
~Fraction();
void setNum(int );
void setDenom(int );
int getNum(void ) const ;
int getDenom(void ) const ;
void print(void );
private :
int num;
int denom;
};
class Circle {
public :
Circle();
Circle(Fraction& arg1);
friend ostream& operator <<(ostream&, Circle&);
~Circle();
void print(void );
protected :
Fraction *radius;
};
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
#include "Fraction.h"
#include "Circle.h"
#include <iostream>
using namespace std;
Fraction::Fraction() {
num = 0;
denom = 1;
}
Fraction::Fraction(int n) {
num = n;
denom = 1;
}
Fraction::Fraction(int n, int d) {
num = n;
denom = d;
}
void Fraction::setNum(int val) {
num = val;
return ;
}
void Fraction::setDenom(int val) {
denom = val;
return ;
}
int Fraction::getNum() const {
return num;
}
int FractionL::getDenom() const {
return denom;
}
Fraction& Fraction::operator =(const Fraction& arg) {
num = arg.num;
denom = arg.denom;
return *this ;
}
void Fraction::print() {
cout << "\n The Length is : " << num << endl;
cout << " The Width is : " << denom << endl;
cout << "\n\n" ;
}
ostream& operator <<(ostream& out, const Fraction& frac) {
out << "\n\tNumerator : " << frac.num
<< "\n\tDenominator : " << frac.denom << endl;
return out;
}
Fraction::~Fraction() {
}
//Circle
Circle::Circle() {
radius->setNum(1);
radius->setDenom(1);
}
Circle::Circle(Fraction& arg1) {
radius->setNum(arg1.getNum());
radius->setDenom(arg1.getDenom());
}
void Circle::print() {
cout << "The radius of the Circle is : " << radius->getNum() << ", "
<< radius->getDenom() << endl;
}
Circle::~Circle() {
delete this ->radius;
}
The problem in main
1 2
Fraction* fPtr = new Fraction(4, 1);
Circle* cPtrA = new Circle(*fPtr);
Both of these are classes.
First i use the copy constructor to set Numerator of classFraction to 4 and Denominator to 1.
Then i want to pass that (4, 1) into my classCircle but i get this error of unhandled exception through debugger.
Last edited on May 23, 2013 at 5:35am UTC
May 23, 2013 at 5:35am UTC
i've added the code
Last edited on May 23, 2013 at 5:35am UTC
May 23, 2013 at 5:43am UTC
In your Circle constructor you are not creating your radius Fraction, so it will be a null pointer and when you do radius->setNum(1);
, you are dereferencing nullptr which is instant crash.
Last edited on May 23, 2013 at 5:43am UTC
May 23, 2013 at 6:14am UTC
How to i create radius fraction?
May 23, 2013 at 8:29am UTC
Sorry, it's a long code so i cut it down.
The problem is not the <<
I accidentally took in out in here.