Skip to content

C++ 面向对象编程

类&对象

class Box {
    public:
        double length;
        double breadth;
        double height;
        double get(void);
        void set(double len, double bre, double hei);
};

double Box::get(void) {
    return length * breadth * height;
}
void Box::set(double len, double bre, double hei) {
    length = len; breadth = bre; height = hei;
}

访问修饰符:

  • public:外部可访问
  • private:外部不可访问,只有类和友元可访问
  • protected:外部不可访问,只有类和派生类可访问