这是3个班。分数/矩形/方框
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 Rectangle {
public:
FinalRectangleRavuthL();
~FinalRectangleRavuthL();
FinalRectangleRavuthL(Fraction&, Fraction&);
void setLen(Fraction arg);
void setWid(Fraction arg);
Fraction getLen(void) const;
Fraction getWid(void) const;
friend ostream& operator<<(ostream&, Rectangle&);
private:
Fraction *len;
Fraction *wid;
};
class Box : public Rectangle {
public:
Box();
FinalBoxRavuthL(Rectangle& arg, Fraction& arg1);
FinalBoxRavuthL(Fraction& arg);
void print(void);
void getBox(void);
~FinalBoxRavuthL();
private:
Fraction *height;
};
//程序
#include "Fraction.h"
#include "Circle.h"
#include "Box.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() {
}
//Rectangle
Rectangle::Rectangle() {
*len = FinalFractionRavuthL(2);
*wid = FinalFractionRavuthL(1);
}
Rectangle::RectangleFraction& frac, Fraction& frac1) {
cout << "First Fraction : ";
frac.print();
cout << "Second Fraction : ";
frac1.print();
}
void Rectangle::setLen(Fraction arg) {
len->setNum(arg.getNum());
len->setDenom(arg.getDenom());
}
void Rectangle::setWid(Fraction arg) {
wid->setNum(arg.getNum());
wid->setDenom(arg.getDenom());
}
Fraction Rectangle::getLen(void) const {
return Fraction(len->getNum(), len->getDenom());
}
Fraction Rectangle::getWid(void) const {
return Fraction(wid->getNum(), wid->getDenom());
}
Rectangle::~Rectangle() {
delete this->len;
delete this->wid;
}
//Box
Box::Box() : height(new Fraction())
{
height->setNum(0);
height->setDenom(0);
}
Box::Box(Rectangle& arg, Fraction& arg1) : height(new Fraction())
{
arg.getLen().print();
arg.getWid().print();
cout << arg1.getNum();
cout << arg1.getDenom();
}
Box::Box(Fractio& arg) {
height->setNum(arg.getNum());
height->setDenom(arg.getDenom());
}
Box::~Box() {
delete this->height;
}
当我在 main 中调用它时会出现问题
Fraction* fPtrA = new Fraction(4, 1);
Rectangle* rPtrA = new Rectangle(*fPtrA, *fPtrA);
Fraction* fPtrD = new Fraction(9, 1);
BoxRavuthL* boxPtrA = new Box(*rPtrA, *fPtrD); //PROBLEM
我将两个分数 fPtrA 放入类 Rectangle 中以创建一个矩形我创建另一个分数
然后我将 Rectangle 和分数传递给 Box
我知道问题与我制作的默认构造函数有关。请帮助:) 我知道我可以使用普通会员数据