Create a class called Point which can be used to set and get a point (x, y) on a X-Y coordinator. Write a program to let your client set any three points to define a triangle on the X-Y plane and determine:-

1. what type of triangle formed by client’s three points, e.g. right angle triangle, isosceles triangle and equilateral triangle,
2. whether the three points are collinear (three points are on the same line),
3. area of the triangle if the three points are not collinear.

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
class point
{
private:
int x, y;
public: 
point(){}
point(int tx, int ty){x=tx;y=ty;}
int getx(){return x;}
int gety(){return y;}

};
void main()
{
point pointer;
int tempX,tempY;
double a, b, c, s, area;
cout<<"Enter the x coordinator for 1st point";
cin>>tempX;
cout<<"Enter the y coordinator for 1st point";
cin>>tempY;
point p(tempX,tempY);
cout<<"\nEnter the x coordinator for 2nd point";
cin>>tempX;
cout<<"Enter the y coordinator for 2nd point";
cin>>tempY;
point p1(tempX,tempY);
cout<<"\nEnter the x coordinator for 3rd point";
cin>>tempX;
cout<<"Enter the y coordinator for 3rd point";
cin>>tempY;
point p2(tempX,tempY);
if(p.getx()==p1.getx() && p1.getx()==p2.getx() || (p.gety()==p1.gety() && p1.gety()==p2.gety()))
cout<<"\nThe points are collinear"<<endl;
else{
/* Compute distance between p1 and p2. */
a = sqrt((p1.getx() - p.getx())*(p1.getx() - p.getx()) + (p1.gety() - p.gety())*(p1.gety() - p.gety()));
/* Print distance. */
cout<<"Distance between points p1 and p2: "<<a<<endl;

/* Compute distance between p2 and p3. */
b = sqrt((p2.getx() - p1.getx())*(p2.getx() - p1.getx()) + (p2.gety() - p1.gety())*(p2.gety() - p1.gety()));
/* Print distance. */
cout<<"Distance between points p2 and p3: "<<b<<endl;
/* Compute distance between p1 and p3. */
c = sqrt((p2.getx() - p.getx())*(p2.getx() - p.getx()) + (p2.gety() - p.gety())*(p2.gety() - p.gety()));
/* Print distance. */
cout<<"Distance between points p1 and p3: "<<c<<endl;


s = 0.5*( a + b + c );
area = sqrt( s*(s-a)*(s-b)*(s-c) );
cout<<"The area of the triangle is : "<<area<<endl;

if(a==b && b==c && a==c)

cout<<"It is an equilateral triangle."<<endl;
else
if(a==b || b==c || a==c)
cout<<"It is an isosceles triangle."<<endl;

else
if(c==sqrt(a*a + b*b) || a==sqrt(b*b + c*c) || b==sqrt(a*a + c*c))
cout<<"It is a right-angled triangle."<<endl;
}
}

Hello Everyone! I'm new here. I'm quite bad at my OOP, and the above is a qn i gotta work on. I'm extremely bad at CLASS, I'm not sure how to go abt with it. I still tried using it and the above is what i got. The progam works well but i'm not sure the CLASS part is right so do help mi please.

Hello Everyone! I'm new here.

Hi! :)

The progam works well but i'm not sure the CLASS part is right so do help mi please.

Well, if the program works well then I don't see a problem with it. ;) The class looks fine, but usually Point classes make x and y public because the objects are just there to define a pair relationship that's easier to work with rather than do any significant data hiding.

There's one thing that will get you in trouble with anal people and that's void main() . main() should be defined to return int because that's what the language standard says is right. You don't even have to actually return something because main() is a special function that automatically returns 0 if you fall off the end. That's specific to main() though, and you shouldn't expect it for any other function. Of course, I'd recommend that you return something explicitly to be consistent. :)

int main()
{
  return 0;
}

The class is fine (And data members should always be private unless you have a compelling reason to do otherwise. since that stops them being accidentally modified by external objects or functions ).

Perhaps you could find other ways to use your point objects which don't involve getX() and getY() - for example, where you need to compare two different points, maybe you could provide your point class with an is_equal() function, which accepts a point object, and returns a true/false value depending whether the coordinates match.

You could also add a function which acccepts a point object as input, and calculates the scalar distance from that point.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.