实例化

  1. #include <iostream>
  2. using namespace std
  3. class Box
  4. {
  5. public:
  6. double length;
  7. double breadth;
  8. double height;
  9. double getVolume(void);
  10. void setLength(double len);
  11. void setBreadth(double bre);
  12. void setHeight(double hei);
  13. };
  14. double Box::getVolume()
  15. {
  16. return length*breadth*height;
  17. }
  18. void Box::setLength(double len)
  19. {
  20. length=len;
  21. }
  22. void Box::setBreadth(double bre)
  23. {
  24. breadth=bre;
  25. }
  26. void Box::setHeight(double hei)
  27. {
  28. height=hei;
  29. }
  30. int main(){
  31. Box box1;
  32. box1.length = 5.0;
  33. Box* pBox2 = new Box();
  34. (*pBox2).length = 4.0;
  35. pBox2->height = 50.0;
  36. double length = pBox2->getVolume();
  37. delete pBox2;
  38. }

image.png

get、set 函数

通过private实现

image.png

构造函数

image.png

析构函数

image.png