Trouble with private portion of Classes

This is a discussion on Trouble with private portion of Classes within the C++ Programming forums, part of the General Programming Boards category; Ok, so I'm working with classes and its been a little while. I basically have to add a new private ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    5

    Trouble with private portion of Classes

    Ok, so I'm working with classes and its been a little while. I basically have to add a new private definition to a program I have already written and change the rest of the program to fit the new definition.
    Here is the whole assignment for reference:http://cs.angelo.edu/~mmotl/2305/labs/lab02.pdf
    Here is the private portion and the only thing I must add:
    Code:
    private:
    long r[2]  //r[0] represents numerator
               //r[1] represents denominator
    Everything else in the assignment is done correctly because i got 100% on it when I turned it in. I just need help on implementing the long r[2] in.

    And here is what I have as of now:
    Code:
    #include <iostream>
    #include <lab02.h>
    
    using namespace std;
    
    Rational::Rational()
    {
      SetNumerator(0);
      SetDenominator(1);
    }
    
    Rational::Rational(long num, long denom)
    {
      SetNumerator(num);
      SetDenominator(denom);
    }
    
    void Rational::SetNumerator(long num)
    {
      r[0]=num;
    }
    
    void Rational::SetDenominator(long denom)
    {
      if(denom==0)
        r[1]=1;
        
      else
        r[1]=denom;
    }
    
    long Rational::GetNumerator() const
    {
      return r[0];
    }
    
    long Rational::GetDenominator() const
    {
      return r[1];
    }
    
    void Rational::Reduce()
    {
      if(GetDenominator()<0)
        {
          SetNumerator(-r[0]);
          SetDenominator(-r[1]);
        }
        
        int gcd=GCD(GetNumerator(), GetDenominator());
        
        SetNumerator(GetNumerator()/gcd);
        SetDenominator(GetDenominator()/gcd);
    }
    
    long Rational::r[2]  //????
    {                          // <-error on this line  (invalid function declaration)
      numerator=r[0];  //????
      denominator=r[1];  //????
    }
    
    long Rational::GCD(long m, long n) const
    {
      int rem;
      
      if(m==0 && n==0)
        return 0;
      else if(m==0)
        return labs(n);
      else if(n==0)
        return labs(m);
      
      else
      {
        m=labs(m);
        n=labs(n);
        
        rem=m%n;
        
        if(rem==0)
          return n;
        else
        {
          while(rem!=0)
          {
            m=n;
            n=rem;
            rem=m%n;
            
            if(rem==0)
              return n;
          }
        }
      }
      return 1;
    }
    This was a program that I had already written and you can see that I changed all the instances that used to say numerator to r[0] and all the instances of denominator to r[1]. I don't know where I'm going wrong and why I'm getting that error. There are no tutors here right now either so any help would be appreciated.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,535
    You are trying to define a function called r[2]. That is an invalid name for a function and consequently not possible.

    You probably will want to move those assignments inside the invalid "function" into your class constructor... I don't know... would need to see your class definition.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You also don't need to use Get... and Set... methods inside other class functions. Your class already has direct access to its private data - how else would the getters and setters work.

    I'm not too sure how much point there is using an array instead of simply two long values: one for numerator, one for denominator. You probably are not going to use the array in loops?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    Quote Originally Posted by anon
    You also don't need to use Get... and Set... methods inside other class functions. Your class already has direct access to its private data - how else would the getters and setters work.

    I'm not too sure how much point there is using an array instead of simply two long values: one for numerator, one for denominator. You probably are not going to use the array in loops?
    The professor was just teaching us other ways about doing things. The original assignment sheet told us to use the Get and Set functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Classes: Use, Misuse...Confusion.
    By Snorpy_Py in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2006, 02:46 AM
  2. IRC, reading the stream
    By Iyouboushi in forum C# Programming
    Replies: 6
    Last Post: 08-03-2006, 06:34 PM
  3. information from download
    By miranda in forum C# Programming
    Replies: 3
    Last Post: 01-01-2006, 07:48 PM
  4. Having Trouble Understanding Classes
    By prog-bman in forum C++ Programming
    Replies: 1
    Last Post: 05-19-2004, 01:44 PM
  5. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21