Member Avatar for HASHMI007
/*
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.
*/


#include<iostream.h>
class  point
{
private :
int x,y;
public :

void init(int u ,int v)
    {
     x=u;y=v;
    }
    void set();
    void print();
};
void point :: print()
{
cout<<"("<<x<<","<<y<<")";
}
void point ::set()
{
point  p; int p1,p2;
cout<<"Enter the first no # ";
cin>> p1;
cout<<"Enter the second no # ";
cin>>p2;
p.init(p1,p2); // we enter p.init(5,6);
}
int main()
{
point p;
p.set();
p.print();
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)

Member Avatar for HASHMI007

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

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.

Member Avatar for HASHMI007

okay . i understand

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.