I have a Java code; this my Poin Java code:
import java.util.Scanner;
public class Poin {
private int X;
private int Y;
public Poin() {
X = 0;
Y = 0;
}
public Poin(int X, int Y) {
this.X = X;
this.Y = Y;
}
public int getX() {
return X;
}
public int getY() {
return Y;
}
public boolean InRect(Poin TopLeft, Poin BottomRight) {
if (this.X < BottomRight.getX() && this.X > TopLeft.getX()
&& this.Y < BottomRight.getY() && this.Y > TopLeft.getY()) {
return true;
} else {
return false;
}
}
public static void main(String [] args) {
int N;
int i;
int x,y;
int count = 0;
Poin TopLeft, BottomRight;
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
Poin[] a = new Poin[N];
int x_top = sc.nextInt();
int y_top = sc.nextInt();
int x_bot = sc.nextInt();
int y_bot = sc.nextInt();
TopLeft = new Poin(x_top, y_top);
BottomRight = new Poin(x_bot, y_bot);
for (i = 0; i < N; i++) {
x = sc.nextInt();
y = sc.nextInt();
Poin p = new Poin(x, y);
a[i] = p;
if (p.InRect(TopLeft, BottomRight)) {
count += 1;
}
}
System.out.println(count);
for (i = 0; i < N; i++) {
System.out.println(a[N-1-i].getX()+","+a[N-1-i].getY());
}
}
}
I want to convert this to c++. This is my c++ code:
#include <iostream>
#include <cstdlib>
using namespace std;
class Poin
{
private :
int x;
int y;
public :
Poin() {
x = 0;
y = 0;
}
Poin(int x, int y) {
this->x = x;
this->y = y;
}
int get_x() {
return x;
}
int get_y() {
return y;
}
bool InRect(Poin& TopLeft, Poin& BottomRight) {
if (this->x < BottomRight.get_x() && this->x > TopLeft.get_x() && this->y < BottomRight.get_y() && this->y > TopLeft.get_y())
{
return true;
}
else
{
return false;
}
}
};
int main ()
{
int N;
int i;
int x,y;
int count = 0;
int x_top;
int y_top;
int x_bot;
int y_bot;
Poin TopLeft, BottomRight;
Poin** a = new Poin*[N];
cin>>x_top;
cin>>y_top;
cin>>x_bot;
cin>>y_bot;
TopLeft = new Poin(x_top, y_top);
BottomRight = new Poin(x_bot, y_bot);
for (i = 0; i < N; i++) {
x = cin>>x;
y = cin>>y;
px = p.poin(x, y);
a[i] = p;
if (px.InRect(TopLeft, BottomRight)) {
count += 1;
}
}
cout<<count;
for (i = 0; i < N; i++) {
cout << a[N-1-i].getX() << "," << a[N-1-i].getY();
}
return 0;
}
I have difficulty converting this code from Java to c++. Poin is class and using this as parameter in main program:
Poin TopLeft, BottomRight;
TopLeft = new Poin(x_top, y_top);
BottomRight = new Poin(x_bot, y_bot);
Does anybody have a solution to solve my problem?
Poin TopLeft(x_top, y_top); Poin BottomRightPoin(x_bot, y_bot).newwill return a pointer to aPoinobject.