L
L
lutokris2021-03-13 03:49:37
OOP
lutokris, 2021-03-13 03:49:37

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

5 answer(s)
G
gazievDima, 2021-03-13
@lutokris

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..

M
mkone112, 2021-03-13
@mkone112

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 ...

S
Saboteur, 2021-03-13
@saboteur_kiev

Horses - how much milk does it give

Basically I'm confused

That's for sure.
You are now parsing data structures, and OOP is more about methods and encapsulation.
In order to keep records of animals, it is not at all necessary for them to do a class, store everything in the database, keep records with sql queries, for example.
PS
there are also dogs, but we don’t seem to be milking them and they don’t give meat (

What country are you from..

V
vanyamba-electronics, 2021-03-13
@vanyamba-electronics

class Animal;
class Pig : public Animal;
class Horse : public Animal;
...

V
Vitsliputsli, 2021-03-15
@Vitsliputsli

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 question

Ask a Question

731 491 924 answers to any question