#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
class Point
{
private:
float x;
float y;
public:
Point();
Point(float, float);
void Nhap ();
float DoDai (Point);
};
class Triangle
{
private:
Point p1, p2, p3;
public:
void Nhap();
void Xuat();
float DienTich ();
};
Point::Point()
{
x=0;
y=0;
}
Point::Point (float a, float b)
{
x = a;
y = b;
}
void Point::Nhap()
{
cout << "x: ";
cin >> x;
cout << "y: ";
cin >> y;
}
float Point::DoDai (Point p)
{
return sqrt(pow(x-p.x,2)+pow(y-p.y,2));
}
void Triangle::Nhap()
{
cout <<"Nhap toa do dinh 1: "<< endl;
p1.Nhap();
cout <<"Nhap toa do dinh 2: " << endl;
p2.Nhap();
cout <<"Nhap toa do dinh 3: " << endl;
p3.Nhap();
}
float Triangle::DienTich ()
{
float a, b, c, p, s;
a = p1.DoDai(p2);
b = p2.DoDai(p3);
c = p3.DoDai(p1);
p = (a+b+c)/2;
s = sqrt(p*(p-a)*(p-b)*(p-c));
return s;
}
void main()
{
Triangle n,s;
n.Nhap();
cout << s.DienTich();
getch();
}
Tại sao mình in ra DT bằng 0 không ạ?
E muốn tạo hàm tạo cho cho class Triangle thì phải làm sao?