[C++/A5] OOP Code

Started by
3 comments, last by georger.araujo 9 years, 7 months ago

Hey, how would one best go by making this code Object Oriented?

I have tried a bit here and there, but I keep getting 'not defined in this scope' errors etc..

Here's my code :)

http://pastebin.com/vMHCTBu9

Any help is very much appreciated :)

Advertisement

You identify state that you can store somewhere, and give it some kind of name that describes what it is. Write some methods for that object. Why would you want to take some pre-existing code and apply OOP to it? Especially if you just have some functions calls to an API and that's it.

I imagine you shouldn't even really worry about what is or isn't OOP and just try to write code that makes sense to you. I hope this helps!

Object orientation is a modelling technique. The obvious candidate for modelling might be the buttons. Right now, buttons are an implicit concept in your program, a consequence of the hard coded logic. If you stripped out the comments, it might not even be clear that there are buttons. An alternative way to write this is to create a Button class or structure, and write code that displays and manipulates Button instances.

Why don't you show us what you've tried?

Because the code is not even done Randy. I am planning to add more to it, and it will just become a mess on the long-term.

Because I would like to take the time and learn OOP, as I've heard it makes life easy.

Also I plan on having people helping me with code in the future, as a team.

Thank you Rip-off, I'll try and recreate what I tried before, and post it here :)

Because the code is not even done Randy. I am planning to add more to it, and it will just become a mess on the long-term.

Because I would like to take the time and learn OOP, as I've heard it makes life easy.

Also I plan on having people helping me with code in the future, as a team.

Thank you Rip-off, I'll try and recreate what I tried before, and post it here smile.png

The code not being done is not a problem, it can actually be a *good* thing. Should you postpone changing your approach from procedural to object-oriented until later, it's quite possible you'll have to tackle a number of problems (e.g. tight coupling, global variables, walls of code) on top of implementing your design.

Saying that OOP makes life easy is a bit superficial. Nothing prevents you from coming up with an object-oriented design that's poorly abstracted and tightly coupled. Even if your design is good, you could end up writing a really bad implementation. I'd say that, if you come up with a sound design and follow good practices, you can write object-oriented code that is easier to extend and maintain than procedural code, and be more productive - but it's still hard work.

EDIT: About your question, I think you can learn much from the design and implementation of the source code of the book SFML Game Development. I took a look at your code, and I think you can use the code from the "01_Intro" chapter as a starting point.

This topic is closed to new replies.

Advertisement