Class member functions

Hello everyone

Can we create a function that is defined as a member of a class (lets say class point) and of type "point" and that takes 2 parameters both of type point? I ve been trying to do that but the program doesnt seem to compile...

Thanks!!
Post some code please.
of type 'point'??? please post the codings, its difficult to understand what your prolem is.
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;
}
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;
}
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;
}
#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;
}
#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;
}
Why don't you try calling in main this:
point m = a.midpt(a,b);

or this:
point m = b.midpt(a,b);

instead of this:
point m = midpt(a,b);

After all, you declare midpt as a member function. And member functions need to be called for a specific object.

Oh, and multi-posting your code won't have your problem solved faster... It's more likely the other way around...

EDIT: Sorry, I just saw the other post where you say you had trouble posting the code here...
Last edited on
Topic archived. No new replies allowed.