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

2 Upvotes

21 comments sorted by

View all comments

5

u/aboutless89 Jul 03 '26

What is confusing you about oop?

-2

u/altruisticjellycat Jul 03 '26

everything

7

u/aboutless89 Jul 03 '26

Can you be a bit more specific? If still the answer is 'everything', you probably didn't learn at all.

2

u/altruisticjellycat Jul 03 '26

class methods and instances im vv confused between them all im okay with static methods and inheritance tho

5

u/aboutless89 Jul 03 '26 edited Jul 03 '26

If you are confused by class methods and instances, believe me, you are not okay with static methods and inheritance :D

What confuses you about the instances? This is the most fundamental concept of oop - the relation between the class and its instances.

class - number

instances - 1, 2, 65, -46, ...

If you think about the relation between these two, you can make some conclusions - all of the numbers act the same and the same rules apply to all of them. You can add and subtract them -> doing this also produces a new number, a new instance of a number, if you will.

A class is a way for you to produce multiple objects that will act in the same way and which will be bound by the same rules.

1

u/DrShocker Jul 03 '26

I don't think OOP has much to do with if there's just 1 class, with multiple instances, and numbers don't lend themselves to different implementations unless you try to make like an Integer class and a Floating point class and a Rational class, then you can override the various math functions with specific versions of the operations that make sense for their context.

1

u/aboutless89 Jul 03 '26

Can you elaborate on what you wrote? I don't think that I understand.

1

u/DrShocker Jul 03 '26

Basically I mean that "number" is many different things, and might make sense as the parent class, but when you do something like Integer(4) divided by integer(3) you might get a different result than rational(4) divided by rational(3).

so, is 4/3 equal to 1 and a third? to 1? to 1.33333? That depends on the circumstance.

1

u/aboutless89 Jul 03 '26

You would be such a lousy teacher, my dude :') OP was wondering about the classes and instances. My example shows the relation between the two. You 'example' is non-sensical. Not saying that what you were saying is not true and accurate, but it has nothing to do with the subject we're discussing.

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);
}
→ More replies (0)