C++ HELP!!!! OVERLOADING??

I am trying to create a class, Dsa, which should be able to create an array of any number of rows and any number of columns. it has to have operator(), to perform doublesubscripting operations. For example, in a 3 by 5 Dsa called a, the user could write any number of arguments. The underlying representation of the double-subscripted array should be a single subscripted array of integers with (rows*columns) number of elements. Function operator should perform the proper pointer arithmetic to access each element of the array. There should be 2 versions of operator(), one that returns const int &, so an element of a const Dsa can be used as an rvalue. The class should also provide the following operators:

==, !=, =, << (for outputting the array in row and column format) and >> (for inputting the array contents).


so far this is what I have, but I am pulling hair out trying to figure out the idea behind function overloading...


//Double Scripted Array (Dsa)
//assert.h not used yet

#include<iostream.h>
#include<assert.h>

class Dsa {

        friend ostream &operator<<(ostream &, Dsa &);
        friend istream &operator>>(istream &, Dsa &);

    public:
        Dsa( int = 3, int = 5);         //constructor


    private:
        int size;               //Size of the array
        int *ptr;               //pointer to first element of the array
};

//Constructor for class DSA default 3-by-5 array
Dsa::Dsa(int r, int c)
{
        int arraysize = r*c;
        size = (arraysize > 0 ? arraysize : 10);
        ptr = new int[ size ];


        for (int i = 0; i < size; i++)

                        ptr[i] = 0;             //initialize array

}

int main()
{
        Dsa r( 2 , 3 );

//for and cout commented out untill I can understand how to implement them.
        //for(int i = 0; i < 6; i++)
                        //cout<<r[i];


return 0;
}





This program compiles but has no output, and I also am not sure where to go from here. I want to minimize the complication of this problem and also understand what overloading is and why it must be used here. hopefully someone can help.
Thanks in advance.
CRAZYAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CRAZYAuthor Commented:
Edited text of question.
0
KangaRooCommented:
You have not given us the implementation of
  Dsa::operator()(int, int)
which is supposed to do the subscripting. I suspect you did not understand that part of the assignment.
Secondly it would be wise to store the dimensions of the wanted array (at least the row size). Without that you won't be able to implemented operator()(int, int)
Thirdly, although correct, the declaration of Dsa::Dsa(int, int) is obscure. Rather use Dsa::Dsa(int rows = 3, int columns = 5). In other words, name your parameters even if this is not required.
0
PIGCommented:
I context of Your code You need implement operator [] for class Dsa. No more, only this.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kentktCommented:
CRAZY,

Maybe you don't even know how to declare the operator? Incorporating Kangaroo's suggestion:

class Dsa
{
    friend ostream &operator<<(ostream &, Dsa &);
    friend istream &operator>>(istream &, Dsa &); public:
public:
    Dsa(int r=3, int c=3); // constructor
    const int& operator()(int r, int c) const;
    int& operator()(int r, int c);
private:
    int rows;               //no. of rows
    int columns;         // no. of columns
    intt *ptr;               //pointer to first element of the array
};

Note that "const"s of the two operator() versions. They allow the elments to be accessed in two directions, as rvalue and lvalue.

Implementing operator[], as suggested by PIG, is not useful because this operator accepts only number but you require two parameters for a 2-D indexed array.

Hope this helps.
0
KangaRooCommented:
In C++ you can define (overload) the common operators, like +, -, *, /, << etc... for your own classes. It happens that brackets like () and [] are considered operators, which can be overloaded.
For a two dimensional array class one could create a memberfunction that returns an element:
  Array2D array;
  array.Element(3,4) = 45;
But this looks nicer if we could write:
  Array2D array;
  array(3,4) = 45;
The latter is achieved by overloading operator()

Now the const. The subscripting function should have two forms, one returning a const int& (an rvalue) and one returning a reference to the int element in the array (an lvalue).
Something like:
1.:  const int& Array2D::operator()(int, int);
2.:  int&          Array2D::operator()(int,int);

But now we have a problem, how does the compiler know which version to use? We can tell it by adding the const modifier to the member function

1.:  const int& Array2D::operator()(int, int) const;
2.:  int&         Array2D::operator()(int,int);

Now the compiler can tell which function to use.

 
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.