C++代码学习

#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;

}

这是一个简单的,计算两个点之间欧式距离,即(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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值