Hello I'm learning a bit about data structures for the first time and so far I seem to understand how they work "I think" at this stage. My questions at this point would be.

1. Are data structures essentially classes minus methods which can preform actions
on the memebers that are held inside?

2. In the case that question 1 is true... Why would I then chose to use a data structure over a class as data being stored is usless without being able to preform actions on that data?

3. I realize that classes are basically objects, it seems data structures are givin some of that good old terminology that could be misleading. As in we give the structure a name which can be described as a "new type" or a "new object". Which term might be more correct and suitable for data structures?

4. I guess at this point my final thoughts on data structures is that data structures would be a good way to store data that you may not "at the time" know what you may possibly want to do with later on. Which seems a bit silly to me but possibly understandable, kinda sounds like a project so large that you couldn't possibly see or be planning far enough ahead to know why or what you are storing that data for.

I've done quite a bit of searching on data structures like I said and I'm sure will continue to do so an play around with them in some code, but if someone can help me out with the answers to those questions I think I'll have a much better understanding about them.

Recommended Answers

All 8 Replies

I'm far from any expert on said subject, but what I do know is that class members are by default privat and inaccessible unless called from within, unless declared public. Structures are by default public and can, to my limited knowledge, not hold any functions.

Feel free to correct me, and don't take anything I state for a fact.

so far I seem to understand how they work "I think" at this stage

I think you're still fuzzy on the distinction between data structures and classes. It helps to think of data structures as more of an abstract concept while classes are a concrete implementation detail. A data structure is really just a pattern of storing and working with data that achieves a specific goal (eg. fast searches).

4. I guess at this point my final thoughts on data structures is that data structures would be a good way to store data that you may not "at the time" know what you may possibly want to do with later on.

On the contrary, data structures are typically selected with a very specific data usage in mind. Occasionally you'll pick a data structure that's "future-proof" and can handle a large number of use cases because you're not entirely sure how the data will react, but for the most part you'll choose from a position of knowledge.

commented: Narue really understood what I was confused about. +2

A class is a data structure. An object is an instance of a class.

If you ask, "Why use a different container instead of a class?" I would probably say it depends. I would use a different container if I felt it was better suited for the data. Take a look at containers and ask yourself when and why you use each.

If you are talking about C style "structs" then you are talking about structures. Calling them data structures would not be correct. A struct is a data structure-- a record. A struct is like a class but there are no simple methods. However, you can make very "bootleg" methods in a C style struct. Stating a struct has no methods would be incorrect. A bigger distinction between structs and classes is in data encapsulation. A struct can't hide which variables are private or public but a class can. I think that is the biggest distinction.

Example I whipped up (in C)... don't take it too serious:

#include <stdio.h>
#include <stdlib.h>

typedef struct tagInventory Inventory;
typedef struct tagCharacter Character; 

struct tagInventory{
  int slots[100]; 
};              

struct tagCharacter{
  Inventory inventory; 
  char *name;
  int  level;
  int  hp;
  void (*use)(Character *character, int *item);   // our bootleg method
};

void Use(Character *character, int *item){
  if (*item = 666){      // if item used is a potion (666)
    printf("%s used a potion-- healing for 50hp\n", character->name);
    character->hp += 50; // increase character that used the potion's hp
    *item = 0;           // remove item because we used the item
  }
  // add other item conditions below.... 
}

int main(){
 // Initialize the character
 Character *corey = malloc(sizeof(Character)); 
 corey->use = Use; // set use pointer to Use function would usually be in a init function
 corey->inventory.slots[0] = 666; // let's say 666 = potion....
 corey->name = "Corey";
 corey->hp = 50;
 // 
 // Can initialize other characters below...


 // start
 printf("Character's name is %s -- HP is %d, and slot 0 of inventory is item %d\n", corey->name, corey->hp, corey->inventory.slots[0]);
 corey->use(corey, &corey->inventory.slots[0]);
 printf("Character's name is %s -- HP is %d, and slot 0 of inventory is item %d\n", corey->name, corey->hp, corey->inventory.slots[0]);


 return 0;
}

Structs are good at holding sets of data or formatted data. For example, if you want to read a custom file type or a standard file type. You also use structs to create more advanced data structures etc.

Interesting read on Object Oriented ANSI C.

1. Are data structures essentially classes minus methods which can preform actions
on the memebers that are held inside?

A struct can't hide which variables are private or public but a class can. I think that is the biggest distinction.

That's completely untrue. Do not read the post above.


In modern C++, the difference between the actual C++ thing known as a "structure" and the actual C++ thing known as a "class" is that the members of a structure are by default public, and those of a class are default private. There is no other difference. Both can contain methods.


The term "data structure" is a descriptive term just meaning something that is structured and is to do with data. You could write your data on lego bricks and put them together and that would be a "data structure".

>>1. Are data structures essentially classes minus methods which can preform actions
on the members that are held inside?

What you are describing are "C-style structs" or, more commonly, "POD-types" (Plain Old Data types). These are not what people would call data structures (or at least, they are an extremely trivial example of a data structure). Data structures typically refer to language-agnostic concepts of how data can be structured, often for a very specific purpose. Examples include binary search trees, linked-lists, sparse-matrix storage patterns, multi-index search trees, space partitioning, etc. These are complex beasts that generally arrange data in a way that optimizes certain operations (e.g. fast search, fast sort, fast nearest-neighbor query, minimal reallocation and copying, non-blocking concurrency, etc.). A data structure is a theoretical and abstract concept. Classes in C++ are a specific implementation (often, an implementation of a data structure and its related functionality).

>>2. In the case that question 1 is true... Why would I then chose to use a data structure over a class as data being stored is usless without being able to preform actions on that data?

Considering POD-types, sometimes, there is just nothing to do really with the data in a standalone fashion. Some things are just very simple bundles of data nothing more, so why should you needlessly put any member functions if none make sense. Also, POD-types, I mean the C++ Standard definition of them, can lead to some performance increase because the compiler is allowed some additional assumptions on the "simplicity" of the contained data and can optimize-away some copying, assigning and constructing.

>>3. I realize that classes are basically objects, it seems data structures are givin some of that good old terminology that could be misleading. As in we give the structure a name which can be described as a "new type" or a "new object". Which term might be more correct and suitable for data structures?

First of all, classes are NOT basically objects. Classes are types and objects are variables. Just like 'int' is not basically the number '2', classes are not objects. Objects are instances of a class (as in MY dog is an instance of the dog species).

When it comes to terminology, there are always problems, but not in this case. You should be able to differenciate POD-type, Classes and Data Structures. Anyone who is implying that "Data structure" == "struct in C++" is wrong, he's mistaken in his use of terminology (and his understanding of C++, since 'struct' is really only trivially different to 'class' in C++).

>>4. I guess at this point my final thoughts on data structures is that data structures would be a good way to store data that you may not "at the time" know what you may possibly want to do with later on. Which seems a bit silly to me but possibly understandable, kinda sounds like a project so large that you couldn't possibly see or be planning far enough ahead to know why or what you are storing that data for.

Absolutely not. If you ever find yourself doing this on a mid to large size project, stop, and think again. Nobody does things so blindly (well, not many people). There are certainly things that are hard to foresee in a big project, but the data structures (at least the main ones) are amongst the first things that get tackled seriously and get predetermined based on sound analysis of the task to be done, before even programming a single line of code. People don't throw data into a bag and sort it out later (that would be a nightmarish way to code).

Thank you for the great responses. That certainly clears my thinking up on data structures immensly, and yes I was reffering mainly to data structures in the c++ sence. I really like what you said about my misconseption narue I think that was the pitfall I was in when I posted the thread. And mike thank you for pointing out my wrong choice of words about classes, I do realize what your saying and that it is true, mainly the reason of my bad choice of words came from my hazziness about data structures though not from the fact I don't have a fairly decent grasp on classes.

No worries, people often use "class" and "object" interchangeably with or without realizing it is wrong to do so. I believe in rigour when it comes to terminology because getting into the habit of using precise terminology improves understanding for yourself and for others.

commented: I appretiated mike pointing out my error so that I continue to convey what I am saying in a proper fasion. +2

// :NOTE:
"C" structures and "C++" structures are different.
"C++" structures support every features that supported by the "C++"
classes. The only difference is it's default access is "public" in
structures.

"C++" structures support inheritance, polymorphism , virtual functions,
RTTI (run time type checking) and all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.