SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: C++ Oops

  1. #1
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    742
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question C++ Oops

    Hi

    I have been reading the concept of OOP in various tutorials, but I still do not understand the proper use of it. If its used to make the code more flexible while maintainance, so why this cannot be done with normal functions?

    I mean what extra feature does OOP give us that function does not?


    Thanx

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It doesn't give you any extra technical feature; all the code has to be compiled down to bits the OS/processor can interpret anyway, right?

    What object oriented design gives you is a straightforward way to think about modularizing your code into components that have clear responsibilities and interfaces. Easier to separate work into units for different developers to work on in a team, easier to wrap your head around some chunk of the system at a time, easier to identify where changes need to be made in the future.

  3. #3
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    742
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Why cannot normal functions play the same role as object oriented design plays?

  4. #4
    SitePoint Addict reboltutorial's Avatar
    Join Date
    Jan 2009
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cancer10 View Post
    Hi

    I have been reading the concept of OOP in various tutorials, but I still do not understand the proper use of it. If its used to make the code more flexible while maintainance, so why this cannot be done with normal functions?

    I mean what extra feature does OOP give us that function does not?


    Thanx
    It's about linear vs non-linear process. OOP allows more complex systems that are non-linears. So contrary to what I can read, it doesn't make thing simpler, it does make in fact make thing more complex but it is a necessary evil if you want the application to be manageable at several levels from requirements to architecture and even team organization. That is you see the advantages with big projects.

  5. #5
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OOP programming is a lot like a DVD player. You don't think about what happens inside the DVD player to produce a picture you see on a TV screen, you think of it in terms of DVD discs and the DVD player and the controls on the DVD player (play, stop, FF, record, etc). That is exactly what OOP programming is. You think of your programs in terms of the data (the objects) and what the data does (like the DVD player controls). OOP is an interface that provides controls to do stuff with your data. So you might have:

    disc = DVD->new
    disc->play
    disc->skip
    disc->stop

  6. #6
    SitePoint Addict reboltutorial's Avatar
    Join Date
    Jan 2009
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cancer10 View Post
    Why cannot normal functions play the same role as object oriented design plays?
    In prototype based language functions does play the same role : there are no classes (object templates) but only instances (concrete objects) that are created when calling a function of the same name as the object. Javascript is the most well-known of that kind of language. Rebol, which is an amazing language but barely known today, is also a prototype based language.

    The best would be to have both choices, unfortunately I don't know any language that offers it (but I'm thinking about how I could create a framework in rebol, my favorite language, to be able to simulate both).

    Nevertheless object oriented design is not all about classes or function prototypes, it's about software architecture and for example the lack of namespace concept is very annoying in the prototype based languages but it is more due to the fact that they were younger than to the lack of concepts. The proof is PHP (though not a prototyped base language) and Rebol in the next release have just or are going to implement the namespace concept.

  7. #7
    SitePoint Member
    Join Date
    May 2003
    Location
    Gainesville, Florida
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The simplest way to explain Oop, especially in C++, is that Objects are just datatypes in themselves.

    They are a natural evolution to Structures that allow private declarations and functions within the object.

    Thus, you will need classes when you need something more useful than a structure. You might find the struct to be sufficient for small applications.

    However, when you want to have an interface to the data being aggregated inside a structure you might find that the ability to have a function, aka method, inside the class, to be more efficient.

    Why have code outside the class to work on the struct when you could have functions INSIDE the struct that will work on that instance of the object?

  8. #8
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    742
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    But my question is still unanswered.

    Why cannot functions replace the work objects do?

  9. #9
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cancer10 View Post
    But my question is still unanswered.

    Why cannot functions replace the work objects do?
    Writing out the machine code in binary could replace the work functions do. That's what your compiler is doing. It's all equivalent in the end, there's nothing one coding style can do that others can't. You're asking the wrong question.

    The right question to ask is why you might choose to code in an object-oriented fashion instead of a procedural fashion when they both accomplish the same ends. The answers are many, from simplifying design specification, to breaking up work modules for parallel development, to shortening the time it takes to bring someone else up to speed on a system.

  10. #10
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    742
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    So these cannot be achieved with functions?

  11. #11
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    For sufficiently complex systems, you cannot achieve the same benefits with only functions. If you have an IEEE or ACM membership, you can find a number of published studies that empirically show the benefits of OOP over procedural programming when both were used to develop the same systems.

  12. #12
    SitePoint Member
    Join Date
    May 2003
    Location
    Gainesville, Florida
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cancer10 View Post
    So these cannot be achieved with functions?
    You are asking a convoluted question.

    I think you need to ask yourself 'where should my functions go' instead of why you should use functions over classes.

    A class is nothing more than an aggregate of datum to allow you to work on records more complex than standard Structures. Moreover, Classes also allow functions to be used inside the aggregate to make your code cleaner.

    It is not whether you can do something in a function vs doing something in a class. It is whether the context of your application would be better suited to doing something with simple functions vs doing them in a class.

  13. #13
    SitePoint Addict reboltutorial's Avatar
    Join Date
    Jan 2009
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cancer10 View Post
    But my question is still unanswered.

    Why cannot functions replace the work objects do?
    It is not that you cannot do it with functions, it is rather can you MANAGE it when it become very big spending the same energy / time or entropy ?

    So it comes down to economics.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •