PDA

View Full Version : What is the difference b/w Procedural/Object oriented programming?



Recursive
06-30-2012, 02:34 AM
Basically what the title says

Rezozo
06-30-2012, 02:36 AM
It'll take a long time to explain, everything is different:

The functions, syntax, and what exactly can be done with it.

Edit: I think this could be in a more appropriate section of the forums, Perhaps it should be moved to:
SRL-Forums > Programming > Programming

Brandon
06-30-2012, 03:01 AM
It'll take a long time to explain, everything is different:

The functions, syntax, and what exactly can be done with it.

Edit: I think this could be in a more appropriate section of the forums, Perhaps it should be moved to:
SRL-Forums > Programming > Programming


Functions are the exact same in Procedural Programming vs. Object Oriented programming in C/C++

The difference is the objects of course.

Procedural Programming uses mostly functions to get everything done. To simulate an object, you'd use a function with a bunch of variables and return a value that can be used to do something else.

Object Oriented programming is a structural type of programming which has a heavy utilization of Classes, Structs, Unions, Inheritance, Polymorphism, Pimpl Idioms, RAII, Interfaces, and Abstracts.
Object Oriented programming is mainly used for encapsulation!

You can say that Objects have Properties and accessors whereas Procedural programming does not.

How would you represent A Table In Procedural Programming? Well you'd probably make a bunch of vars like so:



int TableTopW = 10;
int TableTopH = 10;
int LegLength = 10;
int LegThickness = 5;
int TableTopArea = TableTopWidth * TableTopHeight;
bool RoundTable = false; //Square = True;
bool RoundLegs = true; //Square = False;

void CreateTable(int &TopWidth, int &TopHeight, int &LegLen, int &LegThick, bool &RoundTop, bool &RoundLegs)
{
//Create Some Tables and have our Parameters hold the values for each table made! This can probably be thousands of variables.
}


//The above code provides no encapsulation and can be accessed and changed at any moment in time.
//The above can represent anything.. Any table. We cannot have two tables with the same variables because
//editing one will change the other.. Another thing, Tables variables if allocated on the heap will not delete
//automatically!


class Table
{
private:
int TTW, TTH, LL, LT;
int TTA;
bool RT, RL;

public:
Table(int TopWidth, int TopHeight, int LegLen, int LegThick, bool RoundTop, bool RoundLegs);
~Table();

int GetTopArea();
}

Table::Table(int TopWidth, int TopHeight, int LegLen, int LegThick, bool RoundTop, bool RoundLegs) : TTW(TopWidth), TTH(TopHeight), LL(LegLen), LT(LegThick), RT(RoundTop), RL(RoundLegs){}
Table::GetTopArea(){ return TTW * TTH;}

//Now this provides the user an interface. Every time you want to make a new table, you simple call the constructor like so:
Table FirstTable = Table(10, 10, 10, 5, true, false);
Table SecondTable = Table(50, 50, 25, 15, false, true);

//I've just created two tables without extra variables and stuff.. These tables have properties that can be accessed through the . operator or -> operator depending
//on how it was constructed.

//In Procedural programming, I'd have to make separate variables for EACH table! And Creating a new table will not be so easy!


Now referring to RAII classes, if your object has a pointer member, the class will automatically delete it on deconstruction if implemented right. You don't have to worry about anything. The user will be a happy lad.

Now in Procedural programming, you miss one thing and bam your code blows up in your face. For every new/new[] you have to remember to delete/delete[] respectively.

The functions within an object and outside an object are the same syntax and thus the same thing except one is encapsulated and the other isn't. One can use the same variables for different objects constructed whereas the other may need to be passed these variables as parameters for every thing you want.

Now this may not be a practical answer but it explains what is needed to know. The reason it's not practical is because I used basic functions and strict Procedural programming with no classes or objects. But because of this example, it shows a far better understanding than if I were to mix them both. Hope you understand.

Now I know this example fails to show the usefulness of Procedural programming but think of it this way. Without Procedural programming, for everything you'd be constructing an object that contains the function you need. Or accessing some random namespace.

No one wants to do that for every single thing! So hence we have Procedural programming for global things, for things we will be doing over and over (we just call a simple function, etc).

If you check out a fully object oriented language such as C#, you will notice there are no such things as global functions. A class must be constructed within the same namespace. That class will contain your function and you have to access it through that class/object.

In other words, Learn both Procedural and object oriented programming because BOTH have their advantages over the other!

Based Lord
06-30-2012, 03:06 AM
+1 After 2 years of computer science I was asking myself the same question :p

nielsie95
06-30-2012, 08:33 AM
Brandon, you are talking about procedural, not functional programming.
Chig, just consult Wikipedia (http://en.wikipedia.org/wiki/Procedural_programming).

Brandon
06-30-2012, 08:48 AM
Brandon, you are talking about procedural, not functional programming.
Chig, just consult Wikipedia (http://en.wikipedia.org/wiki/Procedural_programming).

His thread Title says: "What is the difference b/w Procedural/Object oriented programming?"I tend to call Procedure Functional because Functional is also sometimes called Statistical programming and calling it the other way confuses me sometimes. Logic is there though.
I know what you mean though.. That O^N* Sorting Algorithms, recurssion, lambdas and what not. I'll edit my Post terms so OP doesn't get confused.

superuser
06-30-2012, 09:44 AM
Functions are the exact same in Procedural Programming vs. Object Oriented programming in C/C++

...

For the reference, there is no such language as C/C++. There is C and then there is C++. You don't have objects/classes in C.

Brandon
06-30-2012, 03:04 PM
For the reference, there is no such language as C/C++. There is C and then there is C++. You don't have objects/classes in C.

Why so picky? I've used C/C++ only once in my answer and it simply explains the concept to the OP. That statement is completely untrue and doesn't answer OP's question. This is object oriented programming in C:

http://msdn.microsoft.com/en-us/library/cc815324%28v=office.12%29.aspx

It's done with a C++ concept using vTables and polymorphism. You don't have provided classes in C but it doesn't mean you cannot program objects. There are tons of examples showing OOP programming in C on google and StackOverflow. Not only that, a lot of programmers use C/C++ when referring to thigns that can possibly be done in both or sometimes completely unrelated. The reason is because we all know that C/C++ go almost hand in hand and in this case, I used it "once" and correctly.

superuser
06-30-2012, 04:53 PM
Why so picky? I've used C/C++ only once in my answer and it simply explains the concept to the OP. That statement is completely untrue and doesn't answer OP's question. This is object oriented programming in C:

http://msdn.microsoft.com/en-us/library/cc815324%28v=office.12%29.aspx

It's done with a C++ concept using vTables and polymorphism. You don't have provided classes in C but it doesn't mean you cannot program objects. There are tons of examples showing OOP programming in C on google and StackOverflow. Not only that, a lot of programmers use C/C++ when referring to thigns that can possibly be done in both or sometimes completely unrelated. The reason is because we all know that C/C++ go almost hand in hand and in this case, I used it "once" and correctly.

Encapsulation is pretty easy, polymorphism is doable, but inheritence is tricky. Ok, I got your point ;) I shall stand corrected!

ShawnjohnSJ
07-01-2012, 11:17 PM
Wikipedia actually has a really good comparison of the two.




The focus of procedural programming is to break down a programming task into a collection of variables, data structures, and subroutines, whereas in object-oriented programming it is to break down a programming task into objects that expose behavior (methods) and data (members or attributes) using interfaces. The most important distinction is whereas procedural programming uses procedures to operate on data structures, object-oriented programming bundles the two together so an "object", which is an instance of a class, operates on its "own" data structure.
Nomenclature varies between the two, although they have similar semantics.