c++问题,帮我做一下。
1.定义一个平面上点的类Point,该类包含平面坐标x,y和统计当前创建该对象个数的计数器count、构造函数、析构函数,以及能够输出计数器count的函数。设计程序,实...
1. 定义一个平面上点的类Point,该类包含平面坐标x,y和统计当前创建该对象个数的计数器count、构造函数、析构函数,以及能够输出计数器count的函数。设计程序,实现在任意时候都能够访问计数器。
提示:类Point作如下定义:
class Point
{
private:
int x,y;
static int count; //定义静态变量count,在构造函数中要为count加1,在析构函数中count减1。
public:
/*待添加的部分*/
}
...
2. 设计一个程序,利用友元函数计算上题中平面上两点之间的距离。
提示:平面上任意两点A(x1,y1)B(x2,y2).求它的距离公式:distance=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)). 展开
提示:类Point作如下定义:
class Point
{
private:
int x,y;
static int count; //定义静态变量count,在构造函数中要为count加1,在析构函数中count减1。
public:
/*待添加的部分*/
}
...
2. 设计一个程序,利用友元函数计算上题中平面上两点之间的距离。
提示:平面上任意两点A(x1,y1)B(x2,y2).求它的距离公式:distance=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)). 展开
3个回答
展开全部
class Point
{
private:
int x,y;
static int count; //定义静态变量count,在构造函数中要为count加1,在析构函数中count减1。
public:
/*待添加的部分*/
Point(int x=0, int y=0):x(x),y(y)
{count++;}
~Point(){count--;}
void printCount(){cout<<"count="<<count<<endl;}
int getCount(){return count;}//题目没有要求,这个可以没有
};
int Point::count=0;
---------------------------------------------------------------
#include <iostream>
#include <cmath>
using namespace std;
class Point{
double x, y;
public:
Point(double x, double y):x(x),y(y){}
friend double distance(Point p1, Point p2);
};
double distance(Point p1, Point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
int main()
{
Point a(1,2), b(3,4);
cout << distance(a,b) << "\n";
{
private:
int x,y;
static int count; //定义静态变量count,在构造函数中要为count加1,在析构函数中count减1。
public:
/*待添加的部分*/
Point(int x=0, int y=0):x(x),y(y)
{count++;}
~Point(){count--;}
void printCount(){cout<<"count="<<count<<endl;}
int getCount(){return count;}//题目没有要求,这个可以没有
};
int Point::count=0;
---------------------------------------------------------------
#include <iostream>
#include <cmath>
using namespace std;
class Point{
double x, y;
public:
Point(double x, double y):x(x),y(y){}
friend double distance(Point p1, Point p2);
};
double distance(Point p1, Point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
int main()
{
Point a(1,2), b(3,4);
cout << distance(a,b) << "\n";
更多追问追答
追问
还有一题您继续我可以加分。
追答
return 0;
}
展开全部
#include "math.h"
class Point
{
public:
Point()
{
X = Y = 0;
count++;
}
Point(IN INT x,
IN INT y)
{
X = x;
Y = y;
count++;
}
~Point()
{
count--;
}
//得到计数器
int GetCount()
{
return count;
}
//友元函数计算两点之间的距离
friend double GetDistance(Point point1,Point point2);
public:
INT X;
INT Y;
private:
static int count;
};
//友元函数计算两点之间的距离
friend double GetDistance(Point point1,Point point2)
{
return sqrt((point1.X-point2.X)*(point1.X-point2.X) + (point1.Y-point2.Y)*(point1.Y-point2.Y));
}
class Point
{
public:
Point()
{
X = Y = 0;
count++;
}
Point(IN INT x,
IN INT y)
{
X = x;
Y = y;
count++;
}
~Point()
{
count--;
}
//得到计数器
int GetCount()
{
return count;
}
//友元函数计算两点之间的距离
friend double GetDistance(Point point1,Point point2);
public:
INT X;
INT Y;
private:
static int count;
};
//友元函数计算两点之间的距离
friend double GetDistance(Point point1,Point point2)
{
return sqrt((point1.X-point2.X)*(point1.X-point2.X) + (point1.Y-point2.Y)*(point1.Y-point2.Y));
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
Point()
{
x=0;
y=0;
count++;
}
~Point()
{
Point::count--;
}
Point(int a,int b)
{
x = a;
y = b;
count++;
}
void Set(int a,int b);
static void PrintCount()
{
cout<<"count="<<count<<endl;
}
friend double Distance(Point pt1,Point pt2);
private:
int x,y;
static int count;
};
void Point::Set(int a,int b)
{
x = a;
y = b;
}
double Distance(Point pt1,Point pt2)
{
return sqrt((pt2.x-pt1.x)*(pt2.x-pt1.x)+(pt2.y-pt1.y)*(pt2.y-pt1.y));
}
int Point::count=0;
int main()
{
Point a(3,4);
Point b(7,9);
Point::PrintCount();
cout<<"a(3,4)与b(7,9)距离为:"<<Distance(a,b)<<endl;
system("pause");
return 0;
}
#include <cmath>
using namespace std;
class Point
{
public:
Point()
{
x=0;
y=0;
count++;
}
~Point()
{
Point::count--;
}
Point(int a,int b)
{
x = a;
y = b;
count++;
}
void Set(int a,int b);
static void PrintCount()
{
cout<<"count="<<count<<endl;
}
friend double Distance(Point pt1,Point pt2);
private:
int x,y;
static int count;
};
void Point::Set(int a,int b)
{
x = a;
y = b;
}
double Distance(Point pt1,Point pt2)
{
return sqrt((pt2.x-pt1.x)*(pt2.x-pt1.x)+(pt2.y-pt1.y)*(pt2.y-pt1.y));
}
int Point::count=0;
int main()
{
Point a(3,4);
Point b(7,9);
Point::PrintCount();
cout<<"a(3,4)与b(7,9)距离为:"<<Distance(a,b)<<endl;
system("pause");
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询