• 15
• 15
• 11
• 9
• 10

# Help understanding objects? (C++)

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

## 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?

 vector<string> inventory; inventory.push_back("sword"); inventory.push_back("armor"); inventory.push_back("shield"); 

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 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 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 on other sites
To understand objects I highly recommend the book "Object-Oriented Analysis and Design with Applications (3rd Eddition)".

##### 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 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 on other sites
"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 on other sites

"sword", "shield" and "armor" as they appear in the code are string literals

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 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 on other sites

"sword", "shield" and "armor" as they appear in the code are string literals

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. :-)