Thread: what am i doing wrong with inheritance?

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    221

    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;
    
    		}
    		
    		// overloaded addition
    		Fraction operator + (Fraction)const;
    
    		//overloaded minus
    		Fraction operator - (Fraction f2) const;
    
    	
    		//overloaded multiplication
    		Fraction operator * (Fraction f2) const;
    
    	
    		//overloaded division
    		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)
    
    
    }; 
    
    
    // Actual overloaded fraction definitions
    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 fract1, answer;
    	//Fraction fract2 (1,0);
    	Mixed m1(1,3,4);
    
    	//answer = fract1 + fract2;
    	//m1.showwhole(); answer.showdata();
    	//answer = fract1 - fract2;
    	//answer.showdata();
    	//answer = fract1 * fract2;
    	//answer.showdata();
    	//answer = fract1 / fract2;
    	//answer.showdata();
    	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. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    im stupid
    i realized i didnt have the brackets after the constructor

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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++.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    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. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "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. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    how exactly was i cheating?

    it was a take home test...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Virtual inheritance
    By 6tr6tr in forum C++ Programming
    Replies: 13
    Last Post: 05-07-2008, 11:20 AM
  3. Forms of Inheritance
    By kolucoms6 in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2008, 03:09 PM
  4. Replies: 11
    Last Post: 03-11-2004, 04:17 PM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM