Sign in to follow this  
gdgenest

C++ Question from a Beginner

Recommended Posts

gdgenest    122
So first off hello to everyone! Nice to be here and meet you all! My question is about the object oriented programming aspect of C++. I've been teaching myself C++ (on and off) over the last few years and I've finally gotten to the point where my book describes creating variables and objects in the free store. The big problem I have is understanding how an object would actually be useful. I've tried creating programs using objects but I really don't understand why anyone would use them instead of just creating a set of functions and variables and separating them from the rest of the code with a comment line. If anyone could give me a simple example (no need to write any code, just describe it) of a situation where an object would be useful and how it would interact with other objects I would be extremely grateful!! Thank you very much!

Share this post


Link to post
Share on other sites
CrimsonSun    336
Object-oriented programming is all about making your code more modular: by simplifying your code into self-contained objects that mimic real life objects, you make your code more flexible, easier to understand and modify, and much simpler.

Share this post


Link to post
Share on other sites
blakedev    133
Separating them with comments would eventually get cluttered, especially with larger projects that, as CrimsonSun put it, would need to be more modular.

It's like you have this box of the CBox class, where you store all your stuff that would belong to gdgenest. Being modular, I would have a CBox object for my stuff, so would your neighbor, etc.
Doesn't that sound a lot safer and easier to manage than keeping all your stuff in your front yard, or in your neighbor's (function)house?

I hope my (rather strange) real-life example helps. Once you grasp OOP, it's pretty useful, and easy to reuse.

Share this post


Link to post
Share on other sites
Palidine    1315
Yeah. In a nutshell it's a way to make your code more modular. If all of the logic involved with, say, physics, is wrapped up in a class you can remove that class and plug in a different class with different logic (but the same interface) and your code will "just work". Essentially, the paradigm allows you to abstract information away in different parts of your application; part 1 doesn't care how part 2 does it's job, it just cares about the interface.

This makes a huge difference later when you're working with huge codebases with millions of lines of code. Since no other object can see inside the object I'm working on, i can do whatever I want with the code without having to fix up all the other places in the codebase that use this object.

-me

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this