Const and Static

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 พ.ย. 2024

ความคิดเห็น • 1

  • @CamCoders
    @CamCoders  9 หลายเดือนก่อน

    // const - keyword to let compiler know that something imutable
    // static - depends on the context;
    // static variables retain their value between function calls
    // static functions belong to a class, and can be called without an instance/object.
    #include
    using namespace std;
    class Triangle
    {
    private:
    double base;
    double height;
    public:
    Triangle()
    {
    base = 1;
    height = 1;
    }
    Triangle(double b, double h)
    {
    base = b;
    height = h;
    }
    double getBase() const {
    return base;
    }
    double getHeight() const {
    return height;
    }
    double getArea() {
    double area = (base * height) / 2;
    return area;
    }
    static double getArea(double b, double h) {
    double area = (b * h) / 2;
    return area;
    }
    };
    int main()
    {
    cout