Jump to content
  • Advertisement
Sign in to follow this  
Cipher3D

Why OOP?

This topic is 4878 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to explain OOP to somebody; he can understand what it is and how to write OO code, but he doesn't get WHY it is needed. I use it, but I can't find a very easy way to explain it. He says, why not just use functions? I explain that a higher method of organization is needed, but he feels that there is no great need for it; this is probably because he's used to being a solo programmer. How would you explain it?

Share this post


Link to post
Share on other sites
Advertisement
Generally, I would be led to believe that anything you can do in an object-orientated manor can be in a procedural manor. However, it's more of a degree of difficulty and code cleanliness.

Ask him to implement object polymorphism using just functions and structures, and time how long it took. Then, you do it using OOP, and time how long it took. :)

There's many pages out there (ie. google) that describe the differences between, and the pros and cons to using either.. unless there's a PhD in the house who's willing to explain it all here.

Share this post


Link to post
Share on other sites
OOP isn't needed, it's simply useful. It's useful in the sence that it makes a lot of things easier. That claim probably sounds rediculous to someone who doesn't undertand OOP in detail, but it's true. OOP allows programs to be written in a way that more accurately reflects the real world model of what is going on. It's a more intiutive way to structure programs, and it does have real benefits. If he can't see what those benefits are, he probably doesn't understand OOP as well as he thinks he does.

Share this post


Link to post
Share on other sites
OOP is used to provide common functionality among many classes who have similar features / implementations but do differ. It makes writing these classes much easier by just inheriting default functionality and overriding it. Calling functions is a waste of time when common functionality is provided already by a class that can just be inherited by a base class.

Also, calling all the functions necessary to perform tasks manually is a waste of time if there are functions that do it all for you just because your class has an is-a relationship with a superclass.

Without OOP, you wouldnt be able to write a server in 5 lines in java, or thread your app just by extending a class.

Share this post


Link to post
Share on other sites
I explained it from a design perspective to sumone once, seemed to understand.

as in "lets make an parent class called object, then a child furniture, then a child table" etc...

that one seems to work for me

Share this post


Link to post
Share on other sites
Quote:
Original post by poisonfruitloops
I explained it from a design perspective to sumone once, seemed to understand.

as in "lets make an parent class called object, then a child furniture, then a child table" etc...

that one seems to work for me


It's easy to understand what it is, the hard part is to explain to someone why you would use it in a program.

Share this post


Link to post
Share on other sites
OO is useful for a few reasons.

1. Data / method hiding. You don't know what's going on behind the object. For example, I have a FileManager class. In unencrypted mode, it loads files. In encrypted mode, it uses a virtual file format. My code doesn't know the difference between the two.

2. Easy to divide projects. I can make stub(fake) classes, tell each of my team members to fill in the class with working code.
~~
You can code in OO without OO specific code. You can code OO with just functions, if you're careful with names and dependancies. He maybe already be using a pseudo-OO style, it's just difficult for you to read. If that's the case, he would be fairly resistant, because OO is more work for not much gain.

I would suggest designing with OO features, like using CObject to point to CPlayer and CEnemy, which would force him to use OOP. Tricky, but it may work.

Share this post


Link to post
Share on other sites
The best reason to use it is to save lots of time. Suppose you are writing classes which all provide similar functionality (Linked Lists). There is no point in rewriting everything when you can inherit 80% of relevant code and override the 20% that doesnt apply.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It is needed because when you understand it, you are pretty much able to eliminate all code that looks like the following nightmare logic I once had:


bool parser::optimizeStatement(tokStruct_ *tokList) {
int len=0;

//find length of statement. Other logic has assurred end of TokSemi or TokLBrace
while (tokList[len].tok != TokSemiCol && tokList[len].tok != TokLBrace) len++;

//first, if there is an assignment in the statement change the tokenVar before it to
//the correct one (ie - Tok0DimDblAss).
for (int pos=0; pos<len; pos++) {
if (tokList[pos].prec == PrecAssign) {
//back up until the preceeding PrecVarOrVal is found
//(and skip over any inside brackets)
bool isNested = false;
int nestLevel = 0;
for (int prevTok=pos-1; prevTok>-1; prevTok--) {
if (tokList[prevTok].tok == TokRBracket) {
nestLevel++;
isNested = true;
}
if (tokList[prevTok].tok == TokLBracket) {
nestLevel--;
if (nestLevel == 0) {
if (!isNested) {
MsgBox("'[' found without corresponding ']'");
return moveCursorToError(tokList[prevTok].strPos);
}
isNested = false;
if (prevTok == pos) {
MsgBox("Array dimensions given w/o variable");
return moveCursorToError(tokList[prevTok].strPos);
}
continue;
}
}
if (nestLevel == 0) {
if (tokList[prevTok].prec == PrecVarName) {
//need to change the token to PrecVarOrVal and place the address of the
//variable on the token.pos (which currently holds a pointer to the name
//of the variable
bool found = false;
for (int i=0; i<varListSize; i++) {
if (strCmp( (char*)tokList[prevTok].pos, varList.name) ) {
tokList[prevTok].pos = i;
found = true;
break;
}
}
if (!found) {
MsgBox("No matching variable found");
return moveCursorToError(tokList[prevTok].strPos);
}
tokList[prevTok].prec = PrecVarOrVal;
}
if (tokList[prevTok].prec != PrecVarOrVal) {
MsgBox("Need variable name before assignment");
return moveCursorToError(tokList[pos].strPos);
}
if (tokList[prevTok].tok == TokInt || tokList[prevTok].tok == TokDouble) {
MsgBox("Cannot have value before '['");
return moveCursorToError(tokList[prevTok].strPos);
}
tokList[prevTok].prec = PrecVarAssign;
//now, have the previous token. Change it accordingly to an assignment
//of the correct type (Int, Double...)
switch (varList[tokList[prevTok].pos].type) {
case Int:
tokList[prevTok].prec = PrecVarAssign;
switch (varList[tokList[prevTok].pos].dims) {
case 0:
tokList[prevTok].tok = Tok0DimIntAss;
break;
case 1:
tokList[prevTok].tok = Tok1DimIntAss;
break;
case 2:
tokList[prevTok].tok = Tok2DimIntAss;
break;
case 3:
tokList[prevTok].tok = Tok3DimIntAss;
break;
default:
MsgBox("Bad var dimension given");
moveCursorToError(tokList[prevTok].strPos);
break;
} //end of Int dims switch
break;
case Double:
tokList[prevTok].prec = PrecVarAssign;
switch (varList[tokList[prevTok].pos].dims) {
case 0:
tokList[prevTok].tok = Tok0DimDblAss;
break;
case 1:
tokList[prevTok].tok = Tok1DimDblAss;
break;
case 2:
tokList[prevTok].tok = Tok2DimDblAss;
break;
case 3:
tokList[prevTok].tok = Tok3DimDblAss;
break;
default:
MsgBox("Bad var dimension given");
moveCursorToError(tokList[prevTok].strPos);
break;
} //end of Double dims switch
break;
default:
MsgBox("Bad var type given");
return moveCursorToError(tokList[prevTok].strPos);
} //end of type switch
//now, breakout and find any more assignments in the statement
break;
} //end of 'if (nestLevel == 0)'
} //end of 'for (int prevTok=pos-1; prevTok>-1; prevTok--) {'
} //end of 'if (tokList[tempPos].prec == PrecAssign) {'
} //end of 'for (int pos=0; pos<len; pos++) {'

//now, go through the list and change any token.prec's which are PrecVarName
//to PrecVarOrVal and change the token.pos to the correct one on the varList
for (int i=0; i<len; i++) {
if (tokList.prec == PrecVarDeclare) break; //breakout if it is a declaration
if (tokList.prec == PrecVarName) { //statement
//tokList.prec = PrecVarOrVal;
bool found = false;
for (int j=0; j<varListSize; j++) {
if (strCmp( (char*)tokList.pos, varList[j].name) ) {
tokList.pos = j;
switch (varList[j].type) {
case Int:
switch (varList[j].dims) {
case 0:
tokList.tok = Tok0DimIntVar;
break;
case 1:
tokList.tok = Tok1DimIntVar;
break;
case 2:
tokList.tok = Tok2DimIntVar;
break;
case 3:
tokList.tok = Tok3DimIntVar;
break;
default:
MsgBox("Illegal dimension given");
return moveCursorToError(tokList.strPos);
}
break;
case Double:
switch (varList[j].dims) {
case 0:
tokList.tok = Tok0DimDblVar;
break;
case 1:
tokList.tok = Tok1DimDblVar;
break;
case 2:
tokList.tok = Tok2DimDblVar;
break;
case 3:
tokList.tok = Tok3DimDblVar;
break;
default:
MsgBox("Illegal dimension given");
return moveCursorToError(tokList.strPos);
}
break;
default:
MsgBox("Wierd error");
return moveCursorToError(tokList.strPos);
}
found = true;
break;
}
}
if (!found) {
MsgBox("No matching variable found");
return moveCursorToError(tokList.strPos);
}
}
}

//can add other optimizations here (like eliminating '[' ',' ',' ']' for some arrays)
return true;
}


All of that crap disappeared, and I could at last create things I could follow. Virtual functions and OOP - what a godsend!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!