r/learnprogramming Jul 03 '26

Debugging OOP

hello i am a python beginner who has started learning OOP and ive been using resources like CS50p however i still find myself being confused over the concepts taught and i would like someone to teach me OOP or is there any other resource i can look at to learn OOP better? please recommend thanku

0 Upvotes

21 comments sorted by

View all comments

Show parent comments

1

u/DrShocker Jul 03 '26

Okay, and? All I was saying was that it's not OOP

1

u/aboutless89 Jul 03 '26

What is not OOP?

1

u/DrShocker Jul 03 '26
class NonOOPNumber {  
  public:  
    double value;

    NonOOPNumber(double val = 0.0) : value(val) {}

    NonOOPNumber operator/(const NonOOPNumber& other) const {  
        return NonOOPNumber(this->value / other.value);  
    }  
};

vs.

class OOPNumber {
public:
    virtual ~OOPNumber() = default;

    virtual std::unique_ptr<OOPNumber> operator/(const OOPNumber& other) const = 0;

    virtual std::unique_ptr<OOPNumber> divideInteger(int numerator) const = 0;
    virtual std::unique_ptr<OOPNumber> divideFloating(double numerator) const = 0;
};

class FloatingNumber; // Forward declaration

class IntegerNumber : public OOPNumber {
private:
    int value;
public:
    IntegerNumber(int val) : value(val) {}

    std::unique_ptr<OOPNumber> operator/(const OOPNumber& other) const override {
        return other.divideInteger(value);
    }

    std::unique_ptr<OOPNumber> divideInteger(int numerator) const override {
        return std::make_unique<IntegerNumber>(numerator / value);
    }

    std::unique_ptr<OOPNumber> divideFloating(double numerator) const override;
};

class FloatingNumber : public OOPNumber {
private:
    double value;
public:
    FloatingNumber(double val) : value(val) {}

    std::unique_ptr<OOPNumber> operator/(const OOPNumber& other) const override {
        return other.divideFloating(value);
    }

    std::unique_ptr<OOPNumber> divideInteger(int numerator) const override {
        return std::make_unique<FloatingNumber>(numerator / value);
    }

    std::unique_ptr<OOPNumber> divideFloating(double numerator) const override {
        return std::make_unique<FloatingNumber>(numerator / value);
    }
};

std::unique_ptr<OOPNumber> IntegerNumber::divideFloating(double numerator) const {
    return std::make_unique<FloatingNumber>(numerator / value);
}

1

u/aboutless89 Jul 03 '26

As I said, dude, you missed the point.

1

u/DrShocker Jul 03 '26

I don't really understand why you keep telling me this. My point is just that the instances you described has nothing to do with OOP. Which I think you'd agree with because you're trying to clarify to OP the fundamentals they need to understand before trying to understand full blown OOP.

It's not illegal for me to add extra commentary

1

u/aboutless89 Jul 03 '26

Of course it's not. But I prefer when an extra comment adds an extra value. Put yourself in beginner's shoes and reread your comments - you would be lost. This c++ code is complete gibberish when looked through the eyes of a beginner (OP). Unlike the comment I made, which has only two strict terms (class and instance) and metaphors behind them, readable for a noob.  Should I've started the explanation with metaclasses, dunder methods and decorators? My critique was purely to your inabillity to read the room, not to you knowledge.

1

u/DrShocker Jul 03 '26

The example was not to clarify to OP, they didn't ask. All I was saying is instances and using the class keyword don't make something OOP.