Let's say, I have two simple classes: integer
with just one int
variable in it, and real
with double
. I'm already done so that
integer a=2016;
int b=a;
real a=20.16;
double b=a;
working perfectly. Now I have to do the conversion between them, like so:
integer foo; real bar;
foo=bar;
bar=foo;
Or at least with similar syntax. I know that there will be loss of some data when converting double to int, I'll deal with it. So how can I do that? What and where should I #include and what the methods/operator overloads to write? My project is divided into files main.cpp
, integer.h
, integer.cpp
, real.h
, real.cpp
. Thanks.
EDIT: The classes looks like:
//integer.h
class integer
{
private:
int a;
public:
integer();
integer(int number);
operator int() const;
};
//integer.cpp
#include "integer.h"
integer::integer(){
a=0;
}
integer::integer(int number){
a=number;
}
integer::operator int() const{
return a;
}
//real.h
class real {
private:
double a;
public:
real();
real(double number);
operator double() const;
};
//real.cpp
#include "real.h"
real::real(){
a=0;
}
real::real(double number){
a=number;
}
real::operator double() const{
return a;
}