Solved
C++ HELP!!!! OVERLOADING??
Posted on 1999-11-04
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.