Overloading casting Operator: C++ - c++

I am trying to cast two classes Pounds and Cents.
#include <iostream>
using namespace std;
class Pounds;
//Overloading Casting Operators: Pounds and Cents
class Cents{
int mCents;
public:
Cents(int n);
friend ostream& operator<< (ostream& os,const Cents& c);
operator Pounds();
};
class Pounds{
int mPounds;
public:
Pounds(int n);
operator Cents();
friend ostream& operator<< (ostream& os,const Pounds& p);
};
Cents::Cents(int n):mCents(n){}
ostream& operator<< (ostream& os,const Cents& c){
os << c.mCents;
return os;
}
Cents::operator Pounds(){
double d=mCents/100.0 ;
cout << d << endl;
return d;
}
Pounds::Pounds(int n):mPounds(n){}
Pounds::operator Cents(){
return mPounds*100;
}
ostream& operator<< (ostream& os,const Pounds& p){
os << p.mPounds;
return os;
}
int main()
{
Pounds Pnd(4);
cout << (Cents) Pnd << endl;
Cents Cnt(456);
cout.precision(3);
cout << showpoint << (Pounds) Cnt << endl;
return 0;
}
My question is very simple. Why the output is showing as:
$ ./Trial
400
4.56
4
I mean inside the cast operator Pounds() it is showing real decimal valie 4.56 but inside main 4. Not able to figure out, what is happening.

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);
}

Why I add an "&" ,it outputs different?

I've asked a similar question before (Why the code doesn't work on CodeBlocks,but on VS works).
Now a new error confused me.
cout << ++a1 << endl; will call the function operator double().
If in the code Fraction& operator++() I remove the & to make it Fraction operator++(), it will call the ostream& operator<<(ostream& os, Fraction&& obj).
#include <iostream>
using namespace std;
class Fraction
{
private:
int fenzi, fenmu;
public:
Fraction(int a, int b) :fenzi(a), fenmu(b) {}
operator double()
{
return 1.0* fenzi / fenmu;
}
friend ostream& operator<<(ostream& os, Fraction&& obj)
{
os << obj.fenzi << "/" << obj.fenmu;
return os;
}
Fraction& operator++()
{
fenzi++;
fenmu++;
return *this;
}
Fraction operator++(int)
{
Fraction tmp(fenzi, fenmu);
fenzi++;
fenmu++;
return tmp;
}
};
int main()
{
Fraction a1(9, 11), a2(1, 2);
cout << double(a2) << endl;
cout << ++a1 << endl;
cout << a1++ << endl;
return 0;
}
I wonder why is the output different?
Fraction operator++() and Fraction& operator++(), the former returns a copied one, and the latter returns the original one. But their types are both Fraction. I think it should have both called ostream& operator<<(ostream& os, Fraction&& obj).
Fraction operator++() output(I expected):
0.5
10/12
10/12
Fraction& operator++() output:
0.5
0.833333
10/12
operator double()
{
return 1.0* fenzi / fenmu;
}
Is the implicit conversion operator. Adding the explicit keyword to the operator double() will help with this because the compiler won't implicitly convert a Fraction into a double without you explicitly casting it into a double. The addition would look like:
explicit operator double()
{
return 1.0* fenzi / fenmu;
}
The correct output stream operator is defined as
friend ostream& operator<<(ostream& os, Fraction& obj). You defined it as friend ostream& operator<<(ostream& os, Fraction&& obj)
Fraction&& is a rvalue reference instead of a lvalue reference (Fraction&). The compiler used the implicit conversion operator because the operator++ (in either definition) returns a lvalue (reference) instead of a rvalue reference which was defined as the parameter of the output stream operator.

C++ assignment operator and overloading

I was wondering why the following code is being compiled successfully.
#include <iostream>
#include <cassert>
using namespace std;
class Integer{
private:
int i;
public:
Integer(int value):i(value){}
// unary or binary
const Integer operator+(const Integer& rv){
cout << "operator+"<< endl;
return Integer(i + rv.i);
}
// Binary Operator
Integer& operator+=(const Integer& rv){
cout << "operator+=" << endl;
i += rv.i;
return *this;
}
ostream& operator<<(ostream& lv){
lv << i;
return lv;
}
friend ostream& operator<< (ostream& lv, Integer& rv);
};
ostream& operator<< (ostream& lv,Integer& rv){
return lv << rv.i;
}
int main(){
cout << "using operator overloading"<< endl;
Integer c(0), a(4), b(5);
Integer d = 8;
c = a + b;
cout << c << endl;
cout << d << endl;
}
I dont understand why d = 8 is possible. d is a user defined type. I did not overloaded the assignment oeprator for the Integer class. Is there a default overloaded operator?
You have not declared the Integer constructor explicit, so it acts as an implicit conversion from int to Integer.
If you declare your constructor
explicit Integer(int value);
the compiler fires the error:
error: conversion from ‘int’ to non-scalar type ‘Integer’ requested
Your constructor defines conversion from int to Integer
Integer(int value):i(value){}
I dont understand why d = 8 is possible. d is a user defined type.
Just instrument your code a little more and also trace the ctor calls.
#include <iostream>
#include <cassert>
using namespace std;
class Integer{
private:
int i;
public:
Integer(int value):i(value){
cout << "Integer(int) called with " << value << endl;
}
// unary or binary
const Integer operator+(const Integer& rv){
cout << "operator+"<< endl;
return Integer(i + rv.i);
}
// Binary Operator
Integer& operator+=(const Integer& rv){
cout << "operator+=" << endl;
i += rv.i;
return *this;
}
ostream& operator<<(ostream& lv){
lv << i;
return lv;
}
friend ostream& operator<< (ostream& lv, Integer& rv);
};
ostream& operator<< (ostream& lv,Integer& rv){
return lv << rv.i;
}
int main(){
Integer d = 8;
cout << d << endl;
}
output:
Integer(int) called with 8
8
live at Coliru's
The presumed assignment of the immediate 8 in fact causes the non-explicit Integer(int) ctor to be called with 8 as its argument.

Use of overloaded operator '>>' is ambiguous (with operand types 'istream' (aka 'basic_istream<char>') and 'MyIncreEx')

Here is the code and I don't seem to find what's wrong with it; I need to overload the << and >> operators, but I get the following error:
Use of overloaded operator '>>' is ambiguous (with operand types 'istream' (aka 'basic_istream') and 'MyIncreEx')
I can't see what's really ambiguous about it:
class MyIncreEx;
istream& operator>>(istream& is, MyIncreEx& s);
ostream& operator<<(ostream &os, MyIncreEx& s);
MyIncreEx operator++(MyIncreEx& d, int dummy);
MyIncreEx operator++(MyIncreEx& d);
class MyIncreEx
{
friend istream& operator>>(istream& is, MyIncreEx s);
friend ostream& operator<<(ostream& os, MyIncreEx s);
friend MyIncreEx operator++(MyIncreEx& d, int dummy);
friend MyIncreEx operator++(MyIncreEx& d);
public:
int num1 = 0, num2 = 0, num3 = 0;
};
istream& operator>>(istream& is, MyIncreEx& s)
{
is >> s.num1;
is >> s.num2;
is >> s.num3;
return is;
};
ostream& operator<<(ostream &os, MyIncreEx& s)
{
os << "(" << s.num1 <<"," <<s.num2 << "," << s.num3 <<")"<< endl;
return os;
};
MyIncreEx operator++(MyIncreEx& d)
{
d.num1++;
d.num2++;
d.num3++;
return d;
};
MyIncreEx operator++(MyIncreEx& d, int dummy)
{
d.num1++;
d.num2++;
d.num3++;
return d;
};
int main()
{
MyIncreEx obj;
cout << "please enter three numbers: ";
cin >> obj;
cout << "The original value are: " << obj << endl;
obj++;
cout << "The new values after obj++ are: " << obj << endl;
++obj;
cout << "The new values after ++obj are: " << obj << endl;
}
You declared two different versions of the output operators:
istream& operator>>(istream& is, MyIncreEx& s);
ostream& operator<<(ostream &os, MyIncreEx& s);
class MyIncreEx
{
friend istream& operator>>(istream& is, MyIncreEx s);
friend ostream& operator<<(ostream& os, MyIncreEx s);
...
};
The friend operators have a different and conflicting signature. You probably wanted to declare them as
friend istream& operator>>(istream& is, MyIncreEx& s);
friend ostream& operator<<(ostream& os, MyIncreEx const& s);
(assuming you also fix the output operator to work with MyIncreEx const& rather than MyIncreEx&).

operator<< overloading [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Operator overloading
I didn't find any thing that could help me in this subject...
I'm trying to over load the << operator, this is my code:
ostream& Complex::operator<<(ostream& out,const Complex& b){
out<<"("<<b.x<<","<<b.y<<")";
return out;
}
this is the declaration in the H file:
ostream& operator<<(ostream& out,const Complex& b);
I get this error:
error: std::ostream& Complex::operator<<(std::ostream&, const Complex&) must take exactly one argument
what and why I'm doing wrong?
thanks
your operator << should be free function, not Complex class member in your case.
If you did your operator << class member, it actually should take one parameter, which should be stream. But then you won't be able to write like
std::cout << complex_number;
but
complex_number << std::cout;
which is equivalent to
complex_number. operator << (std::cout);
It is not common practice, as you can note, that is why operator << usually defined as free function.
class Complex
{
int a, b;
public:
Complex(int m, int d)
{
a = m; b = d;
}
friend ostream& operator<<(ostream& os, const Complex& complex);
};
ostream& operator<<(ostream& os, const Complex& complex)
{
os << complex.a << '+' << complex.b << 'i';
return os;
}
int main()
{
Complex complex(5, 6);
cout << complex;
}
More info here
As noted, the streaming overloads need to to be free functions, defined outside of your class.
Personally, I prefer to stay away from friendship and redirect to a public member function instead:
class Complex
{
public:
std::ostream& output(std::ostream& s) const;
};
std::ostream& operator<< (std::ostream& s, const Complex& c)
{
return c.output(s);
}