Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I intentionally wrote the following code and it worked!
C++
#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;

class MaTrix
{
	private:
		int v[10][10],n,m;
	public: 
		friend void operator>>(istream& is, MaTrix &m);
		friend void operator<<(ostream& os, MaTrix m);
		void find_max();
};

//-------------------------------

void operator>>(istream& is, MaTrix &m)
{
	cout<<"Input rows = ";is>>m.n;
	cout<<"Input columns = ";is>>m.m;
	for(int i=0;i<m.n;i++)
		for(int j=0;j<m.m;j++)
		{
			cout<<"Input value v["<<i<<"]["<<j<<"]=";is>>m.v[i][j];
		}
	cout<<"Finish!"<<endl;
}

void operator<<(ostream& os,MaTrix m)
{
	int j;
	for(int i=0;i<m.n;i++)
	{
		for(j=0;j<m.m;j++)
			os<<setw(6)<<m.v[i][j];
		if(j==m.m) os<<endl;
	}
}

void MaTrix::find_max()
{
	int max=v[0][0];
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			if(v[i][j]>max) max=v[i][j];
	cout<<"Max = "<<max<<endl;
	system("pause");
}

int main()
{
	MaTrix mt;
	cout<<"Input a matrix"<<endl;cin>>mt;
	cout<<"The matrix is : "<<endl;
	cout<<mt;
	mt.find_max();
	return 0;
}


In my lesson the code is :
C++
istream& operator>>(istream& is, MaTrix &m)
{
	cout<<"Input rows = ";is>>m.n;
	cout<<"Input columns = ";is>>m.m;
	for(int i=0;i<m.n;i++)
		for(int j=0;j<m.m;j++)
		{
			cout<<"Input value v["<<i<<"]["<<j<<"]=";is>>m.v[i][j];
		}
	cout<<"Finish!"<<endl;
        return is;
}

ostream& operator<<(ostream& os,MaTrix m)
{
	int j;
	for(int i=0;i<m.n;i++)
	{
		for(j=0;j<m.m;j++)
			os<<setw(6)<<m.v[i][j];
		if(j==m.m) os<<endl;
	}
        return os;
}


Could you tell me how they different? And what is problem if I use the first code?
Posted
Comments
Philippe Mori 19-Dec-13 9:02am    
In addition to Solution 1, the prototype of the << operator should be: ostream& operator <<(ostream & os, const Matrix &) to avoid a copy of the object
Toan Pham Anh 9-Jan-14 10:28am    
yes, it will make the program run faster!

Its simple if you think about it. The difference is the return parameters, in C++, people like to write code like:

C++
cout << myvariable << mytext << endl;


Which is only possible if the << overloads on the members return an ostream object. If you return void, you will get a compilation error when you write the above line. You return the ostream object so that you can chain << operators.
 
Share this answer
 
Comments
Toan Pham Anh 17-Dec-13 10:20am    
Yes, I have not thought about that case. Thank you!
think bit cin>> and cout<< thats it is belong to #include<iostream.h> same concept.but if it is void then their is no any return type.reason is void mean no any return type.
 
Share this answer
 
Comments
Richard MacCutchan 17-Dec-13 12:05pm    
You may want to translate that into English.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900