class with non member functions

In addition to the main program and the class, I am supposed to write three nonmember functions.
1.A function to determine if three points form a triangle. The function should return a 1 if they form a triangle and return a 0 otherwise. In order to form a triangle. No two points can be equal.Find the distance between each pair of points. If the largest of the three distances equals the sum of the other two distances, then the points are on a line.
2. A function to find the perimeter of a triangle formed by three points (if they are not in a line).
3. A function to find the area of the triangle formed by three points (if they are not in a line) using the following formula, where a, b, and c are the lengths of the three sides and s is the perimeter of the triangle.

I feel lost on this question. I was guessing to use a friend function but I don't know how to connect everything. I don't necessarily need an answer, but some pointers would help. Here is my program so far:



//Jessica Ewing
//Homework Classes
# include <iostream>
# include <iomanip>
# include <cmath>
using namespace std;

class Point
{
private:
double x, y, z;
public:
void prompt_user();
void display();
double dis(Point);
double equal(Point);
};

//**************************************************************

void Point::prompt_user()
{ cout << "Enter three coordinates for a point: " << endl;
cin >> x >> y >> z;
}
void Point::display()
{ cout << x << " " << y << " " << z << endl;
}
double Point::equal(Point P2)
{ double test;
test = ((P2.x - x)+(P2.y - y)+(P2.z-z));
if(test == 0)
return 1;
else
return 0;
}
double Point::dis(Point P2)
{ return(sqrt(pow(P2.x - x,2.0)+ pow(P2.y - y,2.0)+ pow(P2.z - z,2.0)));
}
//****************************************************************








int main()
{ Point A, B, C;


cout << "This program determines if 3 points make a triangle or are in line." << endl;

A.prompt_user();
B.prompt_user();
C.prompt_user();

cout <<"Point 1: ";
A.display();

cout <<"Point 2: ";
B.display();

cout <<"Point 3: ";
C.display();












system("pause");
return 0;
}
//******************************************************






Unless you give your Point class some functions to access their internal coordinates you will have to use friend functions. These must be declared in your point class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Point
{
    friend int is_triangle(const Point& p1, const Point& p2, const Point& p3);
    friend int calc_perimeter(const Point& p1, const Point& p2, const Point& p3);
    friend int calc_area(const Point& p1, const Point& p2, const Point& p3);

private:
    double x, y, z;
public:
    void prompt_user();
    void display();
    double dis(Point);
    double equal(Point); 
};

int is_triangle(const Point& p1, const Point& p2, const Point& p3)
{
    // ... stuff ...
}

int calc_perimeter(const Point& p1, const Point& p2, const Point& p3)
{
    // ... stuff ...
}

int calc_area(const Point& p1, const Point& p2, const Point& p3)
{
    // ... stuff ...
}
Actually, since you only ever need to check distances and whether two points are equal, you don't need friend functions since you have methods to do that.
Topic archived. No new replies allowed.