• 三、类和对象


    文章目录

    一、类和对象

    在C++中,类是对象的抽象,对象是类的实例化,类是不存在现实中的,对象是存在现实中的。例如,我们都是人类,人类是一个类,在现实中并不存在人类这个东西,而只存在人这个人类实例化的东西,所以,认识一个对象。所谓类,就是对一群具有相似属性的对象的抽象。

    • 格式:
      class Name
      {
          public:
              ...;
          private:
              ...;
          protectde:
              ...;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    • public: 可以被类以外成员访问
    • private: 只可以被类和友元函数访问,要想修改相应的参数或操作,只能通过类中public函数和友元函数,类中成员默认是private
    • protected: 可以被派生类访问
    • 代码实现:
      #include 
      
      using namespace std;
      
      class Box
      {
          // color默认是私有的
          char color;
          // public:共有成员,类的外部都是可以访问的
      public:
          double length;
          double breadth;
          double height;
          // 定义类成员函数
          // 成员函数可以操作类的任意对象
          double get(void);
          void set(double len, double bre, double hei);
          void set_name(char s);
          char get_name(void);
          // private:私有成员,除了类和友元函数,类的外部是不可以访问的,派生类也不可以访问
          // 在类中,成员默认是私有的
          // 要想更改私有成员,只能使用类中的函数或友元函数
      private:
          char name;
      
      protected:
          double weight;
      };
      // 类的派生,派生类可以访问父类中的protected类型
      class SmallBox : Box
      {
      public:
          void set_weight(double wei);
          double get_weight(void);
      };
      
      void SmallBox::set_weight(double wei)
      {
          weight = wei;
      }
      
      double SmallBox::get_weight(void)
      {
          return weight;
      }
      // 使用范围解析符定义函数,注意一定要指定类名
      double Box::get(void)
      {
          return length * breadth * height;
      }
      
      void Box::set(double len, double bre, double hei)
      {
          length = len;
          breadth = bre;
          height = hei;
      }
      
      void Box::set_name(char s)
      {
          name = s;
      }
      
      char Box::get_name(void)
      {
          return name;
      }
      int main()
      {
          double volume[2];
          Box box1;
          Box box2;
          box1.height = 10;
          box1.breadth = 10;
          box1.length = 10;
          volume[0] = box1.get();
          box2.set(100, 100, 100);
          volume[1] = box2.get();
          cout << "volume of box1:" << volume[0] << endl;
          cout.precision(3);
          // 以小数点输出:fixed
          cout << fixed << "volume of box2:" << volume[1] << endl;
          box1.set_name('a');
          cout << "name of box1:" << box1.get_name() << endl;
      
          SmallBox box3;
          box3.set_weight(20.0);
          cout << "weight of box3:" << box3.get_weight() << endl;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
      • 58
      • 59
      • 60
      • 61
      • 62
      • 63
      • 64
      • 65
      • 66
      • 67
      • 68
      • 69
      • 70
      • 71
      • 72
      • 73
      • 74
      • 75
      • 76
      • 77
      • 78
      • 79
      • 80
      • 81
      • 82
      • 83
      • 84
      • 85
      • 86
      • 87
      • 88
      • 89
    • 运行结果:
      [Running] cd "d:\code\package_01\src\" && g++ *.cpp -o test && "d:\code\package_01\src\"test
      volume of box1:1000
      volume of box2:1000000.000
      name of box1:a
      weight of box3:20.000
      
      • 1
      • 2
      • 3
      • 4
      • 5
  • 相关阅读:
    javaHTML5寿光农产品资源展示平台计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
    手把手教你在windows上安装mysql8.0最新版本数据库,保姆级教学
    Android 解析APK包
    【JavaEE基础与高级 第53章】Java中的IO流中的缓冲流详细介绍使用、字节缓冲流、字符缓冲流、案例使用与总结
    Ajax与jQuery
    java与es8实战之二:实战前的准备工作
    升级PIP
    MQ回退消息 springboot
    资本频频加码,急于上市的和府捞面有多“疯狂”?
    Linux CentOS 8(磁盘容量配额(Quota))
  • 原文地址:https://blog.csdn.net/qq_43280851/article/details/127838587