Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Objects Vs plain functions 2

Status
Not open for further replies.

Microbe

Programmer
Oct 16, 2000
607
AU
Hey there,

I have started learning about and trying to use objects and classes and was hoping someone could clarify for me why they are any better/different from just using functions.

It seems that the end result is the same, the coding is pretty similar so why are objects better?

Thanks in advance

Steve
- I have fun with telemarketers
 
Functions and Classes are 2 different things.

A Function is a collection or series of procedures that take some input and process it and then return a product of that process.

Classes on the other hand are collection of Functions properties, and other items that work together to create well an object.


for example a function could be:

Code:
paint_chair($color){
}


While an object or class, would be a series of functions and properties of that chair

Code:
public chair{
$height as int
$length as int
$width:as int
$color as hex
function create_chair();
function change_color();
etc...
}

In other words Classes can be used to define objects or instances of objects.


If I wanted to create a chair I would say

mychair = new chair;
and then i could call any of its functions or access its parameters or properties.
mychair.height=5ft.




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
One of the beauties of classes and objects (i.e. Object-Oriented Programming) is encapsulation. A class defines state (e.g. height, colour etc.) and behaviours (e.g. DrawBox, CalculateVolume etc.) - essentially a blueprint for an object. All the information about that particular entity is contained in one place and makes logical sense.

I'd say one of the main benefits of OOP is reusability - a class (or subsystem comprising many classes) can very easily be included in lots of different projects. Also, the concept of extensibility is invaluable whereby a new class can inherit the state (or properties) and behaviours of an existing class and then be extended to provide further functionality without affecting the original class.

Do a google search for "benefits of oop over procedural" and there's lots of article of varying opinions.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top