I made point class it has two objct x,y. i made  saparate function for giving
values to x & y object but when i run the it provide wrong answer.
i enter firstValue=5 , secondVaue=6. the program shoul provide same answer same
as like(5,6) but result show after excuting is wrong . what was wrong with this.

class  point
private :
int x,y;
public :

void init(int u ,int v)
    void set();
    void print();
void point :: print()
void point ::set()
point  p; int p1,p2;
cout<<"Enter the first no # ";
cin>> p1;
cout<<"Enter the second no # ";
p.init(p1,p2); // we enter p.init(5,6);
int main()
point p;
return 0;


Since you are within a method of the object (set()) you can assign to the private members of the class directly. Get rid of line 34, and insert

x = p1;
y = p2;

(or just take the values from cin directly into x and y)

no dear i want to take values of x & y bye another function.

What do you mean by "I want to take values of x & y by another function"? Do you mean that you want to input them in point::set(), then send them to point::init() for the final assignment?

Regardless of what you mean to do, the biggest problem is that you are storing your results to a point object called "p" that is local to point::set(). When the function/method ends, "p" is destroyed and the data is lost. You need to get rid of that local object.

Edited 5 Years Ago by Fbody: n/a

This question has already been answered. Start a new discussion instead.