Hello,
I am trying to learn C++ on my own.I came across this thread http://www.cplusplus.com/forum/beginner/3473/ and tried my hands on some of the problems.I was able to write initial code for ques no 25 there(i.e,Write a program using class to perform addition,subtraction and multiplication of matrices) and it compiles correctly.But when I tried to write the same code using header file matrix.h and matrix.cpp(as in Visual C++ if you add a class),I am having difficulties.
Can somebody please point out the errors(and possibly correction,even more,explanation :-)) in the code,I shall be thankful.
#include <iostream>
#include "matrix.h"
usingnamespace std;
int main()
{
int nX,nY;
cout<<"Enter the dimension of first matrix,i.e,A:\n No. of Rows :";
cin>>nX;
cout<<"\n No of Columns :";
cin>>nY;
matrix A(nX,nY);
A.input();
cout<<"\nEnter the dimension of second matrix,i.e,B:\n No of Rows :";
cin>>nX;
cout<<"\nNo of Columns :";
cin>>nY;
matrix B(nX,nY);
B.input();
cout<<"Choose what opeartions would you like to perform:\n1.Add: Gives A+B\n2.Subtract Gives A-B\n3.Multiply Gives A*B\n";
int nOption;
cin>>nOption;
if(nOption==1)
(A+B).out();
elseif(nOption==2)
(A-B).out();
elseif(nOption==3)
(A*B).out();
else
cout<<"Wrong choice,Please enter 1,2 or 3"<<endl;
return 0;
}
It shows a variety of errors while compiling in Visual C++ express.
What exactly is wrong with it? Does it print funky numbers? Are you receiving access violations or heap corruptions? You need to be more specific so we can isolate the problem (s).
Note that operators should return references to *this, not copies of local structure instantiations.
Honestly, I'm not reading your code because I haven't the foggiest. I'm not being aggressive or anything, I'm just saying that programmers want to know what they're looking for.
the code presented in Matrix.cpp (last code) was written by me originally.It compiles and produces desired output.
But when I tried to write it using separate header file and main file,it failed to compile.I think the problem is in "dividing" the single file in separate header files,etc as I am not proficient with that.
Some of the problems:
In line 35 of matrix.cpp(and similar lines for operator overload).I think it is wrong,but don't know right way to write.
Also,the variables defined in matrix.h,eg nRow,nColumn and mat are not being recognised.
Thanks
If anyone wants,i can copy the error log produced,but that would make this post less readable.
Lastly,I agree to the fact that my post is messy to look,and I apologise for that.But as I am new here,so am unaware if "Hide" can be used(in some forums,something like 'spoiler" or 'hide" is there)??
At a quick glance, I've noticed new was called, but delete wasn't. In short, you have multiple memory leaks. In addition, your matrix class doesn't have a destructor (well, it does, but in this case, not the want one you want). If a data member of class dynamically allocates a region of memory, the default destructor should be overloaded and should release that region of memory with the correct call to delete.
Also, the + operator should not actually modify the members of either operand. Instead, use the bitwise addition operator (+=). This should be the same with all operators.
Another important issue is with your header guards. The problem is that you have none. Your header should look like this:
I can't say any thing as bad practice in the above code (on the basis of what I know, which is little itself!), but you should have a read of this: http://www.cplusplus.com/articles/G8hv0pDG/