C++类

类定义:
定义一个类需要使用关键字 class,然后指定类的名称,并类的主体是包含在一对花括号中,主体包含类的成员变量和成员函数。

1
2
3
4
5
6
7
8
9
10
11
↓关键字
class Box
{
public: --> public/private/protected
double length; --> 变量
double breadth;
double height;
void func(){ --> 函数,称为方法

}
}; --> 以分号结尾

访问修饰符

一个类可以有多个 public、protected 或 private 标记区域。每个标记区域在下一个标记区域开始之前或者在遇到类主体结束右括号之前都是有效的。成员和类的默认访问修饰符是 private。

公有成员public在程序中类的外部是可访问的。您可以不使用任何成员函数来设置和获取公有变量的值,如下所示:

私有成员private变量或函数在类的外部是不可访问的,甚至是不可查看的。只有类和友元函数可以访问私有成员。

受保护成员protected变量或函数与私有成员十分相似,但有一点不同,protected(受保护)成员在派生类(即子类)中是可访问的。

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
#include <iostream>

using namespace std;

class Line{
public:
double length;
void setLength( double len );
double getLength( void );
void setWidth( double wid );
double getWidth( void );
private:
double width;
};

// 成员函数定义
double Line::getLength(void){
return length ;
}

void Line::setLength( double len ){
length = len;
}
double Box::getWidth(void){
return width ;
}

void Box::setWidth( double wid ){
width = wid;
}

// 程序的主函数
int main( ){
Line line;

// 设置长度
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;

// 不使用成员函数设置长度
line.length = 10.0; // OK: 因为 length 是公有的
cout << "Length of line : " << line.length <<endl;

// 不使用成员函数设置宽度
// box.width = 10.0; // Error: 因为 width 是私有的
box.setWidth(10.0); // 使用成员函数设置宽度
cout << "Width of box : " << box.getWidth() <<endl;
return 0;
}

例:string类

创建对象:

1
2
3
4
string s1;
string s2 = "c++";
string s3 = s2;
string s4 (5, 's');

• s1 未使用初始化参数,即默认初始化为空字符串
• 对象类型 对象 = 参数1 , 表示用参数1初始化对象。这里 s2 使用C字符串初始化;s3 用 s2 初始化(又称拷贝复制)。
• 对象类型 对象(参数列表)适用有参数的对象初始化。这里 s4 初始化为 5 个 s;
• string s2 = "c++" 不是赋值运算,它等价于 string s2("C++") ,是初始化。

string 对象的运算

string类实现了以下运算符的重载
• 联接运算:
+连接两个字符串或者一个字符串和一个字符
• 比较运算:
==,!=,<,<=,>,>=,<=>(c++20起),以字典序比较两个字符串
• 下标运算:
[],访问指定字符,返回指定字符引用,即可作为左值
• 赋值运算:
=,+=,右操作数可以是字符串,字符,C字符串,字符数组

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
#include<string>
using namespace std;
int main() {
string s1="hello ",s2,s3;
cin >> s2;
s3 = s1 + s2;
cout << s3 << endl;
s1 += {'s','y','s','u'}; //没\0
cout << s1 << endl;
return 0;
}

string类的成员函数

• 基本操作:length,c_str
• 查找:find, rfind
• 添加,插入,删除:append, insert, erase, clear
• 提取子字符串:substr
• 比较:compare
• 替换:replace
• 复制与交换:copy, swap
• 数值转换(C++11):stoi, stod

基本操作:

length() 返回类型 size_t 的字符串的长度
c_str() 返回类型 const char * 的 C 风格字符串。以null结束。

1
2
3
4
5
6
7
int main() {
string s = "sysu-computer";
int len = s.length();
const char* cs = s.c_str();
cout << cs <<" len is "<< len << endl;
return 0;
}

查找、替换:

find(string|char*|char s , [int pos=0] ) 寻找首个等于给定字符序列的子串。搜索始于 pos,返回类型 size_t 的位置。rfind 从右边开始搜索
replace(int pos, int count, string | char* | char s) 替换指定范围内容

1
2
3
4
5
6
7
int main(){
string s = "sysu-computer";
int pos = s.find("computer");
s.replace(pos,8,"software");
cout << s << endl;
return 0;
}

• 添加,插入,删除:
append(string) 后附 string str
append(int count, char ch) 后附count个字符 ch
insert(int index, int count, char ch) 在index位置插入 count 个 ch
insert(int index, string|char* s) 在index 位置插入 s
erase(int index, int count) 删除从index开始的 count个字符
clear() 清空

1
2
3
4
5
6
7
int main() {
string s = "ysu";
s.insert(0,1,'S').append(1,'-').append("Xcomputer");
s.erase(5,1);
cout << s << endl;
return 0;
}

子串、比较、拷贝、交换:
substr(int pos, int count) 返回子串对象.
compare(string|char* str) 与str比较,返回 1,0,-1之一.
compare(int pos, int count,string|char* str) 字串与str比较,返回1,0,-1之一.
copy(string|char* dest, int pos, int count) 将子串复制到目标对象.
swap(string other) 与 other 交换内容.

1
2
3
4
5
6
7
8
int main() {
string s = "Sysu-Computer";
cout << s.substr(0,4) << " ";
if (s.compare(5,8,"Computer")==0) {
cout << "OK!" << endl;
}
return 0;
}