Class member functions [code]

I tried posting the code over and over again in the last post but it didnt work...

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
31
32
33
34
35
36
37
38
#include <iostream>

using namespace std;

class point
{
    public:
    point() {x = 0; y = 0;}
    point(double p, double q) {x = p; y = q;}
    double x;
    double y;
    void print();
    point midpt(point, point);
};


int main()
{
    point a;
    a.print();
    point b(1,1);
    b.print();
    point m = midpt(a,b);
    m.print();
    return 0;
}
void point::print()
{
    cout << x << "," << y;
}

point point::midpt(point a, point b)
{
    point m;
    m.x = (a.x + b.x) / 2;
    m.y = (a.y + b.y) / 2;
    return m;
}
On line 23
 
point m = midpt(a,b);


This is a member function, so you wouldn't be able to call it this way. It has to be called by a "point" object using the dot "." modifier.

without changing much of you code you could call it this way

 
point m = m.midpt( a, b );

Last edited on
Topic archived. No new replies allowed.