面向对象基础-类与对象-封装

1、类与对象

1.1 概念

类:类是一个抽象的概念,用于描述一类对象的特点。

对象:根据类的概念所创造的实体。

【思考】一个对象可以没有对应的类嘛?

不可以,因为必须现有类才能创建对象。

1.2 类的内容

类中最基础的内容包括两部分,一个是属性、一个是行为。

  • 属性:表示一些特征项的数值,比如说:身高、体重、年龄、性别。型号、配色。这些特征项的数值被称为“成员变量”。属性一般以名词存在。
  • 行为:表示能执行的动作,能干什么事?比如说:睡觉、吃饭、打呼噜、打架、唱、跳、rap、打篮球。行为一般是通过 “成员函数” 实现。行为一般以动词存在。

成员 = 成员函数+成员变量。

【例子】以手机为例,来说明类的定义。

规定手机的行为:打游戏、爆炸、飞、打电话、播放音乐。

规定手机的属性:品牌、型号、重量、尺寸。

#include <iostream>

using namespace std;

// 帕斯卡命名法(大驼峰命名法)
// 每个单词首字母大写
class MobilePhone
{
      
public: // 权限:public是最开放的权限。
//成员变量
    string brand;   // 品牌
    string modle;   // 型号
    int weight;     // 重量
//成员函数
    void play_music()
    {
      
        cout << "只因你太美,哒哒哒" << endl;
    }

    void run_game()
    {
      
        cout << "原神启动" << endl;
    }

    void call()
    {
      
        cout << "上帝您好" << endl;
    }
};

int main()
{
      
    return 0;
}

1.3 对象的创建

C++中存在两种类型的对象;(栈内存对象、堆内存对象)

  • 栈内存对象 (类似 struct 结构体创建)

对象所在的{}执行完毕后,自动被销毁

#include <iostream>
using namespace std;
// 帕斯卡命名法(大驼峰命名法)
// 每个单词首字母大写
class MobilePhone
{
      
public: // 权限:public是最开放的权限。
    // 成员变量
    string brand;   // 品牌
    string modle;   // 型号
    int weight;     // 重量
    // 成员函数
    void play_music()
    {
      
        cout << "只因你太美,哒哒哒" << endl;
    }
    void run_game()
    {
      
        cout << "原神启动" << endl;
    }
    void call()
    {
      
        cout << "上帝您好" << endl;
    }
};
int main()
{
      
    MobilePhone mp;     // 栈内存对象
    mp.brand = "华为";
    mp.modle = "mate 60 xspro";
    mp.weight = 400;
    cout << mp.brand << " " <<
            mp.modle << " " <<
            mp.weight<< " " << endl;
    mp.play_music();
    mp.run_game();
    mp.call();
    return 0;
}

  • 堆内存对象 new 创建 (类似malloc)delete 销毁堆空间(类似free)

必须使用new关键字创建,使用指针保存(当前对象的首地址,调用构造函数)。

如果不使用delete关键字销毁,则内存对象会持续存在。导致内存泄漏。

堆内存对象调用成员时,使用->而不是"."(英文“.”自动生成“->”)

#include <iostream>
using namespace std;
// 手机类
// 帕斯卡命名法(大驼峰)
class MobilePhone
{
      
public:     // 权限:最开放的权限
    string brand;   // 品牌
    string modle;   // 型号
    int weight;     // 重量
    void play_music()
    {
      
        cout << "只因你太美,哒哒哒" << endl;
    }
    void run_game()
    {
      
        cout << "元神 启动" << endl;
    }
    void call()
    {
      
        cout << "上帝你好" << endl;
    }
};
int main()
{
      
    MobilePhone *mp = new MobilePhone; // 创建堆内存对象
    mp->brand = "华为";
    mp->modle = "mate 60 pro plus";
    mp->weight = 300;
    cout << mp->brand << " " << mp->modle << " " << mp->weight << endl;
    mp->play_music();
    mp->run_game();
    mp->call();
    delete mp; // 手动销毁
    mp = NULL;//建议置为空,防止后面调用
    return 0;
}

2、封装

在上一节中MobilPhone类与结构体差别不大,实际上可以认为结构体就是一种完全开放的类。

封装指的是,将类的一些属性和细节隐藏。重新提供对外访问的接口。封装可以提升代码的安全性。并且可以让程序员更关注上层架构而非内部逻辑。

private:私有权限,最封闭的权限,只能在类内访问
public:权限:public是最开放的权限。

#include <iostream>
using namespace std;
// 帕斯卡命名法(大驼峰命名法)
// 每个单词首字母大写
class MobilePhone
{
      
private: // 私有权限,最封闭的权限,只能在类内访问
    string brand;   // 品牌
    string modle;   // 型号
    int weight;     // 重量

public: // 权限:public是最开放的权限。
    // 品牌的get函数
    string get_brand()
    {
      
        return brand;
    }
    // 型号的get函数
    string get_modle()
    {
      
        return modle;
    }
    // 重量的get函数
    int get_weight()
    {
      
        return weight;
    }
    // 品牌的set函数
    void set_brand(string b)
    {
      
        brand = b;
    }
    //型号的set
    void set_modle(string m)
    {
      
        modle=m;
    }
    //重量的set
    void set_weight(int w)
    {
      
        weight=w;
    }
};
int main()
{
      
    // 栈内存对象
    MobilePhone mp1;
    mp1.set_brand("中兴");
    mp1.set_modle("utrl");
    mp1.set_weight(444);
    cout << mp1.get_brand() << " "<<
            mp1.get_modle() << " "<<
            mp1.get_weight() << endl;
    //堆内存对象
    MobilePhone *mp2=new MobilePhone;
    mp2->set_brand("华为");
    mp2->set_modle("mate60");
    mp2->set_weight(9000);
    cout << mp2->get_brand() << " "<<
            mp2->get_modle() << " "<<
            mp2->get_weight() << endl;
    return 0;
}


相关推荐

  1. 面向对象——对象

    2024-01-01 11:00:02       38 阅读
  2. 三、C#面向对象编程(封装抽象

    2024-01-01 11:00:02       60 阅读
  3. Python面向对象-对象

    2024-01-01 11:00:02       56 阅读
  4. PHP面向对象基础对象基本特点

    2024-01-01 11:00:02       53 阅读
  5. 对象——封装

    2024-01-01 11:00:02       50 阅读
  6. C++ 对象面向对象编程基础

    2024-01-01 11:00:02       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-01 11:00:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-01 11:00:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-01 11:00:02       82 阅读
  4. Python语言-面向对象

    2024-01-01 11:00:02       91 阅读

热门阅读

  1. 2023年终总结及计划

    2024-01-01 11:00:02       55 阅读
  2. Python高级数据类型

    2024-01-01 11:00:02       60 阅读
  3. 《Linux Nano命令详解:小而强大的文本编辑器》

    2024-01-01 11:00:02       60 阅读
  4. vue3 element plus el-table封装(二)

    2024-01-01 11:00:02       66 阅读
  5. Vue-Setup

    Vue-Setup

    2024-01-01 11:00:02      46 阅读
  6. 深入理解Vue.js 3的Reactive方法

    2024-01-01 11:00:02       61 阅读
  7. SpringBoot简单整合mybatis

    2024-01-01 11:00:02       47 阅读
  8. 基础算法--搜索与图论(1)

    2024-01-01 11:00:02       51 阅读
  9. OJ选夫婿

    2024-01-01 11:00:02       53 阅读