-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|609|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
SoCoder -> Article Home -> Advanced Techniques


 
Scherererer
Created : 23 November 2006
Edited : 04 January 2007

OOP

A Guide To Object-Oriented Programming

A whole host of people are scared of OOP. They don't understand it, and immediately think it is a very complicated system. In reality, its not; in fact it is a more natural and elegant way of thinking. In this article, I will outline what OOP really is on a conceptual basis, so that it can be applied to your choice of language. First and foremost, OOP means Object Oriented Programming; a program can also be considered Object Oriented (or OO) if it implements OOP.

Now you're prolly wondering, "what's an object?" Simply put, its an encapsulation of data and code into a single entity. Now you're thinking, "well why can't I just use structs or types or arrays". Here's the short answer: because there is so much more functionality this way. Lets look at a simple struct for a person (for blitz users, a struct is what people using C call a type essentially, I'm just using structs because more people are farmiliar with them):



That's convenient, we've created a method of organizing the attributes of a person into a single data structure. But with structs or types, that's about all we can do. What if I wanted to add actions for this person? How could I do that neatly? This is the best implementation I've seen:



That's a rather long and tedious implementation. Not to mention the fact that I always have to specify who I want to move or set name through a parameter. And additionally, I have to add a Person_ prefix to all functions relating to the Person struct so that I know that this funcion moves a person, and not an animal or a plant. Now, lets look at an object-oriented approach to the same thing:



Now look at how well organized that is. Now that we have defined a struct and a class, lets look at how we would use each of them. First, I'll show a snippet of code demonstrating the implementation of the struct.



now the class:


Well, that saved me a bit of typing. In addition to that, its very clear what I'm performing the action on. If I look at the struct implementation, the location where I specified who i'm performing the action on is nested in the middle of a function parameter, and could be in the middle of a long list of parameters. However, with the class its at the head of the call, so I know exactly who's making it happen.

Now you're propably wondering what this whole "new" thing is about in the class implementation. I mean, it looks like I'm calling PersonClass() as a function there. Well, that is called a constructor. A constructor essentially constructs an object of a class, in this case an object of the PersonClass class. By default, a language usually makes a default constructor, however we can make our own constructors if we want, such as in this example:



Wait wait wait; It looks like I just put some parameters on this constructor thing.... Well, you're right, I did. Now lets take a look why. Here is the code for the struct and then the class:





Big difference. As you can see, with the constructor I was able to create an object of the class with several fields already set so that I don't have to set them manually later on.

I hope this gave you a head start into the world of OOP, later on I'll make an article on some of the more advanced techniques such as inheritance and polymorphism. Don't worry, those words only sound scary; in reality they're pretty simple. Till next time!

 

Comments


Thursday, 23 November 2006, 10:03
magicman
Personaly the idea of oo programming isnt scary, im just to lazy to try to learn it. really, i would love to learn it, it sounds fun. anway, this is a nice artical, thank you.
Thursday, 23 November 2006, 16:46
hyruleknight
cool tutorial. i already know OOP and still learning new technigues every day to make better code. i will recommend people to your tutorial when they want to learn OOP
Thursday, 23 November 2006, 17:38
Agent
I think describing an object as simply "a collection of data" is a bit misleading. It's more like an encapsulation of data and code into a single entity.

Also, in your code examples, shouldn't e.g. char[] name; be char name[];
(with an array dimension, if it's uninitialized) ?
Thursday, 23 November 2006, 21:03
Scherererer
Ah bloody, you're right; heh, sorry, i'm used to the C#/Java implementation wherein both char[] name and char name[] are valid. I'll change it. And also, a collection of data was just the easiest way that I could put it... I'll use your terminology though as it is more accurate.
Friday, 24 November 2006, 08:50
JL235
Please note I don't really know c or c++, but in one of the code blocks, shouldn't it be 'p.name = _name;', not person. Also couldn't the same effect of the constructor be also achieved through having a 'newPerson' function. Something like:


Then wouldn't you be achieving the same advantage as your OOP example.
Friday, 24 November 2006, 14:13
Agent
You're right, but in your code example, ps should be allocated in the free store (heap) and not on the stack.

Anyway, I doubt whether in practice any experienced C programmer would write a one-line "set" function just to assign a struct member, but I think Instinct was using it as an example to illustrate the difference between non-OOP and OOP-style syntax.
Thursday, 04 January 2007, 03:25
JL235
Why wouldn't they create a function for making new structs? It would make the code smaller and cleaner in the long run, helps to remove code repetition and abstracts the creation of the struct into it's own function. This improves readability and eases maintenance of the code.

Now I always make a seperate function for making types in Blitz.
Thursday, 04 January 2007, 05:44
power mousey
um eons ago and in this same galxay and the third rock from the sun

I used to do a lot of OOP programming and in Turbo C++
and also with the DEV C++ package too.

When using both Turbo C/C++ and even Microsoft C and on both my Tandy and then Packard Bell computers, I really enjoyed the linked lists utilizing both structures and elements of a class too.

brings back memories

I especially like structures. true.

cheers,