Discussion:
[OT]problem in c++
siddharth_waikar
2006-01-24 18:03:30 UTC
Permalink
'int' is a inbuilt class
int i,j,k=i+j;
above statement is works for int class
similarly, I have written my own class 'Matrix'
but my class 'Matrix' is not working.
I gets ERROR for that code.

this is my code

#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h>
class Matrix
{
int *p,m,n;
public :
Matrix(int r=2,int c=2)
{ m=r; n=c;
p = new int [m*n];
for(int i=0,j;i<m;i++)
for(j=0;j<n;*(p+(i*n+j))=0,j++);
}
void accept();
void display();
~Matrix() { delete []p; }
Matrix (Matrix &Tm)
{ m=Tm.m; n=Tm.n;
p=new int [m*n];
for(int i=0,j;i<m;i++)
for(j=0;j<n;j++)
*(p+(i*n+j))=*(Tm.p+(i*n+j)); }

void operator =(Matrix );
Matrix operator +(Matrix &);
};

void Matrix :: accept()
{
cout << "(" << m << "X" << n << "):\n";
for(int i=0,j;i<m;i++)
for(j=0;j<n;j++)
cin >> *(p+(i*n+j));
}

void Matrix :: display()
{
for(int i=0,j;i<m;cout << endl,i++)
for(j=0;j<n;j++)
cout << setw(5) <<*(p+(i*n+j));
}

void Matrix :: operator =(Matrix Tm)
{
for(int i=0,j;i<m;i++)
for(j=0;j<n;j++)
*(p+(i*n+j))=*(Tm.p+(i*n+j));
}

Matrix Matrix :: operator +(Matrix &Tmp)
{
Matrix Tm3(m,n);
if(m==Tmp.m && n==Tmp.n)
{ for(int i=0,j;i<m;i++)
for(j=0;j<n;j++)
*(Tm3.p+(i*n+j))=*(p+(i*n+j))+*(Tmp.p+(i*n+j));
}
else cout << endl <<"Can't ADD!\n";
return Tm3;
}

int main()
{
int m,n;
cout << endl << "Enter Order of matrix A:";
cin >> m >> n;
Matrix A(m,n);
cout << endl << "Enter Order of matrix B:";
cin >> m >> n;
Matrix B(m,n);
cout << endl <<"Enter matrix A";
A.accept();
cout << endl <<"Enter matrix B";
B.accept();
cout << endl <<"Matrix A\n";
A.display();
cout << endl <<"Matrix B\n";
B.display();
Matrix C=A+B;
cout << endl <<"Matrix C(A+B)\n";
C.display();
return 0;
}










Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/bcslug/

<*> To unsubscribe from this group, send an email to:
bcslug-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Archis Gore
2006-01-24 19:30:27 UTC
Permalink
Inline responses..........
Post by siddharth_waikar
'int' is a inbuilt class
int i,j,k=i+j;
above statement is works for int class
I wouldn't know whether 'int' can really be called a
class. Technically it's called a primitive since it
has different behaviour and characteristics as
compared to a class. Smalltalk fans may have a more
verbose explanation for this.
Post by siddharth_waikar
similarly, I have written my own class 'Matrix'
but my class 'Matrix' is not working.
I gets ERROR for that code.
<snip/>

I dont know what I did, but it seems to work in the
following way in MSVC++ (how ironic!). I tried
compiling your code and got a bunch of unreasonable
errors that I didn't try to analyse much. But this
seems to work. I've tried it on cygwin also, so I hope
it'll work on Linux with few modifications.

First, I broke up your code into three separate files.
The "main.cpp" file is: (since Sriram has recently
disabled attachments :-))

#include "Matrix.h"

int main()
{
int m,n;
cout << endl << "Enter Order of matrix A:";
cin >> m >> n;
Matrix A(m,n);

cout << endl << "Enter Order of matrix B:";
cin >> m >> n;
Matrix B(m,n);

cout << endl <<"Enter matrix A";
A.accept();
cout << endl <<"Enter matrix B";
B.accept();
cout << endl <<"Matrix A\n";
A.display();
cout << endl <<"Matrix B\n";
B.display();

Matrix C=A+B;

cout << endl <<"Matrix C(A+B)\n";
C.display();
return 0;
}


Then, the file "Matrix.h" contains:

// Matrix.h: interface for the Matrix class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(MATRIX_HEADER_INCLUDED)
#define MATRIX_HEADER_INCLUDED

#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>



class Matrix
{

int *p,m,n;

public :


Matrix(int r=2,int c=2)
{ m=r; n=c;
p = new int [m*n];
for(int i=0,j;i<m;i++)
for(j=0;j<n;*(p+(i*n+j))=0,j++);
}

void accept();
void display();

~Matrix() { delete []p; }

Matrix (Matrix &Tm)
{ m=Tm.m; n=Tm.n;
p=new int [m*n];
for(int i=0,j;i<m;i++)
for(j=0;j<n;j++)
*(p+(i*n+j))=*(Tm.p+(i*n+j));
}

void operator =(Matrix );

Matrix operator +(Matrix &);


};

#endif // !defined(MATRIX_HEADER_INCLUDED)


Finally, Matrix.cpp contains:

// Matrix.cpp: implementation of the Matrix class.
//
//////////////////////////////////////////////////////////////////////

#include "Matrix.h"


void Matrix :: accept()
{
cout << "(" << m << "X" << n << "):\n";
for(int i=0,j;i<m;i++)
for(j=0;j<n;j++)
cin >> *(p+(i*n+j));
}

void Matrix :: display()
{
for(int i=0,j;i<m;cout << endl,i++)
for(j=0;j<n;j++)
cout << setw(5) <<*(p+(i*n+j));
}

void Matrix :: operator =(Matrix Tm)
{
for(int i=0,j;i<m;i++)
for(j=0;j<n;j++)
*(p+(i*n+j))=*(Tm.p+(i*n+j));
}

Matrix Matrix :: operator +(Matrix &Tmp)
{
Matrix Tm3(m,n);
if(m==Tmp.m && n==Tmp.n)
{ for(int i=0,j;i<m;i++)
for(j=0;j<n;j++)

*(Tm3.p+(i*n+j))=*(p+(i*n+j))+*(Tmp.p+(i*n+j));
}
else cout << endl <<"Can't ADD!\n";
return Tm3;
}



Let me know if it works. Hopefully, I'll be doing a
5-hour boot into Linux tonight during which I'll
surely try to run it there (I dont have jdk1.5 for
linux and my bandwidth sucks so I'm forced on windows
for a few days).

Sincerely,
Archis

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com



Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/bcslug/

<*> To unsubscribe from this group, send an email to:
bcslug-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/

Loading...