# Thread: what am i doing wrong with inheritance?

1. ## what am i doing wrong with inheritance?

Code:
```#include <iostream>
using namespace std;

class Fraction
{
protected:
int num;
int denom;
public:
Fraction() : num(1), denom (1)
{  }
Fraction(int top, int bottom): num(top), denom(bottom)
{
//check for divide by zero error
if (denom == 0)
{
cout << "Divide by zero error, setting denominator equal to 1" << endl;
denom = 1;
}
}
int reduce(int p, int q)
{
int r;
return ((r=p%q)==0)? q:reduce(q,r);
}
void showdata()
{
int gcd;
gcd = reduce(num,denom);

num /= gcd;
denom /= gcd;
if (num != 0)
cout << num << "/" << denom << endl;

}

Fraction operator + (Fraction)const;

Fraction operator - (Fraction f2) const;

Fraction operator * (Fraction f2) const;

Fraction operator / (Fraction f2) const;

};

class Mixed : public Fraction
{
private:
int whole;
public:
Mixed(int w, int t, int b): whole(w), Fraction(t, b)

};

Fraction Fraction::operator + (Fraction f2) const
{
int numtemp, denomtemp;
//Fraction temp;
numtemp = (num*f2.denom) + (denom * f2.num);
denomtemp = (denom * f2.denom);
return Fraction(numtemp, denomtemp);
}

Fraction Fraction :: operator - (Fraction f2) const
{
int numtemp, denomtemp;
numtemp = (num*f2.denom) - (denom * f2.num);
denomtemp = (denom * f2.denom);
return Fraction(numtemp, denomtemp);
}

Fraction Fraction :: operator * (Fraction f2) const
{
int numtemp, denomtemp;
numtemp = (num * f2.num);
denomtemp = (denom * f2.denom);
return Fraction(numtemp, denomtemp);
}

Fraction Fraction :: operator / (Fraction f2) const
{
int numtemp, denomtemp;
numtemp = (num * f2.denom);
denomtemp = (denom * f2.num);
return Fraction(numtemp, denomtemp);
}

int main()
{

//Fraction fract2 (1,0);
Mixed m1(1,3,4);

return 0;
}```

without the inheritance mixed.. EVREYTHING works perfectly.. even witht he interited class without the public part, it compiles with no problem. its only when i add the public part im getting a problem. does it have to do with the constructor?
can u have a constructor in an inherited class?

2. im stupid
i realized i didnt have the brackets after the constructor

3. The problem is operator +. You will need to provide an operator + for the mixed class that tells it how to add mixed objects together.
You have also broken a golden rule of c++. that rule is Never inherit from a concrete class. And this is why.....

Is a Mixed really a Fraction?

Answer that with yes it is or no it has a fraction.

If you answer has a, then how do you model that relationship in c++.

4. Originally posted by Stoned_Coder
The problem is operator +. You will need to provide an operator + for the mixed class that tells it how to add mixed objects together.
You have also broken a golden rule of c++. that rule is Never inherit from a concrete class. And this is why.....

Is a Mixed really a Fraction?

Answer that with yes it is or no it has a fraction.

If you answer has a, then how do you model that relationship in c++.
1. i havent done any of the overloaded operators for the mixed class yet.. i was just testing inheritance.

2. well, it wouldnt have been my first choice to do that, but its a requirement of the test... that a mixed number is a inherited class from fraction.

5. "well, it wouldnt have been my first choice to do that, but its a requirement of the test... that a mixed number is a inherited class from fraction."

So, you're cheating on a test?

6. how exactly was i cheating?

it was a take home test...