C++ | 学习笔记

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++程序设计》
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yimtcode

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值