如何通过指针变量输出对象的私有数据

定义了一个bike类,它有一个名为height的私有数据。假设我们在main()中编写,并且定义了一个指向bike对象的指针。指针变量称为bikeptr。
编写命令通过使用bikeptr变量输出对象的高度属性。

代码:

#include<iostream>
using namespace std;

class bike {
private:
    string height="124";
public:
    void show() {
        cout << height << endl;
    }
};

int main() {

    bike* bikeptr = new bike();

    bikeptr->show();
    return 0;
}
```c++
效果:
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/911658412656123.png "#left")