C++/am i correct?
Expert: vijayan - 3/15/2010
QuestionQUESTION: Hello vijayan,
here is a question i tried to solve, could you check my solution and let me know if i am close and if any error, fix it abit for me?
question:
To define a point in a two-dimensional coordinate system, it is most common to use the form (x,y), called rectangular coordinates. Construct a class Point which describes points in this way. Write appropriate constructors and a member function which calculates the distance between a given point and another. The distance between two points (x1,y1) and (x2,y2) is given by the formula: d = √((x1-y1)2+(x2-y2)2)
my solution
===============================================================
#include<iostream>
#include<math.h>
using namespace std;
class point{
int x, y;
point(int a = 0, int b=0);
int cal_Distance(point &b);
};
point ::point(): x(a),y(b){}
int point::cal_Distance(point &b)
{
int d;
d = sqrt((x-y)^2)+(b.x-b.y)^2);
return d;
}
int main()
{
point a(2,1);
point b(3,1);
float d;
d = (float)a.call_Distance(point &b);
cout<<d<<endl;
getche();
}
===============================================================
let me know of my errors by commenting them when correcting this code..
thanks..
ANSWER: class point
{
int x, y;
//add access specifier
public:
point(int a = 0, int b=0);
// not const-correct
// int cal_Distance(point &b);
// modify as (return a double, add const)
double cal_Distance( const point &b ) const ;
};
point::point( int a, int b ): x(a), y(b){} // *** parameters added
double point::cal_Distance( const point &b ) const
{
int x2 = b.x ;
int y2 = b.y ;
return sqrt( ( (x-y) * (x-y) ) + ( (x2-y2) * (x2-y2) ) ) ; // *** corrected
}
int main()
{
point a(2,1);
point b(3,1);
double d;
d = (float)a.cal_Distance(b); // *** corrected
cout<<d<<endl;
//getche(); // *** deleted
}
---------- FOLLOW-UP ----------
QUESTION: Hello vijayan,
thnks, i saw my errors and i corrected them. here is a full version of the different codes:
==========================================================================
using namespace std;
class point{
int x, y;
public:
point(int a = 0, int b=0);
double cal_Distance(point &b) const;
};
point ::point(int a, int b): x(a),y(b){}
double point::cal_Distance(point &b) const
{
int x2 = b.x;
int y2 = b.y;
return sqrt( ( (x-y) * (x-y) ) + ( (x2-y2) * (x2-y2) ) ) ;
}
int main()
{
point a(2,1);
point b(3,1);
double d;
d = a.cal_Distance(b);
cout<<d<<endl;
getche();
}
=======================================================================
my next task now is a conversion from polar coordinates to ordinary rectangular coordinates and this can be effected with the formula
X = r.cosϴ
Y = r.sinϴ
First define a class RPoint, where the points are defined using polar coordinates(that i have done using the class Pointer. just rename the Pointer class as RPoint), then write a type conversion constructor for class Point which converts RPoint to a Point.
i got no clue how to write a type conversion constructor class. so, i am counting on your expertise to define this and i can learn how to do that.. you could give a small work frame around what i have done so that i am inline with your way.
Answer> i got no clue how to write a type conversion constructor class
A type conversion constructor for a class is a non-explicit constructor in the destination type that can be invoked with a single argument of the source type. In your case, the constructor would be:
Point::Point( const RPoint& rpt ) ;