0

Here are the 3 classes. Fraction/Rectangle/Box

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;
}; 

//Program

#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;
} 

The problem occurs when i call this in 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

I put two fractions fPtrA into class Rectangle to make a rectangle I create another fraction

then i pass the Rectangle and the fraction into Box

I know the problem has to do with the default constructor i made. Please help :) I know i can use normal member data

2 Answers2

0

One problem is the constructors for Rectangle are dereferencing the Fraction pointers before they been initialized resulting in undefined behavior.

// len and wid have not been initialized and point to random points in memory
Rectangle::Rectangle()
{
    *len = FinalFractionRavuthL(2);
    *wid = FinalFractionRavuthL(1);
}

You need to create a Fraction object for len and wid.

Rectangle::Rectangle()
    : len(new Fraction()), wid(new Fraction())
{
    *len = FinalFractionRavuthL(2);
    *wid = FinalFractionRavuthL(1);
}

Rectangle::Rectangle(Fraction& frac, Fraction& frac1)
    : len(new Fraction()), wid(new Fraction())
{
    // ....
}

Since it doesn't appear a pointer to Fraction is really needed you can avoid the allocations with new by declaring the member variables with automatic storage duration. This will automatically initialize and destroy the objects without having to call new and delete

class Rectangle
{
public:
    // ....
private:
    Fraction len;
    Fraction wid;
};
Rectangle::Rectangle()
{
    len = FinalFractionRavuthL(2);
    wid = FinalFractionRavuthL(1);
}

Rectangle::Rectangle(Fraction& frac, Fraction& frac1)
{
    // ....
}
Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
0

Since you don't mention what the specific error is, one can only guess... But, since your class Box .... does not include a declaration for a constructor that takes two references (even though you seem to have supplied a definition for one outside the box), trying to call new Box(x, y) is going to fail because it can't find the right constructor to call. You should be getting errors on the definitions of the Box constructors that take arguments, as well, since they're not declared in the class Box body. There's a lot of other issues, too, but these are the ones specifically related to the line you have flagged.

twalberg
  • 59,951
  • 11
  • 89
  • 84