385 字
2 分钟
OOP第一课笔记
2025-03-06

由于这个人什么也不会,所以她做了C++的语言笔记。


这节课主要讲了cpp标准库、输入输出、string类、文件I/O。


基础#

#include <iostream>
//标准库(输入输出)
using namespace std;
int main()
{
    int age;
    cout << "hello!" << endl;//控制台输出,endl是换行符
    cin >> age;//控制台输入

    return 0;
}

终端编译:

g++ -o hello hello.cpp # 优化编译,同目录下产生hello文件
# 或者
g++ hellp.cpp # 直接编译,同目录下产生a.out文件
# 也可以加上c++版本要求
g++ hello.cpp -std=c++11 # 指定C++11标准

编译文件运行:

./hello
# 或者
./a.out
  • 关于C++:build on C
  • C++ improvements
    • Support for OOP
    • Templates
    • STL
    • Exception Handling
    • etc.

The string class#

#include <string>
using namespace std;
int main(){
    string str;
    string str1 = "Hello";
    cin >> str;
    cout << str;

    return 0;
}

Assignment:#

char cstr1[20];
char cstr2[20] = "hello";

string str1;
string str2 = "abc";

cstr1 = cstr2; // illegal
str1 = str2; //legal

结论:字符串之间可以相互赋值,char型字符数组不能赋值字符串。

Concatenation#

str3 = str1 + str2;
str2 += str1;

Constructors#

string (const char *cp, int len);

string (const string& s2, int pos);

string (const string& s2, int pos, int len);

Substring#

substr (int pos, int len);
find (const string& str);

Example#

// e.g.
str1 =  "hello, china!"; //assign
string str1("hello, china!"); //assign
string str2(str1); //copy
string str2 = str1; //copy
string str3(str1, 7, 5); //"china"
string str3 = str1.substr(7, 5); //"china"
str1.replace(7, 5, "hangzhou"); //"hello, hangzhou!"
str1.assign(5, 'A'); //"AAAAA" 重新覆盖赋值

string str4 = "hello, hangzhou city";
string str_to_find = "hangzhou";
str4.replace(str4.find(str_to_find), str_to_find.length(), "shanghai") //search and replace

File I/O#

#include <ifstream> //read from file
#include <ofstream> //write to file
// 两者可以合用<fstream>代替 (?)
ofstream File1("C:\\test.txt");
File1 << "Hello, China!" << std::endl;

ifstram File2("C:\\test.txt");
std::string str;
File2 >> str;
OOP第一课笔记
https://herobrine101.top/posts/oop第一课笔记/
作者
发布于
2025-03-06
许可协议
CC BY-NC-SA 4.0