hi people!
i am a beginner in c++ and i wrote this code
this code is designed to get cordinates of 2 points and caleclute the lenght
between those 2 points the problem is that the function named GETCORDINATES that i designed is not working i wanted this code let the user import his points cordinates but programm shows the constructor values only
for sample i run the code and i set x=1 & y=2 but programm well tell me x=0 and y=0 please help tnx
// rectangle.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <math.h>
usingnamespace std;
class point
{
public:
int x, y;
point()
{
x = y =0;
}
staticvoid GetCordinates(point point1) //i wrote this code so when i call it it should set X & Y of the given point
{
cout << "please enter your point cordinates"<<endl;
cout << "x=>";
cin >> point1.x;
cout << "y=>";
cin >> point1.y;
}
private:
};
class line
{
public:
staticdouble line_lengh(point point1, point point2) //this code will caleclute the lenght between 2 points its working right now
{
return sqrt(pow(point1.x - point2.x, 2)+pow(point1.y-point2.y,2));
}
private:
};
int _tmain(int argc, _TCHAR* argv[])
{
point p1,p2;
point::GetCordinates(p1);
cout << "x is "<<p1.x<<endl; // but its not setting cordinates of the point its only show values of constractor that x&y is set 0
cout << "y is "<<p1.y;
getchar(); getchar();
return 0;
}
1/ Make the point1 argument a reference to a point, rather than a copy of a point
1 2 3 4 5 6 7 8
staticvoid GetCordinates(point &point1) // "&" means reference. No "&" means copy
{
cout << "please enter your point cordinates"<<endl;
cout << "x=>";
cin >> point1.x;
cout << "y=>";
cin >> point1.y;
}
2/ Make the GetCordinates function a non-static member function & call it on the point1 object directly
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class point
{
public:
int x, y;
point()
{
x = y =0;
}
/*static */void GetCordinates(/*point point1*/)
{
cout << "please enter your point cordinates"<<endl;
cout << "x=>";
cin >> /*point1.*/x;
cout << "y=>";
cin >> /*point1.*/y;
}
private:
};
and in main:
1 2 3 4 5 6 7 8 9 10
int _tmain(int argc, _TCHAR* argv[])
{
point p1,p2;
//point::GetCordinates(p1);
p1.GetCordinates();
cout << "x is "<<p1.x<<endl;
cout << "y is "<<p1.y;
getchar(); getchar();
return 0;
}