Input/Output Operators Overloading in C++ - c++

Stuck with ostream/istream operator overloading
Q1. why we are using ostream& operator as friend?
Q2. Why we are passing two arguments in ostream & operator << (ostream &out, const Complex &c)
Q3. why we are referencing to Cout and in ? istream & operator >> (istream &in, Complex &c).
Ref to: Overloading stream Oerator overloading- Geeks4Geeks
HERE IS THE CODE
#include <iostream>
using namespace std;
class Complex
{
private:
int real, imag;
public:
Complex(int r = 0, int i =0)
{ real = r; imag = i; }
friend ostream & operator << (ostream &out, const Complex &c);
friend istream & operator >> (istream &in, Complex &c);
};
ostream & operator << (ostream &out, const Complex &c)
{
out << c.real;
out << "+i" << c.imag << endl;
return out;
}
istream & operator >> (istream &in, Complex &c)
{
cout << "Enter Real Part ";
in >> c.real;
cout << "Enter Imaginary Part ";
in >> c.imag;
return in;
}
int main()
{
Complex c1;
cin >> c1;
cout << "The complex object is ";
cout << c1;
return 0;
}

A1. To access the private members
A2. The first argument is the stream and the second is the object. operator<< and operator>> expect two arguments
A3. Because they are modified in the function. The functions read from resp. write into the stream.
In addition:
Don't use using namespace std;
Don't initialize members in the constructor body. Use the constructor initializer list
Complex(int r = 0, int i =0)
{ real = r; imag = i; }
should be
Complex(int r = 0, int i = 0) : real(r), imag(i) {}

Related

How to rectify private error while using Friend Function in C++?

Getting this error while using friend function in C++ : error: ‘int complex::a’ is private within this context. How will I rectify this error? I have created one complex class and while learning a friend function, i get to know that friend function can access private member functions too. But in this code, this error pops out. Thanks in advance.
#include <iostream>
using namespace std;
class complex{
private:
int a, b;
public:
void setNumber(int x,int y){a=x;b=y;}
void getNumber(){cout << "\n a="<< a << "b=" << b; }
friend ostream& operator <<(ostream&, complex);
friend istream& operator >>(istream&, complex&);
};
ostream& operator <<(ostream &dout, complex c){
cout << "a=" << c.a;
cout << "b=" << c.b;
return (dout);
}
istream& operator <<(istream &din, complex &c){
cin>>c.a>>c.b;
return (din);
}
int main(){
complex c1;
cin >> c1;
cout << c1;
return 0;
}
change this
ostream& operator <<(ostream &dout, complex c)
{
cout << "a=" << c.a;
cout << "b=" << c.b;
return (dout);
}
istream& operator <<(istream &din, complex &c)
{
cin>>c.a>>c.b;
return (din);
}
to
ostream& operator <<(ostream &dout, const complex& c)
{
dout << "a=" << c.a;
dout << "b=" << c.b;
return (dout);
}
istream& operator >>(istream &din, complex &c)
{
din>>c.a>>c.b;
return (din);
}

how to use std in code without including "using namespace std"?

In this code of operator Overloading, i don't want to write "using namespace std" instead i want to include "std::" wherever required.
After adding "std::" after cout and cin, i am still getting Errors where else to include "std::".
#include<iostream>
//using namespace std;
class Complex
{
private:
int real, imag;
public:
Complex(int r = 0, int i = 0) : real(r), imag(i) {}
friend ostream & operator << (ostream &, const Complex &);
friend istream & operator >> (istream &, Complex &);
};
ostream & operator << (ostream &out, Complex &obj)
{
out<<obj.real<<" "<<obj.imag;
return out;
}
istream & operator >> (istream &in, const Complex &obj)
{
in>>obj.real>>obj.imag;
return in;
}
int main()
{
Complex obj;
std::cin>>obj;
std::cout<<obj;
return 0;
}
It should take input two numbers using istream operator and output two numbers using ostream operator.
add std:: to ostream and istream
They come from the headers <istream> and <ostream> and are defined in <iosfwd>
#include<iostream>
//using namespace std;
class Complex
{
private:
int real, imag;
public:
Complex(int r = 0, int i = 0) : real(r), imag(i) {}
friend std::ostream& operator<<(std::ostream& out, const Complex& obj);
friend std::istream& operator>>(std::istream& in, Complex& obj);
};
std::ostream& operator<<(std::ostream &out, const Complex &obj)
{
out << obj.real << " " << obj.imag;
return out;
}
std::istream& operator>>(std::istream &in, Complex &obj)
{
in >> obj.real >> obj.imag;
return in;
}
int main()
{
Complex obj;
std::cin >> obj;
std::cout << obj;
return 0;
}
(not related to the std:: problem)
You can also access your private variables outside the class without the friend declaration by using get/set member functions. Thanks to #aschepler for pointing out my mistake regarding the accessibility.
#include<iostream>
class Complex
{
private:
int real, imag;
public:
int get_real() const {
return real;
}
void set_real(int real) {
this->real = real;
}
int get_imag() const {
return imag;
}
void set_imag(int imag) {
this->imag = imag;
}
Complex(int r = 0, int i = 0) : real(r), imag(i) {}
};
std::ostream& operator<<(std::ostream &out, const Complex &obj)
{
out << obj.get_real() << " " << obj.get_real();
return out;
}
std::istream& operator>>(std::istream &in, Complex &obj)
{
int real, imag;
in >> real >> imag;
obj.set_real(real);
obj.set_imag(imag);
return in;
}
int main()
{
Complex obj;
std::cin >> obj;
std::cout << obj;
return 0;
}
Your favourite standard library reference tells you what namespace things are in. In a tiny program like this, you can simply look up each one in turn.
Hint: they're all in std.
So that includes std::ostream and std::istream.
This is just a namespace pollution problem. The importance of it may vary between usages.
When you are just prototyping, using namespace std; is fine, as is including useless headers just in case you need something from one. When you want a super safe code that will be extensively reviewed, you want to prevent name collision and namespace pollutino, so you include in the current namespace only what is required, and you give explicit namepaces for idendifiers that are only seldom used.
The following is more of my own opinion (or more exactly the way I am used to work):
when a symbol is seldom used, I give it its explicit namespace (eg: std::cout << i;)
when a symbol is heavily used in a compilation unit, I import specifically that symbol (eg: using std::cout; ... cout << i; ... cout << j; ...)

Overloading input/output operators in C++

#include <iostream>
using namespace std;
class Complex
{
private:
int real, imag;
public:
Complex(int r = 0, int i =0)
{ real = r; imag = i; }
**friend ostream & operator << (ostream &out, const Complex &c);
friend istream & operator >> (istream &in, Complex &c);**
};
ostream & operator << (ostream &out, const Complex &c)
{
out << c.real;
out << "+i" << c.imag << endl;
return out;
}
istream & operator >> (istream &in, Complex &c)
{
cout << "Enter Real Part ";
in >> c.real;
cout << "Enter Imagenory Part ";
in >> c.imag;
return in;
}
int main()
{
Complex c1;
cin >> c1;
cout << "The complex object is ";
cout << c1;
return 0;
}
What is the use of passing the operator as a reference "& operator".
When we pass a normal operator we never pass the reference, but in the above code, we are passing the reference to the operator.
Can anyone explain the part where operator reference is passed?
In the code friend ostream & operator << the & is associated with the type overloaded operator returns.
So that it returns ostream & and istream & for the second one.
The overloaded operators:
Take the reference to istream or ostream object whcih is I/O object like cin/cout for console I/O or other type of stream object (I/O from/to string, etc).
Affect the state of the object so that data is read/written.
Return the reference to that object so that you can use these operators in sequence like:
Complex c1
Complex c2;
cin >> c1 >> c2;
Generally if a Declare one name (only) per declaration rule is adhered to,
then this allows to consistantly write a pointer/refrence "stuck" next to the type as:
istream& operator>> (istream& in, Complex& c)
{ //...
In this way it can be seen that the function named operator>> is returning a type istream& (a reference to an istream object).
This function takes 2 variables:
in of the type istream& (a reference to an istream object),
c of the type Complex& (a reference to a Complex object).
and similarly for:
ostream& operator<< (ostream& out, const Complex& c)
{ //...
The formatting of the code does not in anyway affect how the code is compiled.
So the functions definitions in this answer are exactly the same as in the question.
As to why use a reference, I suggest to read: When to use references vs. pointers

Operator "<<" overloading return type

Suppose there is a cPoint class.
class cPoint {
int x, y, z;
};
I wanted to print all of three variables in a single statement. So, I overloaded operator << just like
friend std::ostream& operator<< (std::ostream &cout, cPoint &p);
std::ostream& operator<< (std::ostream &out, cPoint &p) {
out << p.get_x() << " " << p.get_y() << " " << p.get_z() << std::endl;
return out;
}
Make sense?
My question lies in the lines of that what would happen in case of insertion operator(>>). I overloaded that as well to take the values of x, y and z into a single statement.
friend std::istream& operator>> (std::istream &cin, Point &p);
std::istream& operator>> (std::istream &in, Point &p) {
int tmp;
in >> tmp;
p.set_x(tmp);
in >> tmp;
p.set_y(tmp);
in >> tmp;
p.set_z(tmp);
}
Clear?
int main() {
cout << p << endl;
cin >> p;
}
I know that if operator<< returned void then the compiler evaluates cout << p << endl;
Due to the precedence/associativity rules, it evaluates this expression as (cout << cPoint) << endl;. cout << cPoint calls our void-returning overloaded operator<< function, which returns void. Then the partially evaluated expression becomes: void << endl;, which makes no sense!
But what would happen in case of >>. Why can't I return a void for >> as like:
void operator>> (std::istream &cin, Point &p);
Because it does not matter if cin >> p returns void or something else. There is no other operand who could use it. This is not clear.
You can return void from stream extracting operator >>, just like you can return void from a stream inserting operator <<. And just like with the inserting one, it will prevent you from doing chaining:
cPoint p, q;
cin >> p >> q; // This would fail with return type void
... and the very common test-correctness idiom:
cPoint p;
if (cin >> p) {
}
I overloaded operator << just like ...
Proper override should take the second parameter by const reference:
friend std::ostream& operator<< (std::ostream &cout, const cPoint &p);
// ^^^^^
I overloaded that as well to take the values of x, y and z into a single statement.
You forgot to return in from the implementation:
std::istream& operator>> (std::istream &in, Point &p) {
int tmp;
in >> tmp;
p.set_x(tmp);
in >> tmp;
p.set_y(tmp);
in >> tmp;
p.set_z(tmp);
return in; <<== Here
}
Making it void would prevent you from reading anything else after the point on the same line.

Private members of class - within this context

I get weird notification that I'm using private members of class - which is entirely valid, but I though that I'm allowed to do so, since I did say that the method I'm using is a friendly one.
Take a look at this:
#include <iostream>
using namespace std;
class complex {
private:
double Re, Im;
public:
complex(): Re(0.0), Im(0.0){}
complex(double Re, double Im): Re(Re), Im(Im){}
double getRe() const { return Re; }
double getIm() const { return Im; }
friend complex operator+(const complex&, const complex&);
friend ostream& operator<<(ostream&, const complex&);
friend istream& operator>>(istream &, const complex &); // FRIENDLY FUNCTION
};
complex operator+(const complex& a, const complex& b) {
double r, i;
r = a.getRe()+ b.getRe();
i = a.getIm() + b.getIm();
return complex(r, i);
}
ostream& operator<<(ostream& out, const complex &a) {
out << "(" << a.getRe() << ", " << a.getIm() << ")" << endl;
return out;
}
istream &operator>>(istream &in, complex &c)
{
cout<<"enter real part:\n";
in>>c.Re; // ** WITHIN THIS CONTEXT ERROR **
cout<<"enter imag part: \n";
in>>c.Im; // ** WITHIN THIS CONTEXT ERROR **
return in;
}
int main(void) {
complex a, b,c;
cin >> a;
cin >> b;
c = a+b;
cout << c;
}
Should I declare some sort of setFunction within the class in order to get the values which are private ?
istream& operator>>(istream &, const complex &);
is not the same as
istream &operator>>(istream &in, complex &c);
Spot the difference left as exercise to the reader.