C++ | 学习笔记
1. Hello world
#include <iostream>
int main()
{
std::cout << "Hello, World!" << std::endl;
return 0;
}
2. 函数
函数重载
#include <iostream>
int sum(int, int);
int sum(int, int, int);
int main()
{
std::cout << sum(1, 2) << std::endl; // Output: 3
std::cout << sum(1, 2, 3) << std::endl; // Output: 6
return 0;
}
int sum(int i1, int i2)
{
return i1 + i2;
}
int sum(int i1, int i2, int i3)
{
return i1 + i2 + i3;
}
3. 动态分配内存
#include <iostream>
int main()
{
double *p;
p = new double[3];
for(int i=0; i<3;i++)
{
std::cin >> *(p+i);
}
for (int i =0; i<3;i ++)
{
std::cout << *(p+i) << std::endl;
}
delete p;
return 0;
}
4. 引用
#include <iostream>
int main()
{
double d1 = 1.1;
double &d2 = d1;
d1 = 9.9;
std::cout << d1 << std::endl; // Output: 9.9
std::cout << d2 << std::endl; // Output: 9.9
return 0;
}#
include <iostream>
int main()
{
// 引用变量
double d1 = 1.1;
double &d2 = d1;
d1 = 9.9;
std::cout << d1 << std::endl; // Output: 9.9
std::cout << d2 << std::endl; // Output: 9.9
// 引用数组
typedef double array[3];
array a = {1, 2, 3};
array& b = a;
for (int i=0; i<3;i ++)
{
std::cout << a[i] << ','; // Output: 1,2,3,
}
return 0;
}
5. 类
5.1. 构造函数和析构函数
#include <iostream>
using namespace std;
struct Point {
double x, y;
// 构造函数
Point() {};
// 构造函数
Point(double a, double b) {
x = a;
y = b;
};
void SetXY(double a, double b) {
x = a;
y = b;
}
void Display() {
cout << x << ", " << y << endl;
}
};
int main() {
Point p{};
p.SetXY(1.1, 2.2);
p.Display(); // Output: 1.1, 2.2
return 0;
}
6. 字符串
#include <iostream>
using namespace std;
int main() {
// 声明
string str1("hello world!");
string str2 = "What are you name?";
cout << str1 << endl; // Output: hello world!
cout << str2 << endl; // Output: What are you name?
// 长度
cout << str1.size() << endl; // Output: 12
// 截取
cout << str1.substr(0, 3) << endl; // Output: hel
// 查找位置
cout << str1.find("world", 0) << endl; // Output: 6
return 0;
}
7. 函数
默认值
#include <iostream>
using namespace std;
int sum(int a, int b = 0);
int main() {
cout << sum(1) << endl; // Output: 1
cout << sum(1, 2) << endl; // Output: 3
return 0;
}
int sum(int a, int b) {
return a + b;
}
8. 类
8.1. 构构方法与析构方法
#include <iostream>
using namespace std;
class Point {
private:
int x, y;
public:
// 构造方法
Point() : x(-1), y(-1) {
cout << "default init" << endl;
}
// 构造方法
Point(int a, int b) {
cout << "init" << endl;
this->x = a;
this->y = b;
}
void PrintInfo() const {
cout << this->x << ", " << this->y << endl;
}
// 析构方法
~Point() {
cout << "destroy" << endl;
}
};
int main() {
Point a;
Point b(15, 25);
a.PrintInfo();
b.PrintInfo();
return 0;
}
8.2. 继承
#include <iostream>
using namespace std;
class Animal {
protected:
string name;
public:
Animal(string name) {
this->name = name;
}
};
class Pig : public Animal {
protected:
float weight;
public:
Pig(string name, float weight) : Animal(name) {
this->weight = weight;
}
void PrintInfo() {
cout << "name: " << this->name << ", weight: " << this->weight << endl;
}
};
int main() {
Pig pig("佩奇", 99);
pig.PrintInfo();
return 0;
}
9. 类模板
#include <iostream>
using namespace std;
template<class T>
class Example {
T x, y;
public:
Example(T X, T Y) : x(X), y(Y) {}
T getx() { return x; }
T gety() { return y; }
};
int main() {
Example<int> e(1, 2);
cout << "x=" << e.getx() << ", y=" << e.gety() << endl; // Output: x=1, y=2
return 0;
}
10. 运算符重载
#include <iostream>
using namespace std;
class Num {
private:
int i;
public:
Num(int t) {
i = t;
}
Num operator+(const Num n) const {
int t = this->i + n.i;
Num obj(t);
return obj;
}
void PrintInt() const {
cout << this->i << endl;
}
};
int main() {
Num n1(1), n2(3);
Num n3 = n1 + n2;
n3.PrintInt(); // Output: 4
return 0;
}
11. 参考
- 《C++程序设计》