首先

关于class,许多OIer便会认为这是一种非常高深的语法,我们无论是现在还是以后的OI学习中都用不到。

确实,classOI比赛中可以永远不用。

可是,如果这么说的话,除了递归之外,其他函数也可以在OI比赛中永远不使用。可是,那我们使用它的原因是什么呢?

对,是因为它可以使程序编写更加容易。

众所周知,C++相较于C,主要的区别就是在于C++增加了关于面向对象的支持,和STL。由于我们一般并不去使用面向对象,所以我们学习的语言被戏称为"C with STL"

但是,学习面向对象的基本思想,和有关面向对象的基本语法,真的可以让我们对C++语言的认识提升一个档次。

class是什么?

其实,我们在使用struct的时候,就已经对面向对象有了初步的认识。

struct其实就相当于是这样:

1
2
3
4
5
6
struct Node
{
int x;
int y;
int value;
}

相当于:

1
2
3
4
5
6
7
class Node
{
public:
int x;
int y;
int value;
}

没错,也就是说,struct就相当于是一个全部都是publicclass

说到这里,读者对class的望而生畏的感觉应当减少了一些。

但是很显然,class远远不只有这一点点用处。

class

class的基本用法:

1
2
3
4
5
6
7
class a_class
{
private:

public:

}

可以看出,这个class比刚才的那一个多出了一个private:

那这个private有什么用处呢?

private内部的函数,只能在类的内部访问,在外部无论通过什么方法也是无法直接访问的。

在调用类的时候,只能使用public内部的函数。

这样就有了一个好处:

我们在使用一个类的时候,不需要管它内部是怎样实现的,只需要调用在public中的函数接口即可。

这样,虽然我们再写类的时候要仔细思考接口怎样编写比较方便,但是大大减少了我们在写类外部代码时的思维量。

而毕竟写类只需要写一次,调用可能需奥很多很多次,所以我们总体的思维量还是会有所下降。

范例

(下面是我自己在考试的时候写的矩阵快速幂代码:其实本来只是想秀给CYC看的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include<bits/stdc++.h>

using namespace std;

class martix
{
private:
long long nums[6][6];
clear()
{
memset(nums,0,sizeof(nums));
}
make_standard()
{
memset(nums,0,sizeof(nums));
for(int i=1;i<=5;i++)
{
nums[i][i]=1;
}
}
public:
martix()
{
clear();
}
martix operator*(martix b)
{
martix result;
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
for(int k=1;k<=3;k++)
{
result.nums[i][j]+=nums[i][k]*b.nums[k][j]%1000000007;
}
}
}
return result;
}
martix quick_pow(long long k)
{
martix result;
result.make_standard();
martix base;
for(register int i=1;i<=5;i++)
{
for(register int j=1;j<=5;j++)
{
base.nums[i][j]=nums[i][j];
}
}
while(k)
{
if(k&1)
{
result=result*base;
}
k>>=1;
base=base*base;
}
return result;
}
void make_fib()
{
nums[1][1]=1;
nums[1][2]=1;
nums[1][3]=1;
}
void make_base()
{
nums[1][1]=1;
nums[1][2]=1;
nums[2][1]=0;
nums[2][2]=0;
nums[3][1]=1;
nums[3][2]=0;
nums[2][3]=1;
nums[1][3]=0;
}
void print()
{
printf("%I64d\n",nums[1][3]%1000000007);
}
};

const int maxn=2e7+90;
martix fib;
martix base;
int T;

int main()
{
freopen("seq.in","r",stdin);
freopen("seq.out","w",stdout);
cin>>T;
int temp;
for(int i=1;i<=T;i++)
{
cin>>temp;
fib.make_fib();
base.make_base();
fib=fib*base.quick_pow(temp-1);
fib.print();
}
return 0;
}

本文目前只是写了一丁点皮毛。以后应该会持续更新。