I just get introduced to C++ OOP.
I created two classes name Integer and Float and defined conversion constructor for every class.
I have no problem converting Float to Integer, as shown below.
The real problem comes when I remove the comment symbol to convert from Integer to Float.
#include <iostream>
using namespace std;
class Float
{
float x, y;
public:
Float() : x(0),y(0) {}
Float(float a, float b) : x(a),y(b) {}
// Float(const Integer&);
float getX() const { return this->x; }
float getY() const { return this->y; }
float summa()
{
return this->x + this->y;
}
};
class Integer
{
int v, w;
public:
Integer() : v(0),w(0) {}
Integer(int a, int b) : v(a),w(b) {}
Integer(const Float&);
int getV() const { return this->v; }
int getW() const { return this->w; }
int summa()
{
return this->v + this->w;
}
};
int main()
{
// Integer i(5, 2);
// Float f = i;
Float F(3.23, 5.78);
Integer I = F;
// cout << f.summa();
cout << I.summa();
return 0;
}
/*
Float::Float(const Integer& i)
{
this->x = (float)(i.getV());
this->y = (float)(i.getW());
}
*/
Integer::Integer(const Float& f)
{
this->v = (int)(f.getX());
this->w = (int)(f.getY());
}