3
Bubbles
6y

Okay so theres something stopping me from understanding how Object Oriented Programming works. im sorry ahead of time this will get messy..

SO in this case we will use python. well what if the object has more than two functions? like the __init__, func1, then func2 and func2 does something else but doesn't get called or would you have to call of of them like class.func1(), class.func2().

I just don't understand when it comes to how the functions interact or effect each other. and how they would work when you dont call that specific function. I see the use of oop i just cant wrap my head around certain things..

Comments
  • 3
    What is actually your question?
  • 2
    You can call functions within other functions.

    An example of how one might use OOP is having:

    1 class: foo,

    2 attributes: an int, bar and a string, foe

    4 functions: the constructor, func1, func2 and func3

    (The constructor is called when an object for foo is made). On construction foo runs the constructor which takes the input of an int. The int is then squared with func1 and set to bar. The int is turned into string form (ie. 4 = four) with func2 and set to foe.

    The code can then run foo.func3() and func3 gives bar run through func1 (squared again) and then run through func2 (turned into a string).

    The code has the ability to:

    Create a foo object with foo(4)

    Get 4 squared (16) with foo.bar

    Get 4 stringed (four) with foo.foe

    Get 16 (4 squared) squared (256) with foo.func3()

    But what really can make OOP powerful us that you could do the same kind of thing with more object like foo(7) and foo(13). You can do more complicated and useful things and utilise the power of OOP.
  • 0
    But how does it go through them and how do I know if my functions will work together or not when called
  • 1
    @Bubbles what do you mean? They aren't called if you don't call them.

    The only function that will be called without you explicitly calling it (dependent on language) is the constructor but even then you call it by making a new one.

    I don't really get what you are asking.
  • 0
    @qwerty77asdf I'm sorry i dont know how to word it..

    When I think of functions I think of a small portion of the program that's seperated and only used when called. So when I was introduced to OOP I started to get confused because I dont know how any of them work if they arent called and how they interact with each other
  • 2
    @Bubbles I guess you could think of them like you did functions.

    They don't do anything if you don't use/call them either.
  • 1
    @Bubbles

    Here's a library I made the other week:

    https://github.com/Qwerty77asdf/...

    (Sorry I haven't gotten around to commenting it yet (too lazy(?)))

    Look through the code and you can see how one might use OOP.
  • 0
    @qwerty77asdf let's say I wanted to create a very simple client for a chat app. Some of the functions I would put in the class are,constructor (which from the way it was described sounds like the python __init__() function) sending message, receiving message. How would I go about calling the functions at the end or would I just call the class
  • 0
    Apologies if this is stupid I'm just trying to find a simple way to understand
  • 1
    @Bubbles if you were making a chat app you'd probably have a few different classes like:

    Main,

    User,

    Message

    Main would be used to call everything and host. In a language like C# this would be a class whereas in a language like Python or PHP you'd probably just have it as a normal script.

    User would have a few methods (functions) like login, logout, send message, receive message and a few attributes (variables) like (String) name and (Int) ID.

    Message would have two attributes, (String) contents, and (Int) ID.

    The user class' receive and send message would have the message class as a function parameter.

    The main class would use I/O to get some details from the user and then construct a new user object (the construction would call the login method); and then it would connect to the server and maybe receive some messages, the messages would be as Strings and made into Message objects and then given to the user with User.recieveMessage(Message). Perhaps then the application ...cont
  • 0
    @Bubbles cont... would then get I/O and know the user wants to send a message, the input would go into a new message object and then use the user's sendmessage class.

    Then perhaps the app is exited and gets an inbuilt function call; in the inbuilt function you'd probably call for User.Logout().

    Like I said give my link a look.
  • 1
    @Bubbles It's okay. I know it can be hard to get your head around some concepts sometimes.
  • 1
    The whole idea of OOP is that you have a very close coupling between some data types and functions that work on these data types. So close that it is worth to stuff them into a common container type (the class).

    For example, a stack has a data area, and you can push a new entry to the stack or pop the topmost entry. The push/pop functions are called when.. well, when you call them because you want to put something onto the stack or get something out of it.
  • 2
    Some good answers here. Functions don’t affect other functions but could affect the objects state and of course you can use there return types within functions to so if you called the car.start method it may set car.isEngineRunning = true
    You may then decide you want a motorbike so instead of giving that a start method as well you could have a base called vehicle and move the cars start method into that instead and make car and bike inherit from vehicle so they get the start method for free. Unfortunately this is an old bike with one of those manual fuel switches so it requires a slightly different start method so you override it in the bike object to call turnFuelOn and then the base.start method to actually start the vehicles engine. You then may want to add an abstract property to vehicle called wheels and that would force you to set the value in bike and car and trike and truck ect
  • 2
    This may sound dumb but the key to OOP is thinking of things as objects. Objects are collections or pieces of data first, but secondly they provide functions to work with that data (e.g. a string class might have a toLowerCase function).

    The methods/functions of a class are like a bridge between the outside caller and the inside (usually private) internals of the class. So as the author of a class it is your responsibility to make sure all the functions work together and maintain a consistent and expected state, but anyone using your class just deals with the public interface and assumes the internals are wired correctly.

    Breaking things up into objects helps divide and conquer a problem, breaking it into smaller isolated pieces that are easier to manage than the monolithic structures in procedural programming.
Add Comment