Thread: Explain Object-Oriented Programming Like I'm 5

  1. #1
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352

    Explain Object-Oriented Programming Like I'm 5

    I'm an experienced C programmer, but I don't know that much about C++ and I'd like to know why it's better, worse, or just as good.

    I'm probably just ignorant, but to me, I don't see that many benefits with classes other than for organization purposes. I know they can keep variables and functions (or "methods") private, but why would you need private variables and functions? If those variables and functions aren't to be called or accessed, then don't call or access them.

    I can see why they would be useful if you're writing a library and there's certain functions you don't want your users to mess with, but why would you use them in your own programs? Aren't structures just as good for organizing your data? You can make copies of them with typedef.

    I'm probably being really stupid, because like I said I don't know that much about OOP, these observations are just from what I know of it.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    In terms of performance, C++ done right should be just as fast as C. That aside, I'm starting to learn it myself because it offers some of the things that have been spoiling me in Python but with the speed of one of the fastest compiled languages. For example:

    Code:
    #include <iostream>
    #include <unordered_map>
    #include <string>
    
    int main(void)
    {
         std::unordered_map<std::string, int> adictionary;
         adictionary["some key"] = 3;
         adictionary["another key"] = 6;
         std::cout << adictionary["some key"];
         return 0;
    }
    I'm not really interested in the nitty gritty of the language and its myriad of complicated features, I'm more interested in its large standard library as recently upgraded by C++11. The standard library includes all kinds of data structures out of the box which makes it ideal IMO.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Even in your own code it is easy to do things you shouldn't. Programmers make mistakes that, in hindsight, are often unnecessary mistakes. You may write the documentation incorrectly. You may forget about some special but important case that should be avoided. Even in your own code.

    In fact, often especially in your own code. You are more likely to be over-confident that code you wrote works correctly, and that you understand it.

    Access control (private, public, etc) is about allowing you to control what code is allowed to do a change, and what is not.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Babkockdood View Post
    I'm an experienced C programmer, but I don't know that much about C++ and I'd like to know why it's better, worse, or just as good.
    C++ and OOP in general are the fine art of taking a device that needs finely grained step by step instructions and getting it to act like it works on blobs of data as unified objects. Underneath even the most wildly abstracted OOP code, you are still going to find ASM like procedures running the show.

    Laughably this is touted as an attempt to make programming more "human"... I don't know a single person, programmer or otherwise, who thinks like a C++ data object... I do however know several people who think just like old style BASIC programming, complete with GOTO 10!

    The only way I can get my head around it is by imagining classes, interfaces and objects to be little machines where pulling one lever does one thing, another lever does something different...

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Babkockdood
    I can see why they would be useful if you're writing a library and there's certain functions you don't want your users to mess with, but why would you use them in your own programs?
    I take it that you recognise that the notion of implementation details being private in some way to the implementation maintainer is useful - this would be the notion of encapsulation. Another thing that you may recognise is that it may be useful to refer to an interface consisting of a set of functions that operate on some kind of data structure as a single entity - this would be the notion of an object or a class.

    Quote Originally Posted by Babkockdood
    I don't see that many benefits with classes other than for organization purposes. (...) Aren't structures just as good for organizing your data?
    If we just compare C++ concrete classes/structs with C structs, then yeah: the main difference would be member functions and access control. Member functions can be considered syntactic sugar since objects of a C struct type can also be seen as an object in an OO sense; a function that operates on the C struct and which forms the core interface provided by the library author can be seen as a member. An access control mechanism as a language feature is nice but unnecessary as convention can also work to achieve encapsulation... or we could use the opaque pointer idiom if necessary.

    I think that it is more interesting to consider the use of inheritance and polymorphism thereof in C++. Start by reading about the Open-Closed Principle and the Liskov Substitution Principle.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Babkockdood View Post
    I'm an experienced C programmer, but I don't know that much about C++ and I'd like to know why it's better, worse, or just as good.
    One way in which it's better: the convenience of the STL containers.
    One way in which it's worse: it is a resource pig compared to C, at least if you make much use of, eg, the STL. The significance of this depends on the nature of the task, I guess.

    I find a lot of the syntax awkward, but I'm now learning Java -- also a strongly typed OO language w/ generics -- and it seem to have some of the same complications.

    I'm probably just ignorant, but to me, I don't see that many benefits with classes other than for organization purposes.
    I think that is true, but it is also true of functions, and has almost as significant ramifications. The difference between "procedural" and "object oriented" is slightly ambiguous in reality; if you think of it on a micro level, class methods are procedural code. If you think of it more meaningfully, on a macro level (the way everything as a whole is organized), you may easily find that they way you already write code could be described as "object oriented"; it is intuitive and logical anyway. I'd actually say most of the C projects I've ever looked at are essentially object oriented in at least a protozoic way, very often explicitly. Certainly, it is easy to think of them that way. OO makes natural, modular sense.

    So the formal OO system in C++ provides some additional "power features", such as tidy inheritance, that are pretty useful for organizational purposes, and as such, can save you a significant amount of work (just like functions) and help "de-complexify" to a large extent. I find there is a sort of "complexity barrier" with coding; OOP simplifies IMO, making it easier to keep that barrier at a distance.

    I know they can keep variables and functions (or "methods") private, but why would you need private variables and functions? If those variables and functions aren't to be called or accessed, then don't call or access them.
    That's how I feel too, but then, I've never (yet) done significant work as part of a large team. In which case I might be very grateful to be able to disarm my co-workers in this way.

    Aren't structures just as good for organizing your data? You can make copies of them with typedef.
    Yeah, that's the kind of "primitive" or casual OOP I was talking about. C++ classes evolved from C structs.

    I'm probably being really stupid, because like I said I don't know that much about OOP, these observations are just from what I know of it.
    There is more OO code and less OO code. I think you start out writing more procedurally, making use of classes where you see it as obvious and convenient. Gradually, as the potential relationships between classes unfold, you start to see more and more stuff in that light. Again, exactly like functions. In general, the more you can break a task down into functions, the better. I suppose there could be such a thing as code which is too object oriented, just as there can be code which is broken down too much into functions, but I would place that bar very low, like near the level of obviously joking around. Some people are unintentionally silly, of course, but I like to think I'm not one of them.

    In short, I'd say that object oriented programming is not contra procedural programming at all. It just adds a sort of meta perspective to it, opening up and structuring different possibilities.
    Last edited by MK27; 01-08-2012 at 09:16 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by CommonTater View Post
    C++ and OOP in general are the fine art of taking a device that needs finely grained step by step instructions and getting it to act like it works on blobs of data as unified objects.

    Laughably this is touted as an attempt to make programming more "human"... I don't know a single person, programmer or otherwise, who thinks like a C++ data object...
    This is a very perverse, polemical mis-interpretation, unfortunately. Like I said, it is a mistake to understand OOP as something that is alien to or incompatible with "procedural" thinking.

    If you are trying to discredit something, a straw dog is useful. But if you are actually trying to understand it, don't chase the straw dog. It's not what you are looking for.

    Quote Originally Posted by CommonTater
    The only way I can get my head around it is by imagining classes, interfaces and objects to be little machines where pulling one lever does one thing, another lever does something different...
    A little better, if only because it is so general. :P
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by CommonTater View Post
    The only way I can get my head around it is by imagining classes, interfaces and objects to be little machines where pulling one lever does one thing, another lever does something different...
    That's not so bad. If you imagine classes as a blueprint for the little machine, describing all the levers, and then objects are the little machines, that's about right. Classes aren't objects in the same way that struct declarations aren't variables. I didn't have the convenience of such nice imagery to learn from, mainly in classes and books, we just dived right in and learned all the OOP terms and learned how to write classes by actually writing code.

    You're very self deprecating when it comes to this stuff CommonTater, but you "get it" better than you think.

  9. #9
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Learning some more C++ today...I think I may have to off myself for the spoils provided by std::string alone...

    (as in, I'm retarded for not having learned C++ sooner)

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Epy View Post
    (as in, I'm retarded for not having learned C++ sooner)
    Nah, your real mistake was going with the trendier python instead of the more expressive perl.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Thanks for all the explanations. I'm starting to understand it better.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  12. #12
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Quote Originally Posted by MK27 View Post
    Nah, your real mistake was going with the trendier python instead of the more expressive perl.
    Perl's syntax just makes me puke blood...and I only use Python at work to make quick and dirty reports really, I prefer C or Fortran otherwise.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Epy View Post
    Perl's syntax just makes me puke blood...
    That's what it's for. You need to get that crap out of your system and purlify. Embrace concise dynamic beauty kind of thing. Eventually it feels good ;$
    Last edited by MK27; 01-08-2012 at 04:57 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by MK27 View Post
    That's what it's for. You need to get that crap out of your system and purlify. Embrace concise dynamic beauty kind of thing. Eventually it feels good ;$
    I don't particularly like Perl's syntax, either. I learned Python because it's syntax _is_ nice. I am aware of Perl being more "hardcore" than Python, however, if I ever wanted a more "expressive" industrial scripting language, I would learn Ruby.

    Funny thing is, I would probably have already learned Perl anyway if not for it's use of sigils.
    Sigilia are evil.

  15. #15
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    I must only be scratching the surface of programming, because I would say that Python is about as expressive as it gets. Between that and the huge standard library, I don't really see the need to learn Perl or Ruby, since I already know Python...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [help] Object-Oriented Programming
    By null321 in forum C++ Programming
    Replies: 4
    Last Post: 08-02-2009, 12:29 PM
  2. object oriented programming
    By l2u in forum C++ Programming
    Replies: 10
    Last Post: 11-06-2006, 10:18 AM
  3. Object Oriented Programming
    By eric123 in forum C++ Programming
    Replies: 6
    Last Post: 11-30-2005, 04:56 AM
  4. Replies: 4
    Last Post: 11-19-2002, 09:18 PM
  5. C++ and Object Oriented Programming
    By Visual Develope in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2002, 05:27 AM