Like Tree15Likes

Explain Object-Oriented Programming Like I'm 5

This is a discussion on Explain Object-Oriented Programming Like I'm 5 within the General Discussions forums, part of the Community Boards category; I'm an experienced C programmer, but I don't know that much about C++ and I'd like to know why it's ...

  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
    Epy
    Epy is offline
    Hen-teaser Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    880
    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
    5,866
    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%.

  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
    20,975
    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.
    MK27 and stahta01 like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 08: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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,527
    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.
    MK27 and manasij7479 like this.

  9. #9
    Epy
    Epy is offline
    Hen-teaser Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    880
    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
    Epy
    Epy is offline
    Hen-teaser Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    880
    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 03:57 PM.
    Epy likes this.
    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
    1,534
    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.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  15. #15
    Epy
    Epy is offline
    Hen-teaser Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    880
    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...

Page 1 of 3 123 LastLast
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, 09:18 AM
  3. Object Oriented Programming
    By eric123 in forum C++ Programming
    Replies: 6
    Last Post: 11-30-2005, 03:56 AM
  4. Replies: 4
    Last Post: 11-19-2002, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21