#include<iostream>
#include<cmath>
using namespace std;
class point
{
public:
point(int x = 0, int y = 0) :x(x), y(y) { count++; }
point(point&p)
{
x = p.x;
y = p.y;
count++;
}
~point(){ count--; }
int getX() { return x; }
int getY() { return y; }
static void showcount()
{
cout << " Object count=" << count << endl;
}
friend inline float dist(point &p1, point &p2); //内联友好函数
private:
int x, y;
static int count;
};
int point::count = 0;
float dist(point &p1, point &p2)
{
double x = p1.x - p2.x;
double y = p1.y - p2.y;
return static_cast<float> (sqrt(x*x+y*y));
}
int main()
{
point a(4, 5);
cout << "point.A:" << a.getX() << "," << a.getY();
point::showcount();
point b(a);
cout << "Point B:" << b.getX() << "," << b.getY();
b.showcount();
point myp1(1, 1), myp2(4, 5);
cout << "The distance is :";
cout << dist(myp1, myp2) << endl;
return 0;
#include<cmath>
using namespace std;
class point
{
public:
point(int x = 0, int y = 0) :x(x), y(y) { count++; }
point(point&p)
{
x = p.x;
y = p.y;
count++;
}
~point(){ count--; }
int getX() { return x; }
int getY() { return y; }
static void showcount()
{
cout << " Object count=" << count << endl;
}
friend inline float dist(point &p1, point &p2); //内联友好函数
private:
int x, y;
static int count;
};
int point::count = 0;
float dist(point &p1, point &p2)
{
double x = p1.x - p2.x;
double y = p1.y - p2.y;
return static_cast<float> (sqrt(x*x+y*y));
}
int main()
{
point a(4, 5);
cout << "point.A:" << a.getX() << "," << a.getY();
point::showcount();
point b(a);
cout << "Point B:" << b.getX() << "," << b.getY();
b.showcount();
point myp1(1, 1), myp2(4, 5);
cout << "The distance is :";
cout << dist(myp1, myp2) << endl;
return 0;
}
这是一个简单的,计算两个点之间欧式距离,即(1,1)到(2,5)。首先定义了点a,然后将a中的值赋给b,需要理解和记住b(a)这种赋值方法。
#include <iostream>
using namespace std;
int abc(int a, int b, int c)
{
if (a <= 0 || b <= 0 || c <= 0)
throw "All parameters should be >0";
return a + b*c;
};
int main()
{
cout << "Hello World"<<endl;
try { cout << abc(2, 0, 4) << endl; }
catch (char*e)
{
cout << "The parameters to abc were 2,0,and 4" << endl;
cout << "An exception has been throw" << endl;
cout << e << endl;
return 1;
}
return 0;
}