修改密码

【2024课程】零基础学会C++编程课程

成品课程

陈远龙老师主讲 & 答疑

课程题单 - T1000

未购买 · 可先试学5节课

课程目录展开/折叠

第72课 面向对象和类

播放快捷键

播放/暂停:空格(或鼠标单击)      全屏:F(或鼠标双击)      退出全屏:Esc

快进10 / 30 / 60秒:方向键→ / Ctrl + 方向键→ / Shift + 方向键→

快退10 / 30 / 60秒:方向键← / Ctrl + 方向键← / Shift + 方向键←

本节课讲解配套PPT&板书:

本节课讲解到的源代码

源代码下载:第72课 面向对象和类-源代码下载

1. 析构函数-1
#include <bits/stdc++.h>
using namespace std;

class MyString {
    private:
        char s[1000]; // 空间放字符串本身 
        int n; // 记录字符串的长度 
};

MyString g_s; // 全局变量 存在于数据段 ,自动管理 

void f()
{
    MyString s2;
}

int main()
{
    MyString s1; // 栈内存空间是自动管理的,不需要手动释放 

    f();

    return 0;
} 
2. 析构函数-2
#include <bits/stdc++.h>
using namespace std;

class MyString {
    private:
        char s[1000]; // 空间放字符串本身 
        int n; // 记录字符串的长度 
};

int main()
{
    MyString *p = new MyString;

    delete p;

    return 0;
} 
3. 析构函数-3
#include <bits/stdc++.h>
using namespace std;

class MyString {
    private:
        char s[1024 * 1024 * 50]; // 空间放字符串本身 
        int n; // 记录字符串的长度 
};

int main()
{
    MyString s1;
    cout << "Hello" << endl;

    return 0;
} 
4. 析构函数-4
#include <bits/stdc++.h>
using namespace std;

class MyString {
    public:
        char *p; // 空间放字符串本身 
        int n; // 记录字符串的长度 
};

int main()
{
    MyString s1;
    s1.p = "hello"; // 写法没问题,但是逻辑不对 

    return 0;
} 
5. 析构函数-5
#include <bits/stdc++.h>
using namespace std;

class MyString {
    private:
        char *p; // 空间放字符串本身 
        int n; // 记录字符串的长度 
    public:
        MyString() // 构造函数 
        {
            p = new char[1024 * 1024 * 50];
        }
        void setValue(char *str)
        {
            strcpy(p, str);
        }
        // 析构函数 在对象被销毁之前,自动调用的清理函数
        // 清理需要手动释放的一些资源:比如堆内存 
        ~MyString()
        {
            cout << "调用析构函数" << endl;
            if (p)
                delete[] p;
        } 
};

void f()
{
    cout << "enter f" << endl;
    MyString s1, s2, s3; 
    cout << "leave f" << endl;
}

int main()
{
    /*
    MyString s1;
    s1.setValue("hello");
    */
    f();
    cout << "main after f()" << endl;

    return 0;
} 
6. 运算符重载-1
#include <bits/stdc++.h>
using namespace std;

class Point {
    public: // private
        int x;
        int y;
    public:
        Point(){}
        Point(int _x, int _y) : x(_x), y(_y){}
        void print()
        {
            cout << x << ' ' << y << endl;
        }
};

// 运算符重载:1.定义成独立的函数 2.定义成类的成员函数

// 1.定义成独立的函数
Point operator+(Point& a, Point& b)
{
    Point c(a.x + b.x, a.y + b.y);
    return c;
}

/*
Student operator+(Student& s1, Student& s2)
{
    ...
} 
 */

int main()
{
    /*
    int a = 1, b = 2;
    int c = a + b;
    */

    Point p1(1, 2), p2(3, 4);
    // p1 + p2; // 运算规则必须是人为定义:相加后得到一个新的坐标点(x1 + x2, y1 + y2)
    Point p3 = p1 + p2;
    // Point p3 = operator+(p1, p2); 与上一行代码等价 
    p3.print(); // (4, 6)

    return 0;
}
7. 运算符重载-2-struct
#include <bits/stdc++.h>
using namespace std;

struct Point {
    int x;
    int y;
    Point(){}
    Point(int _x, int _y) : x(_x), y(_y){}
    void print()
    {
        cout << x << ' ' << y << endl;
    }
};

// 运算符重载:1.定义成独立的函数 2.定义成类的成员函数

Point operator+(Point& a, Point& b)
{
    Point c(a.x + b.x, a.y + b.y);
    return c;
}

int main()
{

    Point p1(1, 2), p2(3, 4);
    // p1 + p2; // 运算规则必须是人为定义:相加后得到一个新的坐标点(x1 + x2, y1 + y2)
    Point p3 = p1 + p2;
    // Point p3 = operator+(p1, p2); 与上一行代码等价 
    p3.print(); // (4, 6)

    return 0;
}
8. 运算符重载-2
#include <bits/stdc++.h>
using namespace std;

class Point {
    private: 
        int x;
        int y;
    public:
        Point(){}
        Point(int _x, int _y) : x(_x), y(_y){}
        void print()
        {
            cout << x << ' ' << y << endl;
        }
        // 友元函数 不是成员函数 
        friend Point operator+(Point& a, Point& b);
};

// 运算符重载:1.定义成独立的函数 2.定义成类的成员函数

// 1.定义成独立的函数
Point operator+(Point& a, Point& b)
{
    Point c(a.x + b.x, a.y + b.y);
    return c;
}

int main()
{

    Point p1(1, 2), p2(3, 4);
    // p1 + p2; // 运算规则必须是人为定义:相加后得到一个新的坐标点(x1 + x2, y1 + y2)
    Point p3 = p1 + p2;
    // Point p3 = operator+(p1, p2); 与上一行代码等价 
    p3.print(); // (4, 6)

    return 0;
}
9. 运算符重载-3
#include <bits/stdc++.h>
using namespace std;

class Point {
    private: 
        int x;
        int y;
    public:
        Point(){}
        Point(int _x, int _y) : x(_x), y(_y){}
        void print()
        {
            cout << x << ' ' << y << endl;
        }
        // 2.定义成类的成员函数
        Point operator+(Point& b)
        {
            return Point(x + b.x, y + b.y); // this->x this->y
        } 

};

// 运算符重载:1.定义成独立的函数 2.定义成类的成员函数

int main()
{

    Point p1(1, 2), p2(3, 4);
    // p1 + p2; // 运算规则必须是人为定义:相加后得到一个新的坐标点(x1 + x2, y1 + y2)
    Point p3 = p1 + p2; // p1 + p2  p1.operator+(p2)
    // Point p3 = p1.operator+(p2);
    p3.print(); // (4, 6)

    return 0;
}
10. 运算符重载-4
#include <bits/stdc++.h>
using namespace std;

class Point {
    private: 
        int x;
        int y;
    public:
        Point(){}
        Point(int _x, int _y) : x(_x), y(_y){}
        void print()
        {
            cout << x << ' ' << y << endl;
        }
        Point operator+(Point& b); // 声明成员函数 

};

// 类外面实现成员函数 
Point Point::operator+(Point& b)
{
    return Point(x + b.x, y + b.y); // this->x this->y
} 

// 运算符重载:1.定义成独立的函数 2.定义成类的成员函数

int main()
{

    Point p1(1, 2), p2(3, 4);
    // p1 + p2; // 运算规则必须是人为定义:相加后得到一个新的坐标点(x1 + x2, y1 + y2)
    Point p3 = p1 + p2; // p1 + p2  p1.operator+(p2)
    // Point p3 = p1.operator+(p2);
    p3.print(); // (4, 6)

    return 0;
}
11. 运算符重载-5
#include <bits/stdc++.h>
using namespace std;

class Point {
    private: 
        int x;
        int y;
    public:
        Point(){}
        Point(int _x, int _y) : x(_x), y(_y){}
        void print()
        {
            cout << x << ' ' << y << endl;
        }
        Point operator+(Point& b); // 声明成员函数 
        friend Point operator+(Point& a, Point& b);
};

// 类外面实现成员函数 
// 2.定义成类的成员函数 
Point Point::operator+(Point& b)
{
    cout << "成员函数重载+" << endl; 
    return Point(x + b.x, y + b.y); // this->x this->y
} 

// 运算符重载:1.定义成独立的函数 2.定义成类的成员函数

// 1.定义成独立的函数
Point operator+(Point& a, Point& b) // 没有this指针 
{
    cout << "独立的函数重载+" << endl; 
    Point c(a.x + b.x, a.y + b.y);
    return c;
}

void print_hello() // 没有this指针 
{
    cout << "hello,world" << endl;
}

int main()
{
    Point p1(1, 2), p2(3, 4);
    // p1 + p2; // 运算规则必须是人为定义:相加后得到一个新的坐标点(x1 + x2, y1 + y2)
    // Point p3 = p1 + p2; // p1 + p2  p1.operator+(p2)
    // Point p3 = p1.operator+(p2);
    Point p3 = operator+(p1, p2);
    p3.print(); // (4, 6)

    return 0;
}
12. 运算符重载-6
#include <bits/stdc++.h>
using namespace std;

class Point {
    private: 
        int x;
        int y;
    public:
        Point(){}
        Point(int _x, int _y) : x(_x), y(_y){}
        void print()
        {
            cout << x << ' ' << y << endl;
        }
        Point operator+(Point& b); // 声明成员函数 
        friend ostream& operator<<(ostream& os, Point& p);
};

// 类外面实现成员函数 
// 2.定义成类的成员函数 
Point Point::operator+(Point& b)
{
    cout << "成员函数重载+" << endl; 
    return Point(x + b.x, y + b.y); // this->x this->y
} 

// 运算符重载:1.定义成独立的函数 2.定义成类的成员函数

ostream& operator<<(ostream& os, Point& p)
{
    /*不应该使用cout 
    // cout << "operator<<" << endl;
    cout << p.x << ' ' << p.y << endl; 
    // cout << p.x << endl;
    */
    os << p.x << ' ' << p.y << endl;
    return os;
}

int main()
{
    Point p1(1, 2), p2(3, 4);
    Point p3 = p1 + p2;
    // p3.print(); // (4, 6)
    cout << p3;

    cout << p3 << p1 << p2; // 支持链式操作 

    string str = "xiaoming";
    // str.print(); 
    cout << str;

    return 0;
}
13. 运算符重载-7
#include <bits/stdc++.h>
using namespace std;

class Point {
    private: 
        int x;
        int y;
    public:
        Point(){}
        Point(int _x, int _y) : x(_x), y(_y){}
        void print()
        {
            cout << x << ' ' << y << endl;
        }
        Point operator+(Point& b); // 声明成员函数 
        friend ostream& operator<<(ostream& os, Point& p);

        // operator<< 不能定义为成员函数
        /* 
        ostream& operator<<(Point& p)
        {
        }
        */
};

// 类外面实现成员函数 
// 2.定义成类的成员函数 
Point Point::operator+(Point& b)
{
    cout << "成员函数重载+" << endl; 
    return Point(x + b.x, y + b.y); // this->x this->y
} 

// 运算符重载:1.定义成独立的函数 2.定义成类的成员函数
ostream& operator<<(ostream& os, Point& p)
{
    os << p.x << ' ' << p.y << endl;
    return os;
}

int main()
{
    Point p1(1, 2), p2(3, 4);
    Point p3 = p1 + p2;
    // p3.print(); // (4, 6)
    cout << p3;

    cout << p3 << p1 << p2; // 支持链式操作 

    string str = "xiaoming";
    // str.print(); 
    cout << str;

    return 0;
}
14. 运算符重载-8
#include <bits/stdc++.h>
using namespace std;

class Point {
    private: 
        int x;
        int y;
    public:
        Point(){}
        Point(int _x, int _y) : x(_x), y(_y){}
        void print()
        {
            cout << x << ' ' << y << endl;
        }
        Point operator+(Point& b); // 声明成员函数 
        friend Point operator+(int a, Point& p);

};

// 类外面实现成员函数 
// 2.定义成类的成员函数 
Point Point::operator+(Point& b)
{
    cout << "成员函数重载+" << endl; 
    return Point(x + b.x, y + b.y); // this->x this->y
} 

/*
Point operator+(Point& a, Point& b) // 没有this指针 
{
    cout << "独立的函数重载+" << endl; 
    Point c(a.x + b.x, a.y + b.y);
    return c;
}
*/

Point operator+(int a, Point& p)
{
    return Point(a + p.x, a + p.y);
}

int main()
{
    Point p1(1, 2), p2(3, 4);
    Point p3 = 100 + p1; // int + Point  operator+(int, Point)
    p3.print();

    return 0;
}
15. 运算符重载-9-代码解读
#include <bits/stdc++.h>
using namespace std;

struct Student {
    int id;
    char name[20];
    double h;
};

ostream& operator<<(ostream& os, Student& s)
{
    os << s.id << ' ' << s.name << ' ' << s.h << endl;
    return os;
}

int main()
{
    Student s1 = {1, "xiaoming", 190.2};
    Student s2 = {2, "lier", 180.2};;
    cout << s1 << s2;

    return 0;
} 
本节课无课后练习

本节课答疑

建议大家有问题先通过AI答疑(比如:DeepSeek 等),AI时代需要学会使用AI辅助学习

陈远龙老师视频讲解:如何使用DeepSeek进行答疑?

通过AI未能获得满意解答的,可以联系陈远龙老师答疑

目录