标题 | 阅读 | 评论 | 转发 | 发布日期 |
---|---|---|---|---|
暂时没有公开的博文,给他留言提醒一下吧。 | ||||
亘井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
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;
}