class用法讲解
class 用法讲解
注意,代码中[]中的内容表示可写可不写。
1 struct 的回顾
如果不想回顾,请跳转至2。
1.1 struct 的定义
格式
struct 结构体名
{
类型 成员变量1 [ = 初值];
类型 成员变量2 [ = 初值];
};
例子
struct Edge
{
int u, v;
};
struct Edges
{
Edge edges[10005];
};
注意:
- 成员变量的类型可以是一个结构体。
1.2 变量的定义
格式
[struct] 结构体名 变量名 [ = { 初值1,初值2 }];例子
Edge edge = { 0, 1 }; Edges edges = {{edge}}; struct Node { int a, b, c; }node;注意事项
- 必须按照定义的顺序赋初值。
- 变量名可以与成员变量名一致。
- 变量可以直接定义在结构体后面。
1.3 成员变量的引用
格式如下
变量名.成员变量名例子
edge.u = 1; edges.edges[0].v = 2;注意事项
- 可以引用成员变量的成员变量。
2 struct 的高级用法
2.1 成员函数
格式
struct 结构体名 { 返回类型 成员函数名(参数列表) [const]; }; [struct] 返回类型 结构体名::成员函数名(参数列表) [const] { 函数体 }或者
struct 结构体名 { 返回类型 成员函数名(参数列表) [const] { 函数体 } }解释和说明
参数列表后的 const 表示是只读函数(即不会修改任何成员变量)。
例子
struct Coord { int x, y; bool IsOutBound(int, int) const; void Print() const; void GoDown(); } int main() { Coord playerCoord = { 1, 1 }; int n = 10, m = 10; while (true) { playerCoord.Print(); char ch = getchar(); if (ch == ' ') { playerCoord.GoDown(n, m); } else if (ch == 16)//ESC { break; } else { puts("Input Error\n"); } } } bool Coord::IsOutBound(int n, int m) const { return (1 <= x && x <= n && 1 <= y && y <= m); } void Coord::Print(int n, int m) { system("cls"); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (i == x && j == y) { puts("@"); } else { puts("."); } } } } void Coord::GoDown(int n, int m) { int tmp = x; ++x; if (IsOutBound(n, m)) { x = tmp; } }注意事项
- 标名有 const 的函数,内部不能修改成员变量(仅限成员变量)。
2.2 public 与 private
格式
struct 结构体名 { public: 类型 成员变量名 [ = 初值]; 类型 成员函数名(参数列表) [const]; private: 类型 成员变量名 [ = 初值]; 类型 成员函数名(参数列表) [const]; };例子
struct Node { public: Node* GetPrecursor() const { return pre; } Node* GetNext() const { return next; } void SetPrecursor(Node* _pre) { pre = _pre; } void SetNext(Node* _next) { next = _next; } int data; private: Node* pre = nullptr; Node* next = nullptr; }; int main() { Node head; head.data; Node node1; node1.data = 1; head.SetNext(&node1); node1.SetPrecursor(&head); printf("head's next is %d\n", head.GetNext()->data); return 0; }解释和说明
public 后的成员变量和成员函数,再结构体外也可以访问,private 后的则外部不能访问,只有结构体内可以。如对于例子的结构体,在 main 函数中编写一下语句是不可以的
printf("head's next is %d\n", head.next->data);这样可以放置其他的程序员使用你的结构体时,不会误改一些成员变量。一般讲成员函数设在 public 后,成员变量设在 private 后。
2.3 结构体的继承与 protected
格式
struct 结构体名 : 继承方式1 结构体名1,继承方式2 结构体名2... { public: //... private: //... protected: //... };例子
struct Petal { public: void Hurt(int damage) { curhp -= max(0, damage - armor); curhp = max(0, curhp); } bool IsDie() { return curhp == 0; } protected: string name; int maxhp; int curhp; int atk; int armor; int cooling; }; struct Basic : public Petal{}; struct Rose : public Petal { public: int cooling2; int retBlood; };解释与说明
基类在派生类中的访问属性如下表所示。 |基类中成员类型|私有继承(private)|公有继承(public)|保护继承(protected)| |:--------:|:--------:|:--------:|:--------:| |私有成员(private)|不可访问 |不可访问 |不可访问 | |公有成员(public)|私有 |共有 |保护 | |保护成员(protected)|私有 |保护 |保护 |
继承与派生是同一个过程从不同的角度看:
- 保持已有类的特性而构成新类的过程称为继承
- 在已有类的基础上新增自己的特性而产生新类的过程称为派生
- 被继承的已有类成为基类(父类)base class
- 派生出的新类称为派生类(子类)derived class
- 直接参与派生出某类的基类成为直接基类
- 基类的基类甚至更高层的基类称为间接基类
保护成员:在派生类中可以作为public,在基类中类似private。(也就是说,如果想要把基类中的私有成员给派生类用,但是却又不想将此成员作为public成员,可以设定为protected成员,这样可以达到派生类可以访问的效果)。\ \ 私有继承:将除了私有成员的其他成员转换成派生类的私有成员。
注意事项
- 可以在派生类中重定义基类中的函数。
2.4 重载运算符#### 格式
返回类型 operator运算符(参数列表) { 函数体 }例子
ostream& operator <<(ostream& os, Petal petal) { os << petal.name << endl; os << "-------------------"; os << "maxhp |" << maxhp << endl; os << "curhp |" << curhp << endl; os << "atk |" << atk << endl; os << "armor |" << armor << endl; os << "cooling |" << cooling << endl; return os; }解释和说明
例子中的函数是用来重载输出流 cout 的,这样子就能使得输出更方便了。\ 重载运算符函数的参数列表是由运算符来决定的,若运算符为 <<、+、%、^ 等双目的运算符,则它的参数列表有两个参数,它左边的东西和右边的东西。\ 对于 !、~ 等单目的运算符,它的参数有一个参数。\ 有些运算符既可以当坐双目也可以是单目,如 &、*,那么参数列表根据你的需求来决定。\ 特别的,sizeof 也是一个单目运算符。++ 这个运算符有前置和后置两种,为了区分,后置的 ++ 的参数列表另外加一个 int 参数,就像这样。
struct Node { int a, b; }; Node operator++(Node& n, int) { return { n.a++, n.b++ }; } ostream& operator<<(ostream& os, Node n) { os << n.a << " " << n.b << endl; } int main() { Node n; n.a = 1; n.b = 2; cout << n++; return 0; }$\color{white}x_2=\lfloor\omega\cdot\sqrt[3]{-\frac{q}{2}+\sqrt{(\frac{q}{2})^2+(\frac{p}{3})^3}}+\omega^2\cdot\sqrt[3]{-\frac{q}{2}-\sqrt{(\frac{q}{2})^2+(\frac{p}{3})^3}}\rfloor - \frac{b}{3a}$\ $\color{white}x_2=\lfloor\omega^2\cdot\sqrt[3]{-\frac{q}{2}+\sqrt{(\frac{q}{2})^2+(\frac{p}{3})^3}}+\omega\cdot\sqrt[3]{-\frac{q}{2}-\sqrt{(\frac{q}{2})^2+(\frac{p}{3})^3}}\rfloor - \frac{b}{3a}$\ 其中 :\ $\color{white}p=\frac{3ac-b^2}{3a^2},q=\frac{2b^3-9abc+27a^2d}{27a^2},\omega=e^{2\pi i/3}=-\frac{1}{2}+\frac{\sqrt{3}}{2}i$\ `Node operator++(Node& n, int)` 就是前置自增。\ 还有,重载运算符还可以定义在结构体里面。这时候,参数表里的第一个参数就自动地填充为所属的结构体,你只需要填 1~2 个参数就行了。\ 另外,像 [],=,+= 等运算符,只能定义在结构体里。 ## 3 class 用法 ### 3.1 替换 我上面讲的所有,都是为了让你熟悉 class。来,让我们把之前的所有代码,struct 都改为 class,那么,恭喜你,你学会了 class 的用法。 ### 3.2 不同 结构体和类的唯一的不同,就是当你没有写 public/private/protected 时,结构体默认为公有,类默认为私有,举个例子。 ``` struct A { int a, b;//公有 }; class B { B() { } int a, b;//私有 }; ``` ### 3.3 模板 我们都知道 vector,stack,queue,deque,map 这些 STL 容器,你知道为什么它们定义是总是要加尖括号吗? #### 格式 ``` template<类型形式参数表> 类声明 ``` #### 例子 ``` template<class Tp> class Node { public: Node() { } Node& operator+=(Tp num) { data += num; return *this; } private: Tp data; Node* left; Node* right; Node* root; }; ``` #### 解释和说明 懒得说明了,看到例子你应该就懂了。 #### 注意事项 一个模板仅限于它下面的一个类,过后就失效。 ### 3.3 构造函数和析构函数的定义 #### 格式 ``` class 类名 { public: 类名(参数列表) [: 成员变量1(初值), 成员变量2(初值)...] { 函数体 } ~类名() { 函数体 } }; ``` #### 例子 ``` class Node { public: Node(int _a = 0, int _b = 0, int _c = 0): a(_a), b(_b), c(_c), next(nullptr); { b += c; a += b; } ~Node() { delete next; next = nullptr; } private: int a; int b; int c; Node* next; }; ``` #### 注意事项 - 构造函数和析构函数没有返回值
- 构造函数和析构函数名字只能是类名
- 一个类必须要有一个构造函数
3.4 构造函数和析构函数的调用
格式
[class] 类名 变量名(参数列表);或者
[class] 类名 变量名; 变量名 = 类名(参数列表);例子
Node node1(1, 2, 3); Node node2 = Node(1, 2, 3);解释和说明
构造函数在变量定义时自动调用,析构函数在变量作用域退出时调用。
完结撒花