Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

can you give me an example of when Classes or Structures would...?

actually be helpful. because right now to me, they only seem to make things more complicated, perhaps thats just because of what i'm programming.

but i'm kind of having trouble understanding why exactly we need encapsulation in programming also.

4 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Hi,

    Classes: reference types

    Structures: value types.

    Reference types are allocated on the heap, and memory management is handled by the garbage collector.

    Value types are allocated on the stack or inline and are deallocated when they go out of scope.

    When you can using them?

    Defining a structure instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.

    For additional information you can visit the following page:

    http://msdn.microsoft.com/en-us/library/ms229017.a...

    encapsulation:

    The ability of an object to hide its internal data and methods, making only the intended parts of the object programmatically accessible.

    why exactly we need encapsulation in programming?

    You can visit the following page:

    http://www.c-sharpcorner.com/UploadFile/ggaganesh/...

  • 1 decade ago

    I don't use structures any more since I moved to .NET.

    A class is great because it uses the "black box" method of programming.

    An object can have properties, and actions, and you don't need to know HOW the class works to be able to use it.

    For instance, if you have a class that represents a vehicle (maybe you get a job with the police department, and they want to be able to keep track of all the vehicles with tickets).

    A vehicle can have a MAKE, a MODEL, a LICENSE PLATE, a STATE, COLOR, NUMBER OF DOORS, etc.

    The "VEHICLE" class models a real world vehicle, and the vehicle can DO things (i.e. functions and procedures) built in to the class. "MOVE FORWARD" or "OPEN DOOR", etc.

    The actions that take place through the class functions are carried out, and variables that represent different conditions can change.

    For instance "MOVING" can be a boolean value. If you call the "MoveForward" procedure, "Moving" is set to True, and "Location" can be changed in the "MoveForward" procedure within the "Vehicle" class.

    "Lights" can be a boolean variable to represent the condition of the headlights, "on" or "off", and the procedures "LightsOn" and "LightOff" can change that value accordingly.

    You can have a function called "AreLightsOn" and return the value of "Lights" (True or False).

    All this is done within the class and the person who uses this class doesn't need to know anything about the internal structure or function of the class to be able to use the "Vehicle"

    To use the "Vehicle" class in Visual Basic, you would use this line of code:

    DIM Ford as Vehicle = New Vehicle()

    Then, something like this:

    If Ford.ParkingBreak is True THEN Ford.NotMoving

    Or within the class itself, in the "MoveForward" procedure, you can have something like:

    If ParkingBreak is True then Sxit Sub

    Then continue with the rest of the procedure to simulate what ever you want to do with in the "MoveForward" procedure.

    Does any of this help?

    One last item is something called "Inheritence"

    You can create a basic "Vehicle" class, then create specific classes based on the "Vehicle" class, that has all the attributes of the "Vehicle" class, but then you can add functionality to the new class.

    There are things you can do with classes that you can NEVER do in the main program that doesn't use classes.

    Also, an Object can include another Object.

    A Car can include the Door Class, the Windows Class, and even the Seats class.

  • 1 decade ago

    Encapsulation:

    You use Encapsulation to keep the parts of your code that stay the same separate from the parts that change. With that it's easy to make changes to your code without breaking everything.

    OOP (Object-Oriented Programming):

    check http://java.sun.com/docs/books/tutorial/java/conce...

  • 1 decade ago

    I recall an example in XNA (C# Game Engine) where we created a structure to stores the main players stats (variables). Thats all I remember......

Still have questions? Get your answers by asking now.