LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-08-2009, 01:16 AM   #1
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
OOPS, I have to do OOP


I have tried to get my head around OOP as 'the world' told me it was the way to go. However in the simple examples that usually go with tutorials and books, I failed to see the difference between a class and a struct; anything in those simple examples could be done in C so what is all the fuzz about. The idea of a private member appealed to me till the example showed the setter method that still allowed access by any piece of the application that knows about the class; so much for protection. So there I was, not seeing advantage in using OOP, and falling back to the languages that I was familiar with.

However, there was that nagging feeling that 'the world' could not be that stupid so there had to be something. And after a while I tried again. I think I've tried about 5 times (from the days of TurboC++ through Borland C++ for windows 3.x till recently MS C#).

Now, due to a job change, the time has come and I have to use OOP. One of my first projects is JAVA related.

Wish me luck and maybe, one day, I will be able to explain advantages of OOP over normal languages and vice versa.

PS I bought 'java for dummies' and the impression is that it indeed seems to be able to clear some things for me.
 
Old 09-08-2009, 01:35 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Good Luck Wim.

Kind regards,

Eric
 
Old 09-08-2009, 02:27 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
First, all the best.You do raise a few important points about the OO approach.

The first is what is the different between a class and a struct. I see this essentially as a convenient way of grouping your code. With a class there is (built into the language) a mechanism of placing the data and the functionality together. This helps when it comes to designing the code and maintaining the code.

The other is with the concept of private member variables and a setter methods. First not all member variables need to have setter methods, or the setter methods could themselves be private, restricting access to member variable to the class itself. The other important point is that for setter methods this is where you put the validation. For example if you have a member variable that holds the ISBN for a book the setter method should validate that the checksum is correct.
 
Old 09-08-2009, 02:37 AM   #4
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
In simple cases, "class" is just syntactic sugar for a struct. The C++ member function

Code:
void Stack::push(int newValue);
can always be trivially converted to a C-style function by making the "this" pointer explicit.

Code:
void Stack_push(Stack* obj, int newValue);
The difference comes when you have inheritance and virtual functions.
 
Old 09-08-2009, 07:53 AM   #5
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by Wim Sturkenboom View Post
I failed to see the difference between a class and a struct
Several important things have already been said on this thread, but since nobody else has said the following so far, please let me say it:

In C++, the only difference between a class and a struct is that in a class, members are private by default, and in a struct, members are public by default.

Inheritance, member functions, member functions as the only way to set private data, virtual functions, all the rest are available for both classes and structs in C++.
 
Old 09-08-2009, 10:24 PM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
OOP is a lot of hype, but it got stuck and some people can only think in OOP.

In procedural programming, you call a function and pass objects (variables, structs) as parameters on which you operate. In OOP you take an object and call the function to perform an operation on that object. There is not that much difference.

Problem is that once you master OOP, my experience is that it is difficult to change your mind and write again a procedural program. Procedural programming appears as programming inside out.

The big advantage of OOP? Polymorphism and Inheritance? Well, sometimes. If you have to program GUI environments (I mean writing the libraries, not using QT or so) it might be beneficial.

What I have against OOP is that it might be totally unreadable. If you have a pointer in your source code, do you know to what object it points? What method in the inheritance chain will be called? After all, source code is source code as we know it because we want it to be readable for humans. If that weren't true, we could happily program away in Brainfuck.

I have seen (unfortunately) program being written in PHP where the common ancester consisted of 50 lines of code, 4 different derived objects overloading every method, and consisting of 1000-2000 lines of PHP code. The classes as such were not in one inheritance chain, but all derived of one base object. So what is the benefit of OOP? There is no inheritance, no polymorphism and no code re-use, just confusion (with me)

OTOH, you say you have to program in Java. This is an extremely powerful OOP framework, providing an wealthy library of routines and objects you can use. It is just another tool, OOP or not.

jlinkels
 
Old 09-08-2009, 11:08 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by jlinkels View Post
OOP is a lot of hype, but it got stuck and some people can only think in OOP.

In procedural programming, you call a function and pass objects (variables, structs) as parameters on which you operate. In OOP you take an object and call the function to perform an operation on that object. There is not that much difference.

Problem is that once you master OOP, my experience is that it is difficult to change your mind and write again a procedural program. Procedural programming appears as programming inside out.

The big advantage of OOP? Polymorphism and Inheritance? Well, sometimes. If you have to program GUI environments (I mean writing the libraries, not using QT or so) it might be beneficial.

What I have against OOP is that it might be totally unreadable. If you have a pointer in your source code, do you know to what object it points? What method in the inheritance chain will be called? After all, source code is source code as we know it because we want it to be readable for humans. If that weren't true, we could happily program away in Brainfuck.

I have seen (unfortunately) program being written in PHP where the common ancester consisted of 50 lines of code, 4 different derived objects overloading every method, and consisting of 1000-2000 lines of PHP code. The classes as such were not in one inheritance chain, but all derived of one base object. So what is the benefit of OOP? There is no inheritance, no polymorphism and no code re-use, just confusion (with me)

OTOH, you say you have to program in Java. This is an extremely powerful OOP framework, providing an wealthy library of routines and objects you can use. It is just another tool, OOP or not.

jlinkels
Not only the item in bold, but it can be totally counterintuitive (my favorite ellipse <-> circle example).

Simplistically, OOP makes sense when we have "small deltas", i.e. there is a powerful base class and small modifications need to be made through inheritance to enhance functionality.

At the moment the modifications become big, the code becomes unreadable - as you've correctly pointed out.

Another problem - when there are too many deltas, it's difficult to see the whole picture.

...

To OP - yes, the whole world may be that crazy. OOP is not the only paradigm, and not at all always a convenient one.

There are more basic things: visibility/scope, data protection, making code modification easy, etc. OOP is just a way, and not always a good way.
 
Old 09-08-2009, 11:16 PM   #8
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
Thanks guys for the 'good lucks', the explanantions and sharing the sentiments.

WimS
 
Old 09-08-2009, 11:18 PM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Oh, on public setters on private data members - this is my favorite OOP nonsense. The only advantage it may have is simultaneous modification of all setting, i.e. instead of

Code:
void set_a(double b)
  {
  a = b;
  }
one can make it

Code:
void set_a(double b)
  {
  a = 1.1 * b + c;
  }
.

Likewise, traceability - instead of

Code:
void set_a(double b)
  {
  a = b;
  }

Code:
void set_a(double b)
  {
  fprintf(stderr, "about to set a to %g\n", b);
  a = b;
  }
.

But the above features have nothing to do with OOP.
 
Old 09-09-2009, 02:41 AM   #10
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by jlinkels View Post
What I have against OOP is that it might be totally unreadable.
Is there a paradigm for which this is not the case?

The OOP approach can help to design large programs and make them maintainable, but by breaking them down into smaller more manageable chunks it is necessary to have a good design, but again that is true of any paradigm.
 
Old 09-09-2009, 02:45 AM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by graemef View Post
Is there a paradigm for which this is not the case?

The OOP approach can help to design large programs and make them maintainable, but by breaking them down into smaller more manageable chunks it is necessary to have a good design, but again that is true of any paradigm.
Every modular approach helps to make large programs maintainable.

I.e. the basic values are compartmentalization/encapsulation/protection/reuse.
 
Old 09-09-2009, 08:09 AM   #12
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by Sergei Steshenko View Post
Oh, on public setters on private data members - this is my favorite OOP nonsense. The only advantage it may have is simultaneous modification of all setting
...
Quote:
Likewise, traceability
There is a third advantage: checking the validity of a proposed new value of a data member.

The usual example is to make sure that the proposed new value is within a certain constant range. But this checking can be as complex as you like. For example, it may be valid to set a data member to a certain value until a certain date, but not after that date.
 
Old 09-09-2009, 12:07 PM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by wje_lq View Post
...

There is a third advantage: checking the validity of a proposed new value of a data member.

The usual example is to make sure that the proposed new value is within a certain constant range. But this checking can be as complex as you like. For example, it may be valid to set a data member to a certain value until a certain date, but not after that date.
I agree. But, again, it has nothing to do with OOP. AFAIK, C# "allows" '=', and default setter is called. Once you change your mind, you just override the default setter not changing your code.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
OOP in C CoderMan Programming 8 04-03-2009 03:27 AM
OOP what it is and how :) PB0711 Programming 16 07-11-2006 10:52 AM
OOP Help Please InvisibleSniper Programming 41 09-18-2005 03:19 AM
What is true OOP? tumana Programming 4 09-13-2004 06:51 AM
When to use OOP KptnKrill Programming 10 08-24-2003 01:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration