Sign in to follow this  
JeremyYox

Help understanding objects? (C++)

Recommended Posts

So today I'm trying to wrap my mind around what makes an object. I think I've got a marginal grasp on what an object is and really just want to make it concrete. The following is a snippet from the current example in my book. I've browsed through a handful of different explanations from various guides and tutorials and some have helped more than others - but what I got is an object is basically an (using my limited terminology to explain best I can) instance of a certain class that: a) has member data that it stores locally b) has member functions that can affect itself and other objects?

[code]
vector<string> inventory;
inventory.push_back("sword");
inventory.push_back("armor");
inventory.push_back("shield");
[/code]

In the snippet, 'inventory' is a vector class object while 'sword', 'shield', and 'armor' are string type objects? I can only assume the string types as I honestly don't see how they differ from string literals aside from explicitly being called as a string type. I haven't worked with objects yet, but the author keeps pointing out to 'keep in mind the vector diagram is abstract because these are string objects and not literals'...and yeah, I'd never know the difference at this point.

Am I getting warmer yet?

Edit: would the string objects count as member data of inventory? eesh, my mind is hurting. Edited by ebontide

Share this post


Link to post
Share on other sites
You are over-thinking it.

An object is a chunk of data.

A class is a chunk of data plus a group of functions that use the data.


The object can be ANY chunk of data. It can be a single byte. It can be an array of objects, such as a text string. It can be a set of multiple other objects, such as a label ("sword of slashing") a bunch of numbers and flags (isPoisoned, isCursed, damageModifier) and any other data you want to give it.

As for the "string object" vs "string literal" issue, that is a bit more technical. A string object is a chunk of data, usually an array of char values. A string literal like "abc" is a value you have typed in your code, it can be copied into objects.

Share this post


Link to post
Share on other sites
Sigh, thanks frob.

I'm just going to keep reading and hope better understanding comes from it. I'm probably just looking too far ahead. I want to understand what you're saying but it just opens a million more questions - for now I'll just keep at it. :) I honestly don't know why the author wants me to keep things in mind he hasn't covered yet to begin with.

You did, however, make it seem like less of a monster - so thats a plus.

Share this post


Link to post
Share on other sites
Thanks NEXUSKill, I'll definitely look that up. At this point though I think I'd be better off actually finishing an introductory book before branching off trying to tackle a particular part of a whole in depth. :) I just have a habit of trying to know every detail of what is presented prior to moving on, it eats at me otherwise.

I appreciate the advice.

Share this post


Link to post
Share on other sites
"sword", "shield" and "armor" as they appear in the code are string literals which works just as in C. Because the inventory is a a vector of string the push_back function will automatically take a string object as the parameter. But when you pass string literals the C++ compiler will try to create an anonymous string object by pasing the string literal to the string constructor and then pass that string object to push_back.

The string objects are not said to be members of the vector class. Class members are only the declarations of variables and functions what you can see in the class declaration. When you want to talk about the data that was put into a collection object it is better to use the word elements or objects.

Share this post


Link to post
Share on other sites
[quote]"sword", "shield" and "armor" as they appear in the code are string literals[/quote]

I was with you that far, but pretty much everything after that went miles over my head. I'm getting the feeling I'm in no position to start worrying over objects by the responses I'm getting. I'll keep to my studies and come back better equipped to tackle this hopefully. :)

Edit: Actually, I can make bits and pieces of sense from what you're saying, but its so clouded with misunderstanding on my part that its hard to tell just what to make of it. Again, I think its just a lack of understanding of base concepts and I need to study more.

Share this post


Link to post
Share on other sites
[quote name='ebontide' timestamp='1306269711' post='4815299']
[quote]"sword", "shield" and "armor" as they appear in the code are string literals[/quote]

I was with you that far, but pretty much everything after that went miles over my head. I'm getting the feeling I'm in no position to start worrying over objects by the responses I'm getting. I'll keep to my studies and come back better equipped to tackle this hopefully. :)

Edit: Actually, I can make bits and pieces of sense from what you're saying, but its so clouded with misunderstanding on my part that its hard to tell just what to make of it. Again, I think its just a lack of understanding of base concepts and I need to study more.
[/quote]

Probably a better book for your situation would be Accelerated C++, by Koenig and Moo.

Share this post


Link to post
Share on other sites
Ahh, I actually have this one. I started with "Getting started with game programming" because of the more casual approach but now it seems it doesn't have enough detail for me. Being only four chapters in, I can make the switch fairly easily.

I appreciate the recommendation - back to the basics I go. :)

Share this post


Link to post
Share on other sites
[quote name='ebontide' timestamp='1306269711' post='4815299']
[quote]"sword", "shield" and "armor" as they appear in the code are string literals[/quote]

I was with you that far, but pretty much everything after that went miles over my head. I'm getting the feeling I'm in no position to start worrying over objects by the responses I'm getting. I'll keep to my studies and come back better equipped to tackle this hopefully. :)

Edit: Actually, I can make bits and pieces of sense from what you're saying, but its so clouded with misunderstanding on my part that its hard to tell just what to make of it. Again, I think its just a lack of understanding of base concepts and I need to study more.
[/quote]

Maybe don't worry so much and just continue with the book and play with examples. It takes time to really understand programming. :-)

Share this post


Link to post
Share on other sites
[quote name='jonathanjansson' timestamp='1306276182' post='4815340']
Maybe don't worry so much and just continue with the book and play with examples. It takes time to really understand programming. :-)
[/quote]
+1.

@The OP: It's probably not so much 'understanding objects' that's the problem here, but rather just becoming familiar with the idiosyncrasies of C++. The fact that you can push a string literal into a vector of std::string has to do with single-argument conversion constructors (as described above), and definitely isn't the kind of thing one would be expected to understand, necessarily, if one were just getting started with C++ and/or programming in general.

So yeah, don't get too hung up on the idea of 'understanding objects'; just forge ahead, and things will gradually start to make more sense.

Share this post


Link to post
Share on other sites
[quote name='jyk' timestamp='1306282518' post='4815373']
[quote name='jonathanjansson' timestamp='1306276182' post='4815340']
Maybe don't worry so much and just continue with the book and play with examples. It takes time to really understand programming. :-)
[/quote]
+1.

@The OP: It's probably not so much 'understanding objects' that's the problem here, but rather just becoming familiar with the idiosyncrasies of C++. The fact that you can push a string literal into a vector of std::string has to do with single-argument conversion constructors (as described above), and definitely isn't the kind of thing one would be expected to understand, necessarily, if one were just getting started with C++ and/or programming in general.

So yeah, don't get too hung up on the idea of 'understanding objects'; just forge ahead, and things will gradually start to make more sense.
[/quote]

I appreciate the feedback, I started the first chapter of Accelerated C++ and the number of times they mentioned "just know what it is for now, not how it works" convinced me that my biggest problem at the moment is me in regards to learning. I'm going to keep going through my Beginning Game Programming book, try my best to let some things pass, and pull as much info from it as I can. I can always read it again if need be, right? :)

Anyway, thanks to everyone for your responses - I'm really enjoying learning the language despite the frustrations I've come across.

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