zoukankan      html  css  js  c++  java
  • 类指针

    一个指向 C++ 类的指针与指向结构的指针类似,访问指向类的指针的成员,需要使用成员访问运算符 ->,就像访问指向结构的指针一样。与所有的指针一样,您必须在使用指针之前,对指针进行初始化。

    下面的实例有助于更好地理解指向类的指针的概念:

    #include <iostream>

    using namespace std;

    class Box
    {
       public:
          // 构造函数定义
          Box(double l=2.0, double b=2.0, double h=2.0)
          {
             cout <<"Constructor called." << endl;
             length = l;
             breadth = b;
             height = h;
          }
          double Volume()
          {
             return length * breadth * height;
          }
       private:
          double length;     // Length of a box
          double breadth;    // Breadth of a box
          double height;     // Height of a box
    };

    int main(void)
    {
       Box Box1(3.3, 1.2, 1.5);    // Declare box1
       Box Box2(8.5, 6.0, 2.0);    // Declare box2
       Box *ptrBox;                // Declare pointer to a class.

       // 保存第一个对象的地址
       ptrBox = &Box1;

       // 现在尝试使用成员访问运算符来访问成员
       cout << "Volume of Box1: " << ptrBox->Volume() << endl;

       // 保存第二个对象的地址
       ptrBox = &Box2;

       // 现在尝试使用成员访问运算符来访问成员
       cout << "Volume of Box2: " << ptrBox->Volume() << endl;
     
       return 0;
    }

  • 相关阅读:
    作业要求 20200924-5 四则运算试题生成,结对
    作业要求 20200924-1 每周例行报告
    作业要求20200924-3 单元测试,结对
    作业要求20200924-4 代码规范,结对要求
    20200910-1每周例行报告
    20200910-博客作业
    通读《构建之法》
    20200910-3 命令行和控制台编程
    20200924-2 功能测试
    20200924-5 四则运算试题生成,结对
  • 原文地址:https://www.cnblogs.com/nku-wangfeng/p/7726115.html
Copyright © 2011-2022 走看看