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);
~Circle();
void print(void);
protected:
Fraction *radius;
};
和
#include "Fraction.h"
#include "Circle.h"
#include <iostream>
using namespace std;
Fraction::Fraction() {
num = 0;
denom = 1;
}
Fraction::Fraction(int n, int d) {
num = n;
denom = d;
}
Fraction& Fraction::operator=(const Fraction& arg) {
num = arg.num;
denom = arg.denom;
return *this;
}
Fraction::~Fraction() {
}
//Circle
Circle::Circle() {
radius->setNum(1);
radius->setDenom(1);
}
Circle::Circle(Fraction& arg1) {
radius->setNum(arg1.getNum());
radius->setDenom(arg1.getDenom());
}
Circle::~Circle() {
delete this->radius;
}
当我在 main() 中创建对象时,问题就来了
Fraction* fPtr = new Fraction(4, 1);
Circle* cPtrA = new Circle(*fPtr);
这两个都是类。首先,我使用复制构造函数将 classFraction 的 Numerator 设置为 4,将 Denominator 设置为 1。
然后我想将 (4, 1) 传递到我的 classCircle 但我通过调试器收到未处理异常的错误