Answer the question
In order to leave comments, you need to log in
Am I understanding OOP correctly?
All good. Let's say I need to create a program for accounting for livestock on a farm. Chickens, cows, bulls, horses, piglets. I understand correctly, I will have a parent class for everyone with the name Animal and with parameters (date of birth, gender, weight, date of death). From him there will be subordinate classes -
Cattle (cattle) and he will have parameters - how much milk he gives)
Chickens with parameters - how many eggs he gives
Horses - how much milk
Pigs give - I don’t even know.
In general, I'm confused, it seems that you can just create one class Animal with the parameters dr, sex, weight, date of death, and add the attributes of the amount of milk, eggs to it and not bother creating subclasses. In general, I’m confused (probably a farm shouldn’t use OOP?) There are also dogs, but we don’t seem to be milking them and they don’t give meat (
Answer the question
In order to leave comments, you need to log in
Based on the task that you wrote, namely (storing / receiving information), that is, you just need to store data, it’s enough to use collections for this, you can store animals in the list as objects, or you can just as elements. In the future, if you need to add methods to animals, then you can do it without any problems. Here you can stop at a simple hierarchy. Dogs extends Animal, Cat extends Animal, etc..
Any analogy has limits, do you want to oop? Write any bullshit in a structured style, and then start developing it. When you stop understanding what you write at all, and produce bugs faster than fixing, you will begin to understand the methods of managing complexity (including OOP), or you will not start ...
Horses - how much milk does it give
Basically I'm confused
there are also dogs, but we don’t seem to be milking them and they don’t give meat (
class Animal;
class Pig : public Animal;
class Horse : public Animal;
...
OOP is applicable in any task, and yes, in very simple tasks it can be redundant. OOP objects have little to do with the real world, this is a purely technical implementation, so you need to proceed from the technical requirements in the first place, and not from the biological classification of species.
It's not at all clear from the question what the requirements are. It is quite possible that you have enough accounting objects, each object is like a line in the accounting book. Depending on the usage, you may need the resource objects provided by the accounting objects. But it all depends on the requirements.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question