Reputation: 65
What is Problem Here. It is not working as an expected.
I want that << (insertion) Work For both cout and cin.
#include<iostream>
using namespace std;
class a
{
private:
string name;
int age;
unsigned long int salary;
public:
friend ostream& operator << (ostream& ,a );
friend istream& operator << (istream& ,a );
};
ostream& operator << (ostream& dout,a a1){
cout<<"Name = "<< a1.name<<"Age = "<<a1.age<<"Salary = "<<a1.salary<<end;
return dout;
}
istream& operator << (istream& din,a& a1){
cout<<"Enter Your Name , Age , Salary .....Press Enter To Seperate New Value"<<end;
cin>>a1.name>>a1.age>>a1.salary;
}
main(int argc, char const *argv[])
{
a a1;
cin<<a1;
cout<<a1;
return 0;
}
Error is too long. ->
Upvotes: 1
Views: 198
Reputation: 17428
I am not sure WHY you want to do this, but it is possible. Just remember that simply because you can do something does not mean that you SHOULD do that thing (see C++ Faq Law of Least Surprise.
Aside from violating the law of least surprise, you can do what you are trying to do, your code just has several simple compile errors in it that once fixed will work just fine (see here for working example).
Here are the changes to make it compile:
friend istream& operator << (istream& ,a& ); // Note the addition of the &
// Here the variabe is dout, so change to dout. I also added some spacing
ostream& operator << (ostream& dout,a a1){
dout<<"Name = "<< a1.name<<" Age = "<<a1.age<<" Salary = "<<a1.salary<<endl;
return dout;
}
// Here you are using din, so you need to change to din, also you had end instead of endl
istream& operator << (istream& din,a& a1){
cout<<"Enter Your Name , Age , Salary .....Press Enter To Seperate New Value"<<endl;
din>>a1.name>>a1.age>>a1.salary;
return din;
}
Just so the complete code is also in one easy place for you. Here is your entire program with the changes to make it compile.
#include<iostream>
using namespace std;
class a
{
private:
string name;
int age;
unsigned long int salary;
public:
friend ostream& operator << (ostream& ,a );
friend istream& operator << (istream& ,a& );
};
ostream& operator << (ostream& dout,a a1){
dout << "Name = "<< a1.name <<" Age = "<< a1.age <<" Salary = "<< a1.salary << endl;
return dout;
}
istream& operator << (istream& din,a& a1){
cout <<"Enter Your Name , Age , Salary .....Press Enter To Seperate New Value" << endl;
din >> a1.name >> a1.age >> a1.salary;
return din;
}
main(int argc, char const *argv[])
{
a a1;
cin<<a1;
cout<<a1;
return 0;
}
Now, if we want to follow the Law of Least surprise, then we would change the istream
operator overload to use >>
instead of <<
and move the console text out of the >>
operator overload and just present it to the user before reading the values.
#include<iostream>
using namespace std;
class a
{
private:
string name;
int age;
unsigned long int salary;
public:
friend ostream& operator << (ostream& ,a );
friend istream& operator >> (istream& ,a& );
};
// Note - Changed variable 'dout' to 'os' for clarity
ostream& operator << (ostream& os, a a1){
os << "Name = " << a1.name << " Age = " << a1.age << " Salary = "<< a1.salary << endl;
return os;
}
// Changed variable from 'din' to 'is' for clarity
istream& operator >> (istream& is,a& a1){
is >> a1.name >> a1.age >> a1.salary;
return is;
}
main(int argc, char const *argv[])
{
a a1;
cout << "Enter Your Name , Age , Salary .....Press Enter To Seperate New Value" << endl;
cin >> a1;
cout << a1;
return 0;
}
Upvotes: 1