修改密码

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

成品课程

陈远龙老师主讲 & 答疑

课程题单 - T1000

未购买 · 可先试学5节课

课程目录展开/折叠

第71课 面向对象和类

播放快捷键

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

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

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

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

本节课讲解到的源代码

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

1. Circle类-1
#include <bits/stdc++.h>
using namespace std;

// const double PI = 3.1415;
class Circle {
    private:
        double r;
        const double PI;
    public:
        /*
        Cirlce()
        {}
        */

        Circle(double _r) : r(_r), PI(3.1415)
        {
            // PI = 3.14;
        }

        double getArea()
        {
            return PI * r * r;
        }
};

/*
void getArea(double r)
{
    cout << PI * r * r;
}
*/

int main()
{
    Circle c1(5);
    cout << c1.getArea() << endl;

    return 0;
} 
2. Circle类-2
#include <bits/stdc++.h>
using namespace std;

// const double PI = 3.1415;
class Circle {
    private:
        double r;
        const double PI;
    public:    
        Circle(double _r) : r(_r), PI(3.1415)
        {
            // PI = 3.14;
        }

        double getArea(); // 声明下成员函数原型 
};

// Circle::double getArea()
// 成员函数的具体实现 
double Circle::getArea()
{
    return PI * r * r;
}

int main()
{
    Circle c1(5);
    cout << c1.getArea() << endl;

    return 0;
} 
3. Point类-1
#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 setX(int _x)
        {
            x = _x;
        }

        int getX()
        {
            return x;
        }

        void setY(int _y)
        {
            y = _y;
        }
        int getY()
        {
            return y;
        }

        void print()
        {
            cout << x << ' ' << y << endl;
        }

};

int main()
{
    Point p1(1, 2);
    p1.print();

    Point p2; // 调用默认构造函数 

    Point p3; // 想用p1初始化p3 
    p3.setX(p1.getX());
    p3.setY(p1.getY());
    p3.print();

    return 0;
} 
4. Point类-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;
        }

};

int main()
{
    Point p1(1, 2);
    p1.print();

    // Point p3 = p1; // 想用p1初始化p3 调用的是拷贝构造函数 
    Point p3(p1); // 与 Point p3 = p1;一样,调用默认的拷贝构造函数 
    p3.print();

    return 0;
} 
5. Point类-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;
        }

        Point(const Point& p) // 自己实现的拷贝构造函数 
        {
            cout << "调用拷贝构造函数" << endl; 
            x = p.x;
            y = p.y;
        }

        void print()
        {
            cout << x << ' ' << y << endl;
        }

};

// 值传递 
void print(Point p) // 会调用拷贝构造函数 Point &p 不会调用 
{
    p.print();
}

Point getPoint() // 值返回 值传递 
{
    Point p(5, 6);
    return p;
}

int main()
{
    /*
    Point p1(1, 2);
    p1.print();

    Point p3 = p1; // 想用p1初始化p3 调用的是拷贝构造函数 
    // Point p3(p1); // 与 Point p3 = p1;一样,调用默认的拷贝构造函数 
    p3.print();

    Point p4;
    p4 = p1;
    */
    Point p5(1, 2);
    print(p5);

    Point p6 = getPoint();
    p6.print(); 

    return 0;
} 
6. 对象指针-1
#include <bits/stdc++.h>
using namespace std;

class Student {
    private:
        int id;
        char name[30]; // string name;
        double h;
    public:
        string addr;

        Student()
        {}

        Student(int _id, const char _name[], double _h)
        {
            id = _id;
            strcpy(name, _name);
            h = _h;
        }

        void print()
        {
            cout << id << ' ' << name << ' ' << h << endl;
        }
};

int main()
{
    /*
    Student s1(1, "xiaoming", 189.5); // s1是对象 栈内存中 
    s1.print();
    s1.addr = "Nanjing";
    cout << &s1 << endl;

    Student *ps1 = NULL; // ps1对象指针,指向Student对象的指针 
    ps1 = &s1;
    ps1->print();
    cout << ps1->addr;
    */

    /*
    Student *ps2 = new Student(2, "lier", 190.2); 
    ps2->print();
    ps2->addr = "Shanghai";
    cout << ps2->addr << endl;

    (*ps2).print();
    cout << (*ps2).addr << endl;

    delete ps2;

    */
    // Student *ps3 = new Student; 
    Student *ps3 = new Student(); 
    ps3->print();

    return 0;
} 
7. this指针-1
#include <bits/stdc++.h>
using namespace std;

class Point {
    private:
        int x;
        int y;
    public:
        void setX(int x)
        {
            x = x;
        }
        void setY(int y)
        {
            y = y;
        }
        void print()
        {
            cout << x << ' ' << y << endl;
        }
};

int main()
{
    Point p1;
    p1.setX(1);
    p1.setY(2);
    p1.print();

    return 0;
} 
8. this指针-2
#include <bits/stdc++.h>
using namespace std;

class Point {
    private:
        int x;
        int y;
    public:
        void setX(int x, Point *p)
        {
            p->x = x;
        }
        void setY(int y, Point *p)
        {
            p->y = y;
        }
        void print()
        {
            cout << x << ' ' << y << endl;
        }
};

int main()
{
    Point p1;
    p1.setX(1, &p1);
    p1.setY(2, &p1);
    p1.print();

    return 0;
} 
9. this指针-3
#include <bits/stdc++.h>
using namespace std;

class Point {
    private:
        int x;
        int y;
    public:
        void setX(int x) // void setX(int x, Point *this)
        {
            cout << "this:" << this << endl;
            this->x = x;
        }
        void setY(int y)
        {
            this->y = y;
        }
        void print()
        {
            cout << this->x << ' ' << this->y << endl;
            // cout << x << ' ' << y << endl;
        }
};

int main()
{
    Point p1;
    cout << "main, &p1:" << &p1 << endl; 
    p1.setX(1); // p1.setX(1, &p1)
    p1.setY(2);
    p1.print();

    return 0;
} 
10. this指针-4
#include <bits/stdc++.h>
using namespace std;

class Point {
    private:
        int x;
        int y;
    public:
        void setX(int x) // void setX(int x, Point *this)
        {
            // this->x = x;
            (*this).x = x;
        }
        void setY(int y)
        {
            // this->y = y;
            (*this).y = y;
        }
        void print()
        {
            cout << this->x << ' ' << this->y << endl;
            // cout << x << ' ' << y << endl;
        }
};

int main()
{
    Point p1;
    cout << "main, &p1:" << &p1 << endl; 
    p1.setX(1); // p1.setX(1, &p1)
    p1.setY(2);
    p1.print();

    return 0;
} 
11. this指针-5
#include <bits/stdc++.h>
using namespace std;

class Point {
    private:
        int x;
        int y;
    public:
        Point& setX(int x) // void setX(int x, Point *this)
        {
            this->x = x;
            return *this;
        }
        Point& setY(int y)
        {
            this->y = y;
            return *this;
        }

        Point* setX2(int x)
        {
            this->x = x;
            return this;
        }

        Point* setY2(int y)
        {
            this->y = y;
            return this;
        }

        void print()
        {
            cout << this->x << ' ' << this->y << endl;
        }
};

int main()
{
    Point p1;
    p1.setX(1).setY(2).print(); // 链式的调用 
    p1.print();

    Point p2;
    p2.setX2(1)->setY2(2)->print();
    p2.print();

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

本节课答疑

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

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

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

目录