Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12382
  • 博文数量: 1
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-17 19:15
文章分类

全部博文(1)

文章存档

2014年(1)

我的朋友
最近访客

发布时间:2014-05-29 10:02:23

C++中面向对象和基于对象的线程封装方法......【阅读全文】

阅读(1661) | 评论(7) | 转发(0)
给主人留下些什么吧!~~

亘井2015-12-23 16:24

13


#include <iostream>

using namespace std;

class Sum {
public:
 Sum(int a)
 {
  this->a = a;
  sum += a;
 }
 ~Sum()
 {
  sum -= a;
 }
 static int GetSum()
 {
  return sum;
 }
private:
 static int sum;
 int a;
};

int Sum::sum = 0;

int main()
{
 Sum s1(3), s2(4);
 cout << "sum of s1 and s2 is " <<  Sum::GetSum() << endl;
 return 0;
}

回复  |  举报

亘井2015-12-23 16:24

12




#include <iostream>

using namespace std;

class Goods {
public:
 Goods(int w)
 {
  weight = w;
  totalWeight += w;
 }
 ~Goods()
 {
  totalWeight -= weight;
 }
 static int GetTotalWeight()
 {
  return totalWeight;
 }
private:
 int weight;
 static int totalWeight;
};

int Goods::totalWeight = 0;

int main()
{
 Goods g1(30), g2(40);
 cout << "Total weight of all goods is " << Goods::GetTotalWeight() << endl;
 return 0;
}

回复  |  举报

亘井2015-12-23 16:24

11





#include <iostream>

using namespace std;

class Share {
public:
 Share(int a)
 {
  this->a = a;
 }
 ~Share()
 {}
 int GetA()
 {
  return a;
 }
private:
 static int a;
};

int Share::a = 0;

int main()
{
 Share s1(2);
 cout << s1.GetA() << endl;
 Share s2(5);
 cout << s1.GetA() << endl;
 return 0;
}

回复  |  举报

亘井2015-12-23 16:24

4



#include <iostream>
#include <cmath>

using namespace std;

class Point {
public:
 // Point(double x, double y)
 {
  this->x = x;
  this->y = y;
 }
 friend double Dist(Point &a, Point &b)
 {
  double dx = double(a.x - b.x);
  double dy = double(a.y - b.y);
  return sqrt(dx * dx + dy * dy);
 }
private:
 double x;
 double y;
};

int main()
{
 Point p1(3.0, 6.0), p2(6.0, 8.0);
 cout << "distance = " << Dist(p1, p2) << endl;
 return 0;
}

回复  |  举报

亘井2015-12-23 16:24

3


#include <iostream>

using namespace std;

class Interger {
public:
 Interger(int num)
 {
  this->num = num;
 }
 Interger operator + (const Interger &rhs)
 {
  return Interger(num + rhs.num);
 }
 int GetValue()
 {
  return num;
 }
private:
 int num;
};

int main()
{
 Interger i1(3), i2(4);
 Interger i3 = i1 + i2;
 cout << "i1 = " << i1.GetValue() << endl;
 cout << "i2 = " << i2.GetValue() << endl;
 cout << "i3 = " << i3.GetValue() << endl;
 return 0;
}

回复  |  举报
留言热议
请登录后留言。

登录 注册