I'm new to C++ and I'm having a hard time with classes
the question is

int main(void)
{
Point ary[5] = { Point(1, 2), Point(3, 4), Point(5, 6) };
Point sum;

sum.SetXY(sum.GetSumX(ary, 5), sum.GetSumY(ary, 5));

sum.Print();

return 0;
}

for this main function how do I make a Point class
and also make and define a GetSumX and GetSumY function.

The results come out as (9, 12) which is the sum of the arrays.

Plz help me~

#include <iostream>
using namespace std;

class CPoint {
private :
int x;
int y;
public :
CPoint(){}
CPoint(int a, int b){}
int SetXY(int a, int b){x=a; y=b;}

int GetSumX(CPoint* pCPoint, int nsize){
int nvalue = 0;
for(int i=0; i<nsize; ++i){
nvalue += pCPoint.x;
}

return nvalue;
}
int GetSumY(CPoint* pCPoint, int nsize){
int nvalue=0;
for(int i=0; i<nsize; ++i){
nvalue += pCPoint.y;
}

return nvalue;
}
void Print(){cout<<"("<<x<<", "<<y<<")"<<endl;}
};


int main(void)
{
int i;
CPoint ary[5] = {CPoint(1,2), CPoint(3,4), CPoint(5,6)};
CPoint sum;

sum.SetXY(GetSumX(ary,5), GetSumY(ary,5));

sum.Print();

return 0;
}

I made it this far but when I compile it
it says that it can't find 'GetSumX', and 'GetSumY'....
Can any anyone plz tell me whats wrong??

PS. Is it possible to use 'friend' for 'GetSumX', and 'GetSumY'
to make the functions outside the class or another way??

Make a class called Point.
Make two member function of type int called perhaps X and Y.
You will have to make a two-arg constructor that will initialize the values of these data member.
Write Two member functions accordingly which takes a array and its size and calculate the sum of abscissa and ordinates respectively.
You will have to write a member function that takes two argument and set the value of X and Y accordingly.
So, write the class and let us know where you face the problem

>it says that it can't find 'GetSumX', and 'GetSumY'....
This is because you didn't make them the member function of the class. You can do this by declaring them inside the class in the public.

class CPoint
{
    ...
    ...
    ...
public:
    ...
    ...
    int GetSumX(CPoint* pCPoint, int nsize)
    {
        int nvalue = 0;
        for (int i=0; i<nsize; ++i)
        {
            nvalue += pCPoint[i].x;
        }

        return nvalue;
    }
    int GetSumY(CPoint* pCPoint, int nsize)
    {
        int nvalue=0;
        for (int i=0; i<nsize; ++i)
        {
            nvalue += pCPoint[i].y;
        }
    }
};

>Is it possible to use 'friend' for 'GetSumX', and 'GetSumY'
Yes it is possible. You must add the declaration of the function on in the declaration of the class. But I don't suggest that you make them friend functions.

Always post your codes in the code tags. [[B]code=c++[/B]][[B]/code[/B]]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.