[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

C++ HELP!!!! OVERLOADING??

Posted on 1999-11-04
5
Medium Priority
?
223 Views
Last Modified: 2011-09-20
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.
0
Comment
Question by:CRAZY
5 Comments
 

Author Comment

by:CRAZY
ID: 2183606
Edited text of question.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2183680
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
 
LVL 2

Accepted Solution

by:
PIG earned 400 total points
ID: 2183807
I context of Your code You need implement operator [] for class Dsa. No more, only this.
0
 
LVL 1

Expert Comment

by:kentkt
ID: 2185171
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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2185353
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question