I need to write program as follow:
#include <iostream>
using namespace std;
class Point
{
public:
double X;
double Y;
Point(){}
Point(double x, double y)
{
X = x;
Y = y;
}
};
class Circle
{
public:
Point P;
double R;
Circle(){}
Circle(Point p, double r)
{
P = p;
R = r;
}
Circle operator +(Circle C1, Circle C2)
{
return Circle(C1.Point, C1.R + C2.R);
}
Circle operator -(Circle C1, Circle C2)
{
return Circle(C2.Point, C1.R - C2.R);
}
};
int main()
{
Circle c1, c2, cr1, cr2, ck1, ck2;
// create c1
// create c2
cr1 = c1 + c2;
// display cr1
cr2 = c2 + c1;
// display cr2
ck1 = c1 - c2;
// display ck1
ck2 = c2 - c1;
// display ck2
return 0;
}
Two classes Point and Circle, where Circle have member of Point as it's center, two operators to add and to subtract two Circles.
And I can't compile this, what is wrong?
#################################################################################
EDIT:
After correction it looks like that, and works perfectly:
#include <iostream>
using namespace std;
class Point
{
public:
double X, Y;
Point(){}
Point(double x, double y)
{
X = x;
Y = y;
}
};
class Circle
{
public:
double R;
Point P;
Circle(){}
Circle(Point p, double b)
{
P = p;
R = b;
}
Circle operator+(const Circle& C1)
{
Circle C;
C.P = this->P;
C.R = this->R + C1.R;
return C;
}
Circle operator -(const Circle& C1)
{
Circle C;
C.P = C1.P;
C.R = this->R - C1.R;
return C;
}
};
int main()
{
double X, Y, R;
cout << "Coordinates for C1:" << endl;
cout << "\tX: ";
cin >> X;
cout << "\tY: ";
cin >> Y;
cout << "Radius for C1:";
cin >> R;
Circle *c1 = new Circle(Point(X, Y), R);
cout << "Coordinates for C2:" << endl;
cout << "\tX: ";
cin >> X;
cout << "\tY: ";
cin >> Y;
cout << "Radius for C2:";
cin >> R;
Circle *c2 = new Circle(Point(X, Y), R);
Circle cs1 = c1->operator+(*c2);
Circle cs2 = c1->operator-(*c2);
Circle cr1 = c2->operator+(*c1);
Circle cr2 = c2->operator-(*c1);
cout << "cs1([" << cs1.P.X << ", " << cs1.P.Y << "], " << cs1.R << ")" << endl;
cout << "cs2([" << cs2.P.X << ", " << cs2.P.Y << "], " << cs2.R << ")" << endl;
cout << "cr1([" << cr1.P.X << ", " << cr1.P.Y << "], " << cr1.R << ")" << endl;
cout << "cr2([" << cr2.P.X << ", " << cr2.P.Y << "], " << cr2.R << ")" << endl;
char ch;
cin >> ch;
return 0;
}
When you define operators like operator- inside your class as member functions, then when you use it
C3 = C1 + C2;
The compiler is actually calling your member function like
C3 = C1.operator+(C2);
From this you should be able to figure out that operators as member functions only takes one argument, and that the first object in the operator is the this object.
For stand-alone (non-member) functions they need two arguments.
You might want to check e.g. this reference on operator overloading.
You have incorrect overloaded operators. Look at the following code:
Circle operator +(const Circle& C)
{
return Circle(this->P, this->R + C.R);
}
Circle operator -(const Circle& C)
{
return Circle(this->P, this->R - C.R);
}
Related
I have been trying to print the points like The position of the point is (1,2) from using class, but I can't figure out a way to do it. I simply can't find a way to return two numbers like that, but the problem requires solution that way.
#include <iostream>
using namespace std;
class MyPoint{
public:
int x,y,radius;
MyPoint()
{
x=0;
y=0;
}
MyPoint(int x1,int y1)
{
x=x1;
y=y1;
}
int point_display()
{
char st=(x,y);
return st;
}
int getAdd()
{
return x+y;
}
};
int main()
{
MyPoint mypoint;
cin>>mypoint.x>>mypoint.y;
cout<<"The position of the point is "<<mypoint.point_display()<<endl;
cout<<"The sum of the coordinates is "<<mypoint.getAdd()<<endl;
return 0;
}
The usual solution to this is to provide an overload of operator << for class MyPoint to print the point.
Something like this:
#include <iostream>
using namespace std;
class MyPoint{
public:
int x,y,radius;
MyPoint()
{
x=0;
y=0;
}
MyPoint(int x1,int y1)
{
x=x1;
y=y1;
}
int getAdd()
{
return x+y;
}
friend ostream& operator << (ostream& os, const MyPoint& p);
};
ostream& operator << (ostream& os, const MyPoint& p)
{
os << p.x << ", " << p.y;
return os;
}
int main()
{
MyPoint mypoint { 1,2 };
cout<<"The position of the point is "<<mypoint<<endl;
cout<<"The sum of the coordinates is "<<mypoint.getAdd()<<endl;
return 0;
}
Output:
The position of the point is 1, 2
The sum of the coordinates is 3
Live demo
Your point_display could return a string composed of the 2 values:
std::string point_display()
{
return std::string{"("} + std::to_string(x)
+ "," + std::to_string(x) + ")";
}
Alternatively, as your question asks about returning 2 values, the function could return a pair:
std::pair<int,int> point_display ()
{
return {x,y};
}
and in main, you could do:
auto [x, y] = mypoint.point_display();
cout << "The position of the point is ("
<< x << "," << y << ")" << endl;
However, since the data members are public, you could just destructure the object and print out the values in main:
auto [x, y, radius] = mypoint;
cout << "The position of the point is ("
<< x << "," << y << ")" << endl;
If all you want to do is print the coordinates, you could have the method do it:
void point_display()
{
cout << "(" << x << ", " << y << ")";
}
...
cout<<"The position of the point is ";
mypoint.point_display();
cout << endl;
If you really want to return the coordinates, you could have separate accessors ("getters"):
int getX()
{
return x;
}
int getY()
{
return y;
}
or use references:
void getCoordinates(int &rx, int &ry)
{
rx = x;
ry = y;
}
...
int a, b;
mypoint.getCoordinates(a, b);
cout << a << " " << b << endl;
I am trying to convert different coordinate systems. From polar to rectangular and vice versa. My pol_to_rect()function is not working properly. It is giving very small values(~10^(-44)) after converting and also before converting. There might be some problem while using the sin() and cos() functions. The rect_to_pol() is working fine for positive values.
Edit - When I changed atan() to atan2() how can I incorporate other values of x and y.
#include <iostream>
#include <cmath>
using namespace std;
#define PI 3.1415926
class Polar; // Forward declaration
class Rectangular {
private:
float x, y;
public:
Rectangular() {} // default constructor
Rectangular(float mv_x, float mv_y) {
x = mv_x;
y = mv_y;
}
void showData() const;
Polar rect_to_pol();
float& get_x() {
return x;
}
float& get_y() {
return y;
}
};
void Rectangular::showData() const {
cout << "--Rectangular--" << endl;
cout << "x: " << x << "\t" <<"y: " << y << endl;
}
class Polar {
private:
float r;
float theta;
public:
Polar() {} // default constructor
Polar(float mv_r, float mv_theta) {
r = mv_r;
theta = mv_theta;
}
void showData();
Rectangular pol_to_rect();
float& get_r(){
return r;
}
float& get_theta() {
return theta;
}
};
void Polar::showData() {
cout << "--Polar--" << endl;
cout << "r:" << r << "\t" << "Theta(Radians):" << theta << endl;
}
Rectangular Polar::pol_to_rect() {
Rectangular temp;
temp.get_x() = r * cos(theta*(PI/180.0)); // in degrees
temp.get_y() = r * sin(theta*(PI/180.0));
return temp;
}
Polar Rectangular::rect_to_pol() {
Polar temp;
temp.get_r() = sqrt(pow(x, 2) + pow(y, 2));
temp.get_theta() = atan2(y, x);
return temp;
}
int main()
{
Rectangular r1(-1, -1), r2;
Polar p1(12.0, 30.0), p2;
r1.showData();
p2 = r1.rect_to_pol();
cout << "After Conversion (RECT TO POLAR)->" << endl;
p2.showData();
p1.showData();
r2 = p1.pol_to_rect();
cout << "After Conversion (POLAR TO RECT)" << endl;
r2.showData();
return 0;
}
Hello I have trouble access STL complex + binary operator, my fVector is derived from std::complex, I try to call the std::complex binary + operator function to do the calculation, but I say it does have member of operator +.
ok update more of my code as request, there is 3 file which is fVector2D_test.cpp fVector2D.cpp and fVector2D.hpp
#include <iostream>
#include <complex>
#include "fVector2D.hpp"
//using namespace std;
using std::cout;
using std::endl;
int main()
{
/*fVector2D u, v(2.4f, 7), w(v);
cout << v.X(); u = fVector2D(u.X(), v.Y());
cout << u <<endl;
// v.Y() = 3.4f; // compiler error
fVector2D a(1, 2), b(2, 3);
float dot = a*b;
cout << dot <<endl;
cout << fVector2D::EX << "+" << fVector2D::EY;*/
fVector2D v(3,4.1f), u(1.2f,8.5f);
fVector2D w = u + v;
cout << w << endl;
//w = exp(std::complex<float>(0,0.2f))*v;
//cout << w << endl;
// cout << (u*v) << endl;
//cout << fVector2D::EX << endl;
//cout << fVector2D::EY << endl;
//cout << abs(v) << endl;
return 0;
}
#include <complex>
class fVector2D : public std::complex<float>
{
public:
fVector2D(float x=0, float y =0);
fVector2D(const fVector2D& floatVector2D);
float X()const;
float Y()const;
//static const EX;
//static const EY;
private:
};
fVector2D operator+(const fVector2D& , const fVector2D&);
#include "fVector2D.hpp"
fVector2D::fVector2D(float x, float y)
: std::complex<float>(x, y)
{
}
fVector2D::fVector2D(const fVector2D& floatVector2D)
: std::complex<float>(floatVector2D)
{
}
float fVector2D::X()const
{
return real();
}
float fVector2D::Y()const
{
return imag();
}
fVector2D operator+(const fVector2D& lhs, const fVector2D& rhs)
{
return std::complex<float>::operator+(lhs,rhs);// compile error , no operator + in the member
}
+ for complex numbers is a free function, not a member function. That is why you can not access it using complex<float>:: syntax.
Beside this problem your code has other problems(I have trouble seeing any real scenario where one would want to inherit from std::complex), but that is outside the scope of the question.
Not able to understand a block of code in below given program.
Especially the variable, temp which has return type as the complex(the class name) and when we return the variable, temp where it gets returned to?
That is return(temp); in the program.
The Program
#include <iostream>
using namespace std;
class complex
{
public:
complex();//default constructors
complex(float real, float imag)//constructor for setting values
{
x = real;
y = imag;
}
complex operator +(complex);
void display(void);
~complex();
private:
float x;
float y;
};
complex::complex()
{
}
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
complex complex::operator+(complex c)
{
complex temp;
temp.x = x + c.x;
temp.y = y + c.y;
return(temp);
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
void complex::display(void) {
cout << x << "+j" << y << "/n";
}
complex::~complex()
{
}
int main()
{
complex C1, C2, C3,C4;
C1 = complex(1, 3.5);//setting of first number
C2 = complex(2,2.7);//setting of second number
C4 = complex(2, 5);
C3 = C1 + C2+C4;//operator overloading
cout << "C1 = ";
C1.display();
cout << "\n C2 = ";
C2.display();
cout << "\n C4 = ";
C4.display();
cout << "\n C3 = ";
C3.display();
system("pause");
return 0;
}
complex C5;
C5=C1+C2;
means
C5=C1.operator+(C2)
equivalent to
complex temp;
temp.x = x + C2.x; /* x=C1.x*/
temp.y = y + C2.y; /* y=C1.y*/
C5=temp;
I am trying to implement comparison between different subclasses of the same base class. The comparison should return false if the two instances are of different subclass or return the actual comparison result if they are of the same subclass.
Check the last line in function main: although I have declared equalTo as virtual, only the equalTo method of the base class is called. What is my mistake?
Thanks in advance.
#include <iostream>
#include <fstream>
#include <cmath>
#include <limits>
#include <sstream>
#include <stdexcept>
#include <algorithm>
using namespace std;
bool fileInput = false, fileOutput = false;
class Point
{
public:
double x,y;
Point(){};
Point(double x1, double y1) {
x=x1;
y=y1;
}
bool operator==(Point other) const
{
return (abs(x - other.x) < numeric_limits<double>::epsilon()) and (abs(y - other.y) < numeric_limits<double>::epsilon());
}
};
class Shape
{
protected:
virtual double area() const
{
return 0;
}
virtual void print(std::ostream& os) const {}
virtual void read(std::istream& is) {}
public:
bool compare(Shape* other) {
return area() < other->area();
}
virtual bool equalTo(Shape other) const {
cout << "original";
return false;
}
friend std::ostream& operator<<(std::ostream &strm, const Shape &t)
{
t.print(strm);
return strm;
}
friend std::istream& operator>>(std::istream &strm, Shape &t)
{
t.read(strm);
return strm;
}
};
class Circle : public Shape
{
Point c;
double r;
double area() const
{
return M_PI * r * r;
}
void print(std::ostream &strm) const
{
strm << "Circle. Center coordinates: (" << c.x << "," << c.y << "). Radius: " << r << ". Area: " << area();
}
void read(std::istream &strm)
{
if (!fileInput) cout << "Enter Circle\nCenter: ";
strm >> c.x >> c.y;
if (!fileInput) cout << "Radius: ";
strm >> r;
if (r<0)
throw std::invalid_argument( "The radius cannot be negative." );
}
public:
Circle() {}
Circle(Point x, double y)
{
c = x;
r = y;
}
bool equalTo(Shape other1) const
{
Circle* other = dynamic_cast<Circle*>(&other1);
if (other == 0) return false;
return (c == other->c) and (abs(r - other->r)<numeric_limits<double>::epsilon());
}
};
class Hexagon : public Shape
{
Point c;
double r;
double area() const
{
return 1.5 * sqrt(3) * r * r;
}
void print(std::ostream &strm) const
{
strm << "Hexagon. Center coordinates: (" << c.x << "," << c.y << "). Circumcircle radius: " << r << ". Area: " << area();
}
void read(std::istream &strm)
{
if (!fileInput) cout << "Enter Hexagon\nCenter: ";
strm >> c.x >> c.y;
if (!fileInput) cout << "Circumcircle radius: ";
strm >> r;
if (r<0)
throw std::invalid_argument( "The circumcircle radius cannot be negative." );
}
public:
Hexagon() {}
Hexagon(Point x, double y)
{
c = x;
r = y;
}
bool equalTo(Shape other1) const
{
Hexagon* other = dynamic_cast<Hexagon*>(&other1);
if (other == 0) return false;
return (c == other->c) and (abs(r - other->r)<numeric_limits<double>::epsilon());
}
};
int main()
{
Shape c1 = Circle(Point(0,0), 3);
Shape c2 = Circle(Point(0,0), 3);
Shape c3 = Hexagon(Point(0,0), 3);
cout << "circles: " << c1.equalTo(c2) << endl << "diff: " << c1.equalTo(c3) << endl;
}
This is slicing, when you assign objects to object of type Shape - object was sliced to Shape. Use pointers, or may be references.
Circle p1(Point(0, 0), 3);
Circle p2(Point(0, 0), 3);
Hexagon h1(Point(0, 0), 3);
Shape& c1 = p1;
Shape& c2 = p2;
Shape& c3 = h1;
When you copy-construct a Shape out of a derived class, the object will be sliced so that only the Shape part of it is preserved. Then when you call equalTo, the function is statically bound.
In order to call the derived versions, make c1 and friends into Shape&.