64,647
社区成员
发帖
与我相关
我的任务
分享
//Point.H
#ifndef _POINT_H
#define _POINT_H
#include <iostream>
#include <math.h>
using namespace std;
class Point{
private:double x,y;
public:Point(){}
~Point();
void set();
void display();
static double lenth(const Point & ,const Point &);
};
void Point::set ()
{
int a,b;
char c;
cout << "please input x and y:";
cin >> a ;
c=getchar();
cin >> b ;
x=a;
y=b;
}
Point::~Point ()
{
cout << "P(" << x <<"," <<y << ")" << "has been deleted." << endl;
}
void Point::display()
{
cout << "(" << x <<"," << y << ")" << " ";
}
double Point::lenth(const Point & P1,const Point & P2 )
{
double lenth;
lenth=pow((pow(P1.x-P2.x,2)+pow(P1.y-P2.y,2)),0.5);
return lenth;
}
#endif
//main.cpp
#include <iostream>
#include "Point.h"
using namespace std;
int main()
{
Point* ptp;
ptp = new Point[10];
for(int i=0;i <10;i++)
{
cout << "P" << i+1 << ":";
ptp[i].set ();
}
cout << endl;
cout << "ten points:";
for(int j=0;j <10;j++)
{
ptp[j].display ();
}
cout << endl;
double lenth=0;
for(int k=0;k <9;k++)
{
lenth+=Point::lenth (ptp[k],ptp[k+1]);
}
cout << "the lenth of the lines connecting this ten points is " << lenth << endl;
delete[] ptp; // 楼主用new忘记释放了.
return 0;
}
#include <iostream>
#include <math.h>
using namespace std;
class Point
{
private:
double x,y;
public:
Point(){}
~Point();
void set();
void display();
static double lenth(const Point & ,const Point &);
};
void Point::set ()
{
int a,b;
char c;
cout << "please input x and y:";
cin >> a ;
c=getchar();
cin >> b ;
x=a;
y=b;
}
Point::~Point ()
{
cout << "P(" << x <<"," <<y << ")" << "has been deleted." << endl;
}
void Point::display()
{
cout << x <<"," << y << " ";
}
double Point::lenth(const Point & P1,const Point & P2 )
{
double lenth;
lenth=pow((pow(P1.x-P2.x,2)+pow(P1.y-P2.y,2)),0.5);
return lenth;
}
#include <iostream>
using namespace std;
int main()
{
Point* ptp;
ptp = new Point[10];
for(int i=0;i <10;i++)
{
cout << "P" << i+1 << ":";
ptp[i].set ();
}
cout << endl;
cout << "ten points:";
for(int j=0;j <10;j++)
{
ptp[j].display ();
}
cout << endl;
double lenth=0;
for(int k=0;k <9;k++)
{
lenth+=Point::lenth (ptp[k],ptp[k+1]);
}
cout << "the lenth of the lines connecting this ten points is " << lenth << endl;
delete []ptp;//楼主没有调用析构啊,显式的。
return 0;
}