c++——指向类的指针

以你之姓@ 2023-08-17 15:44 188阅读 0赞
  1. #include<iostream>
  2. using namespace std;
  3. class Box
  4. {
  5. public:
  6. Box(double l=2.0,double b=2.0,double h=2.0)
  7. {
  8. cout << "constructor called" << endl;
  9. length = l;
  10. breadth = b;
  11. height = h;
  12. }
  13. double Volume()
  14. {
  15. return length * breadth*height;
  16. }
  17. private:
  18. double length;
  19. double breadth;
  20. double height;
  21. };
  22. int main()
  23. {
  24. Box box1(2.0,1.0,2.0);
  25. Box box2(2.1, 1.2, 2.3);
  26. Box* ptrBox;
  27. ptrBox = &box1;
  28. cout << "Volume of box1:" << ptrBox->Volume() << endl;
  29. ptrBox = &box2;
  30. cout << "Volume of box2:" << ptrBox->Volume() << endl;
  31. return 0;
  32. }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,188人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C++之指向指针

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

    相关 指向成员/函数指针

    C++扩展了指针在类中的使用,使其可以指向类成员,这种行为是类层面的,而不是对象层面的。 指向类成员/函数的指针的本质并不是取地址.而是利用了对象地址的偏移量 我们创建了一