0

i hav coded a matrix class and its member functions (many of them are overloaded ones)...now in a function..for eg. function of matrix addition.. **after the addition i want to return the local object "result" through a pointer (and not call by value)..however i aint getting d values of the result in main()...instead i am getting garbage values when i print it...**

here is my code :

```
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
// general function declarations
void list (int *choice);
// class declaration
[B]class matrix
{
private :
int rows;
int columns;
int * element;
public :
matrix();
matrix (int, int);
void setdata (int, int, int *);
void showptrdata();
matrix* operator+(matrix &);
friend istream& operator >> (istream &, matrix &);
friend ostream& operator << (ostream &, matrix &);
// function overloading -- for printing a pointer object
friend ostream& operator << (ostream &, matrix *&);
};[/B]
matrix :: matrix ()
{
rows = 0;
columns = 0;
*element = 0;
}
matrix :: matrix (int r ,int c)
{
rows = r;
columns = c;
element = new int [rows * columns];
}
istream& operator >> (istream &read, matrix &mat)
{
for (int i = 0; i < mat.rows * mat.columns; ++i)
read >> *(mat.element+i);
return read;
}
[B]ostream& operator << (ostream &print, matrix &mat)
{
for (int i = 0; i < mat.rows; ++i)
{
for (int j = 0; j < mat.columns; ++j)
{
print << *mat.element << " ";
++mat.element;
}
print << endl;
}
mat.element -= mat.rows * mat.columns;
return print;
}
ostream& operator << (ostream &print, matrix *&mat)
{
for (int i = 0; i < mat->rows; ++i)
{
for (int j = 0; j < mat->columns; ++j)
{
print << *mat->element << " ";
++mat->element;
}
print << endl;
}
mat->element -= mat->rows * mat->columns;
return print;
}
[/B]
[B]matrix* matrix :: operator + (matrix &matrix2)
{
matrix result (rows, columns);
for (int i=0; i< rows * columns; ++i)
*(result.element+i) = *(element+i) + *(matrix2.element+i);
//printf ("%d %d\n", result.rows, result.columns);
//printf ("%u %u %u \n", &result, &result.rows, &result.columns);
return &result;
}
[/B]
void main()
{
clrscr();
int r, c;
int choice;
cout << "Enter the rows of the matrix : ";
cin >> r;
cout << "Enter the columns of the matrix : ";
cin >> c;
matrix matrix1 (r, c);
cout << "Enter the elements rowwise : " << endl;
cin >> matrix1;
cout << "You have created the following matrix : " << endl;
cout << matrix1;
cin.ignore();
cin.get();
do
{
list (&choice);
switch (choice)
{
case 1 :
cout << endl << endl << "The matrix is as follows : " << endl;
cout << matrix1;
break;
case 2 :
cout << "Enter the new values of the matrix : " << endl;
cin >> matrix1;
cout << "The modified matrix is : " << endl;
cout << matrix1;
break;
case 3 :
[B] matrix matrix2(r,c);
cout << "Enter the elements of the second matrix : " << endl;
cin >> matrix2;
matrix *result;
result = matrix1 + matrix2;
//printf ("%u\n", result);
//printf ("%u %u\n", &result->rows, &result->columns);
//printf ("%d %d\n", result->rows, result->columns);
cout << "The resultant matrix is : " << endl;
cout << result;[/B]
break;
case 4 :
break;
case 5 :
break;
case 6 :
break;
case 0 :
exit(0);
}
cin.ignore();
cin.get();
} while (choice != 0);
}
void list (int *choice)
{
clrscr();
cout << "1. Display the matrix." << endl << "2. Modify the matrix." << endl
<< "3. Matrix Addition." << endl << "4. Matrix subtraction." << endl
<< "5. Matrix multiplication." << endl
<< "6. Transpose of matrix." << endl << "0.Exit." << endl;
cin >> *choice;
cin.ignore();
if (!(*choice >= 0 && *choice <= 6))
{
cout << "Invalid choice." << endl;
cin.get();
list (choice);
}
}
```

i have given the bold effect to the code that i felt is related to my problem..so..pls help me with this problem..

and yes..one more question..**is there any way to return this "result" by a reference ??? i know it may seem absurd**...bcoz of the scope of the local variable...still i would like to learn a way if any... :)