C++ 知识点08 类与对象

C++ 类与对象

一、类和对象核心概念

  • 类(class) :是模板 / 图纸,描述一类事物的属性和行为

  • 对象 :是类实例化出来的实体,根据图纸造出来的具体东西

  • 属性 :类里的成员变量

  • 行为 :类里的成员函数

1. 最简单的类 + 对象 创建

cpp 复制代码
#include <iostream>
using namespace std;
​
// 定义一个类
class Person
{
public:
    // 成员变量(属性)
    string name;
    int age;
​
    // 成员函数(行为)
    void showInfo()
    {
        cout << "姓名:" << name << ",年龄:" << age << endl;
    }
};
​
int main()
{
    // 实例化对象
    Person p1;
    p1.name = "张三";
    p1.age = 18;
​
    // 调用成员函数
    p1.showInfo();
​
    return 0;
}

运行结果:

cpp 复制代码
姓名:张三,年龄:18

二、访问权限修饰符(三大权限)

C++ 类里有 3 种权限:

  1. public:公有,类内、类外、派生类都能访问

  2. private :私有,只能类内部访问,外面不能碰

  3. protected:保护,类内、派生类能访问,外部不能

代码演示权限区别

cpp 复制代码
#include <iostream>
using namespace std;
​
class Student
{
// 公有
public:
    string name;
    void setAge(int a)
    {
        // 类内部可以访问私有成员
        age = a;
    }
​
// 私有
private:
    int age;
};
​
int main()
{
    Student s;
    s.name = "李四";   // public 可以访问
    // s.age = 20;     // 报错!age 是 private,外部不能直接访问
​
    s.setAge(20);      // 通过公有函数间接设置私有变量
    return 0;
}

规范写法

成员变量尽量设为 private,用 public 函数赋值、取值,封装安全。


三、构造函数 & 析构函数

1. 构造函数

  • 名字和类名一模一样

  • 没有返回值

  • 创建对象时自动调用

  • 作用:初始化成员变量

无参构造、有参构造 代码

cpp 复制代码
#include <iostream>
using namespace std;
​
class Car
{
public:
    string brand;
​
    // 1. 无参构造函数
    Car()
    {
        cout << "无参构造函数调用" << endl;
        brand = "未知品牌";
    }
​
    // 2. 有参构造函数
    Car(string b)
    {
        cout << "有参构造函数调用" << endl;
        brand = b;
    }
​
    void show()
    {
        cout << "汽车品牌:" << brand << endl;
    }
};
​
int main()
{
    Car c1;          // 调用无参构造
    c1.show();
​
    Car c2("奔驰");  // 调用有参构造
    c2.show();
​
    return 0;
}

2. 析构函数

  • 名字:~类名()

  • 无返回值、无参数

  • 对象销毁时自动调用

  • 作用:释放内存、收尾工作

构造 + 析构 完整示例

cpp 复制代码
#include <iostream>
using namespace std;
​
class Test
{
public:
    // 构造
    Test()
    {
        cout << "构造函数执行" << endl;
    }
​
    // 析构
    ~Test()
    {
        cout << "析构函数执行" << endl;
    }
};
​
int main()
{
    Test t;  // 进作用域构造,出作用域析构
    cout << "主函数中..." << endl;
    return 0;
}

四、封装(C++ 三大特性之一)

封装:把属性私有化,提供公开接口访问 / 修改。

优点:安全、可控、易维护。

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;
​
class People
{
private:
    // 私有成员,外部不能直接改
    string name;
    int age;
​
public:
    // 设置姓名
    void setName(string n)
    {
        name = n;
    }
    // 获取姓名
    string getName()
    {
        return name;
    }
​
    // 设置年龄(可以加判断)
    void setAge(int a)
    {
        if (a > 0 && a < 150)
            age = a;
        else
            age = 18;
    }
​
    int getAge()
    {
        return age;
    }
};
​
int main()
{
    People p;
    p.setName("王五");
    p.setAge(200);  // 非法值,自动修正为18
​
    cout << p.getName() << " " << p.getAge() << endl;
    return 0;
}

五、对象数组

批量创建多个同类对象

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;
​
class Student
{
public:
    string name;
    int id;
};
​
int main()
{
    // 对象数组
    Student stu[3];
​
    stu[0].name = "学生1";
    stu[0].id = 101;
​
    stu[1].name = "学生2";
    stu[1].id = 102;
​
    // 遍历输出
    for(int i = 0; i < 2; i++)
    {
        cout << stu[i].name << " " << stu[i].id << endl;
    }
    return 0;
}

六、this 指针

this 指针 :指向当前对象本身

用途:

  1. 区分成员变量和局部变量重名

  2. 返回当前对象

代码示例:

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;
​
class Person
{
private:
    string name;
public:
    void setName(string name)
    {
        // this->name 是成员变量
        // 右边 name 是形参
        this->name = name;
    }
​
    void show()
    {
        cout << "姓名:" << this->name << endl;
    }
};
​
int main()
{
    Person p;
    p.setName("赵六");
    p.show();
    return 0;
}

七、常对象 & 常成员函数

  • 常对象const 类名 对象名,不能修改成员变量

  • 常成员函数 :函数后面加 const不能修改成员变量,只能读取

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

class Person
{
private:
    string name;
public:
    void setName(string n)
    {
        name = n;
    }

    // 常成员函数:不修改任何成员变量
    void show() const
    {
        cout << "姓名:" << name << endl;
    }
};

int main()
{
    // 常对象
    const Person p;
    p.show();   // 常对象只能调用常成员函数
    return 0;
}

八、必背总结(考试 / 面试)

  1. 类是模板,对象是实例;

  2. 权限:public公开、private私有、protected保护;

  3. 构造函数:同类名、无返回、创建对象自动调用;

  4. 析构函数:~类名、无参无返回、对象销毁自动调用;

  5. 封装:变量私有,提供公有接口读写;

  6. this 指针代表当前对象;

  7. 常函数后面加 const,不可修改成员。

相关推荐
米粒12 小时前
力扣算法刷题 Day 63 Bellman_ford 算法
数据库·算法·leetcode
楼田莉子2 小时前
仿Muduo的高并发服务器:Http协议模块
linux·服务器·c++·后端·学习
你不是我我8 小时前
【Java 开发日记】HTTP3 性能更好,为什么内网微服务依然多用 HTTP2?HTTP2 内网优势是什么?
java·开发语言·微服务
IT大白鼠8 小时前
AIGC性能的关键瓶颈:算力、数据、算法三者如何互相制约?
算法·aigc
tjl521314_218 小时前
04C++ 名称空间(Namespace)
开发语言·c++
ximu_polaris8 小时前
设计模式(C++)-行为型模式-备忘录模式
c++·设计模式·备忘录模式
赏金术士9 小时前
Kotlin 数据流与单双向绑定
android·开发语言·kotlin
白雪茫茫9 小时前
监督学习、半监督学习、无监督学习算法详解
python·学习·算法·ai
FengyunSky9 小时前
浅析 空间频率响应 SFR 计算
算法