Implementation of Array Index operator for Fraction class

Hi ,

I need an implementation help for the array index operator overloading of my Fraction class . The Fraction.h looks like :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#include <iostream>	
using namespace std;

class Fraction {

public:

	
	Fraction(int = 1, int = 1);	
		
	Fraction(const Fraction&);	

	// DESTRUCTOR
	
	~Fraction();	

	// BASIC ASSIGNEMENT OPERATOR
	
	Fraction& operator=(const Fraction&);

// ARRAY INDEX OPERATOR
	
	Fraction const& operator[](unsigned int) const;	// Read-only access
	Fraction& operator[](unsigned int);		// Read/Write access:


private:

	// DATA MEMBERS
	int				iNumerator_;	// The Numerator
	unsigned int	uiDenominator_;	// The Denominator 
};



From main.cpp , it is called as :


1
2
3
4
5
6
7
8
9
10
11
// ARRAY INDEX OPERATOR
	
	// Fraction& operator[](unsigned int);
	Fraction f[2];
	f1 = Fraction(7, 18);
	f2 = Fraction(5, 12);
	f[0] = f1;
	f[1] = f2;
	cout << "[]: f[0] = " << f[0] << ". f[1] = " << f[1] << endl;




I have implemented the Fraction.cpp as :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "Fraction.h"


Fraction::Fraction(int n, int d):iNumerator_(n),uiDenominator_(d)
{
	cout << "Normal Constructor (2 Params): " << *this <<"\n";
}

// BASIC ASSIGNMENT OPERATOR 
Fraction& Fraction::operator= (const Fraction &rightSide)
{
	if(this != &rightSide) {
		iNumerator_ = rightSide.iNumerator_;
		uiDenominator_ = rightSide.uiDenominator_;
		}
	return *this;
}

// ARRAY INDEX OPERATOR
	// --------------------
// Read-only access
Fraction const& Fraction::operator[](unsigned int i) const{
  return  *this;
}
// Read/Write access:
//Fraction& Fraction::operator[](unsigned int i){
  //return *this; 
//}


I am not able write the Array index operator overloading functions, please help me .

thanks.
From your main.cpp :
1
2
3
4
5
6
7
// Fraction& operator[](unsigned int);
	Fraction f[2];
	f1 = Fraction(7, 18);
	f2 = Fraction(5, 12);
	f[0] = f1;
	f[1] = f2;
	cout << "[]: f[0] = " << f[0] << ". f[1] = " << f[1] << endl;


Note that in those assignments and cout statements, you don't actually call the
operator[]( unsigned int ) of Fraction, you are simply doing the old way of accessing
elements in an array.

The problem, maybe is you don't have an overloaded operator<< for fraction instead, so :
cout << "[]: f[0] = " << f[0] << ". f[1] = " << f[1] << endl;
will not work.

Here is the common implementation for operator <<

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

class Fraction
{
public :
    Fraction( int x = 0, int y = 0 ) : x_(x), y_(y) { }
    
    // declare as friend to be able to access private members of the class 
    friend std::ostream& operator << ( std::ostream& stream, const Fraction& fraction );
        
private :
    int x_, y_;
};

// NOTE :
// operator << is not a member function, it is simply a function that has access to private members
// of Fraction
std::ostream& operator << ( std::ostream& stream, const Fraction& fraction )
{ return stream << fraction.x_ << '/' << fraction.y_; } // accesing private member of fraction

int main ()
{
    Fraction fracs[ 4 ] { {3, 4}, {1, 2}, {9, 3}, {68, 37} };
    
    for( int i = 0; i < 4; ++i )
        std::cout << fracs[ i ] << std::endl; // using operator << we have defined
}



3/4
1/2
9/3
68/37


http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/

AFAIK operator[] is commonly used for classes w/c internally has an array or some sort of. inside it ( like vectors )
Last edited on
Hi ,

I am sorrry , I had operator << written, but i forgot to mention it here.
please find it below :
1
2
3
4
5
6
7
8
9
ostream& operator<<(ostream& os, const Fraction& f1)
{
	//cout << "Indirection ostream func" << endl ; 
	os << '(' << f1.iNumerator_ << ", " << f1.uiDenominator_ << ')';

	return os;
}



Please help me now on the [ ] operator overloading.
Why should a fraction be indexable like an array?
Fractions and arrays are two totally different things.
Topic archived. No new replies allowed.