CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jun 2013
    Posts
    3

    Understanding Object Oriented Programming

    I didn't know where to put this, but i understand Java and C# the best.

    I'm having a hard time getting my head around OOP topics. i understand that everything is a object, such as a dog is a object and has a property of saying bark. Or the dog inherits from the animal class and has four legs. But creating a program that states a dog say bark is pointless.

    I guess my problem is how does OOP help when creating a application and if you could explain OOP in very simple terms that would be great.

    Also i am no where near understand abstracts

  2. #2
    Join Date
    Jun 2013
    Location
    New York, USA
    Posts
    21

    Re: Understanding Object Oriented Programming

    Object-oriented programming (OOP) is a programming paradigm that represents concepts as "objects" that have data fields (attributes that describe the object) and associated procedures known as methods. Objects, which are usually instances of classes, are used to interact with one another to design applications and computer programs. C++ and Java are examples of object-oriented programming languages
    Basically you have the concept of Object-Oriented programming (the idea of creating objects to store values) you just need to understand why it is a good thing. It allows us to store data for later use in a program. HTML (the base language for creating websites) is not OOP, and because of this getting user data and working with it can be difficult (without using Javascript/PHP/MySQL). You can't store values to be later changed or used, you can only display what's on the page.

    If I had to think about outside the box, I would say it like this. OOP is like math and and HTML is like English. With HTML, you just read what ever is written on the page (like English). With OOP languages like Java, you can change these objects or variables and use them to help you solve a problem, not just display information. Does this help make any more sense or did I just screw with your mind more than needed? Let me know if it helped

  3. #3
    Join Date
    Jun 2013
    Posts
    3

    Re: Understanding Object Oriented Programming

    comparing it to HTML is helpful. however, couldn't you just store all of the information, that is needed to be used later, in a array instead of making a object. Sure you would have a lot more variables but it would still accomplish the same thing.

    for example, couldn't i create a array that for dog that holds, dog[0] = four legs. dog[1] = bark. etc.

  4. #4
    Join Date
    Jun 2013
    Location
    New York, USA
    Posts
    21

    Re: Understanding Object Oriented Programming

    Quote Originally Posted by meltingchain View Post
    comparing it to HTML is helpful. however, couldn't you just store all of the information, that is needed to be used later, in a array instead of making a object. Sure you would have a lot more variables but it would still accomplish the same thing.

    for example, couldn't i create a array that for dog that holds, dog[0] = four legs. dog[1] = bark. etc.
    Yes but objects are used for more than just calling upon stored input. They can be used to call other functions and can be used to access data from imports in a class. You will learn more about it once you dive deeper in the book.

  5. #5
    Join Date
    Jan 2014
    Location
    Lugano
    Posts
    7

    Re: Understanding Object Oriented Programming

    I am actually in your same situation. I have been told that you have to thing about an object like a box where you can place datas and create methods to take those datas and manipultate. Methods are like robots or human beings capable to work with those datas and create something (imagine a knife, a handsaw, an hammer or ring spanner) You take thosethings out of the box use to make a work and you place them back in the box when you don't need those tools anymore.

  6. #6
    Join Date
    Jan 2014
    Location
    Lugano
    Posts
    7

    Re: Understanding Object Oriented Programming

    If anybody can give a better explanation or want to correct me please go ahead. I am here to learn.

  7. #7
    Join Date
    Jul 2013
    Posts
    576

    Re: Understanding Object Oriented Programming

    Quote Originally Posted by tarmac View Post
    If anybody can give a better explanation or want to correct me please go ahead. I am here to learn.
    You can approach object orientation from different angles and to me it helps to consider what OO offers over the old procedural languages such as BASIC, C and Pascal.

    In procedural languages you can do this,

    int i = 42;

    Here the variable i of type int gets assigned the value 42.

    But OO languages don't stop there. They allow you to define your own types called classes. And for each class you can define a set of values called objects. The OO version of the above could look like this for example,

    Integer i = new Integer(42);

    In principle it's the same as before. The variable i of type Integer gets assigned the value 42. Only now the type is a class and the value is an object of that class. Also note the dual role of the class name (Integer). It acts both as type (left hand side of =) and as blueprint for object creation using new (right hand side of =).

    This abliity to define your own types (in addition to the built-in primitive types) is key to OO and it has great implications on style. In OO you program by type extension. You evolve a program by defining ever more classes using existing classes (defined by yourself or others) and primitives.

    That's OO programming in a nutshell but practice is more messy of course. For example Java has three kinds of classes; class, interface and enum with different properties and uses. And you can expand classes in two ways; inheritance and composition and that has implications. And you must master the concepts of encapsulation and polymorphism to design well. And you must recognize and use common design patterns. Etcetera. It takes years to become good at it.

  8. #8
    Join Date
    Jul 2013
    Posts
    576

    Re: Understanding Object Oriented Programming

    Quote Originally Posted by tarmac View Post
    If anybody can give a better explanation or want to correct me please go ahead. I am here to learn.
    Well if you're so ****ing interested why don't you reply?

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Understanding Object Oriented Programming

    Quote Originally Posted by meltingchain View Post
    I'm having a hard time getting my head around OOP topics.
    OOP is one of several methods for creating/designing programs.
    It is not ideal. It is not the only way to design programs, but as a whole it is largely accepted as "the best we have come up with so far".

    OOP does NOT relate to the real world and trying to do so will get you into trouble. Any books/tutorials that try to explain it this way tend to be the wrong kinds of books, they start with flimsy attempts at RL analogies, that invariably break down into nonsense once you elaborate them and try to actually make them work as a design concept.

    i understand that everything is a object
    Not everyting. there are still simple types that aren't.
    And not even in a full design needs everything to be an object.

    such as a dog is a object and has a property of saying bark.
    1) bark is not a property, it's a method.
    Make sure you know the difference between the two, in some languages this can be subtle because properties are automatically accessed via a 'hidden' get/set method, in some other languages, you need to explicitely provide getter/setter methods.

    A property is a "value"
    a method is "an action" (make the object do something)

    2) dog could be an object, it doesn't have to be.
    if you want it to be something more than just some values, you'll need it to be an object and have methods.

    3) Or the dog inherits from the animal class and has four legs.
    probably the first real world analogy failure.
    you could have an animal class, and derive a dog class from animal.

    but...
    does the dog have four legs or does the animal have four legs?
    either can work depending on the needs of your program. The failure here is that it doesn't need to be real world, just because in the real world an animal can have no legs (snakes), 2, 4 or maybe more (what about amputated legs, or mutants with abnormal amounts...)

    if your program only needs to handle animals with 4 legs, then it's perfectly acceptable to create an animal class that has 4 legs so all derived animals have 4 as well.
    your animal class doesn't represent the entire world of possible animals, but it doesn't have to.


    But creating a program that states a dog say bark is pointless.
    why pointless?
    Code:
    Dog myDog("bulldog");
    myDog.bark();
    is a perfectly reasonable program fragment. maybe the Dog class has a database of audiofragments and the above makes it play the bark of a bulldog through the OS audiosystem.


    I guess my problem is how does OOP help when creating a application and if you could explain OOP in very simple terms that would be great.
    Also i am no where near understand abstracts
    class design IS abstract because as I already said, "don't try to replicate the real world".


    you create classes that make sense within the confines of your program and into a hierarchical system that works even if it doesn't resemble the actual world.

    A certain amount of real world analogy is fine, but trying to get it "right" will not always work.

    if you make a program that handles files, then yes, you probably will be wanting a file class
    if those files hold information about books in a library,
    then you probably will want some form of book class and a library class and a system of keeping track of books located in the library.

    OOP is something you'll have to learn by practice. ANd yes, it will be dificult and mindbending, especially if you have been doing programming using other design philosophies.

    OOP will help especially in larger projects because it helps keep things in focus. When done right, you Always are working with only a very few different objects at any one time, so you don't need to keep thinking about everything.
    When done right, you have readable code, a good amount of reuse without "copy-pasting" chunks of code everytime you need something else that is "almost the same", the stuff that's not relevant outside of the place where something is needed can be made "hidden" (such as hiding class implementation and details from it's public interface) ...
    Last edited by OReubens; January 22nd, 2014 at 08:31 AM.

  10. #10
    Join Date
    Dec 2013
    Posts
    14

    Re: Understanding Object Oriented Programming

    I will try to be very simple and clean to be understood.
    Everything is object.
    No matter how much you differ, you have to carry some fundamental properties of your ancestors. Because you "inherit" their abilities.

    Interface, is the beating heart of Object oriented paradigm.
    You use remote for your TV. You know when you press a button, it does "something" for you. You don't have to know how it does. You don't have to know its frequency, current flow diagrams etc. You just know it would change the channel. Therefore it is an interface. You just need to know the usage of an object without knowing it's implementation.
    This gives you power to change almost everything about the implementation without changing the interface at all.

    Flying, to our understanding is an interface. Objects somehow manage to fly through thin air and move.
    Birds can do it.
    Planes can do it as well.
    So does superman.
    Parrot "is" a bird, therefore it "inherits" the ability to fly thanks to his ancestors. But again, parrot "is" also an object.

    Assume you are a commander, giving order to any "flying unit" to do your deeds. Your method is something like this,

    giveCommand(Flyer flyer);

    You can give commands to birds, parrots, planes and even superman. And you do not have to know how superman flies, how much torque plane engines produces, how parrot evolved. All you have to know is the Flyer interface. Like you did with your remote.

    This is the power of objcets encapsulating their behaviour and sharing what needs to be done only.

    This gives you flexibility to build huge softwares by small building blocks, this gives you advantage of changing little code yet changing whole system's attitude.

    This discipline is basically OO.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured