Dec 3, 2011 at 10:33am UTC
#include<iostream>
#include<cmath>
using namespace std;
class Point{
public:
double x,y;
Point(){
x=0,y=0;
//cout<<"a point ("<<x<<","<<y <<") is created" <<endl;
}
Point(double m,double n){
x=m;
y=n;
//cout<<"a point ("<<m<<","<<n<<") is created" <<endl;
}
~Point(){
//cout<<"a point ("<<x<<","<<y<<") is destroyed" <<endl;
}
double get_x(){
return x;
}
double get_y(){
return y;
}
friend ostream &operator<<(ostream &stream,Point A);
friend Point operator+(Point A,Point B);
friend double dist(const Point A, const Point B);
};
ostream &operator<<(ostream &stream,Point A){
stream<<"("<<A.x<<","<<A.y<<")"<<endl;
return stream;
}
Point operator+(Point A,Point B){
return Point(A.x+B.x, A.y+B.y);
}
double dist(const Point A, const Point B){
return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}
class Triangle{
public:
Point P1,P2,P3;
double get_area(){
double s,a;
s=0.5*(dist(P1,P2)+dist(P2,P3)+dist(P3,P1));
a=sqrt(s*(s-dist(P1,P2))*(s-dist(P2,P3))*(s-dist(P3,P1)));
return a;
}
};
int main(){
Point p1(23,30),p2(50,25),p3(15,15);
Triangle t;
cout<<"point1:"<<p1<<endl;
cout<<"point2:"<<p2<<endl;
cout<<"point3:"<<p3<<endl;
cout<<"distance between"<<p1<<"&"<< p2 << "is:a= " << dist(p1,p2) << endl;
cout<<"distance between"<<p2<<"&"<< p3 << "is:b=" << dist(p2,p3) << endl;
cout<<"distance between"<<p3<<"&"<< p1 << "is:c=" << dist(p3,p1) << endl;
cout<<"AREA:"<<t.get_area()<< endl;
return 0;
}
Dec 3, 2011 at 10:54am UTC
You never assign p1, p2, p3 to t.P1, t.P2, T.P3
Those are all different and the last three are (0,0) due to default constructor.
Next time include a description of your problem or at least the output.
Also, use [code] tags.
Dec 3, 2011 at 11:26am UTC
got it..:)
thanx a lot for your help...