2

I heard this in a tutorial : "In Object Oriented Programming Languages there are pre-provided classes like strings and arrays in their standard library so that you don't need to define those classes yourselves before defining an object of that type." I have also heard my professor saying "The best data structure for solving this problem is an array". So is it correct regard array as both a class and a data structure as well?

  • 1
    Comparing a class to a data structure makes no sense. A class must represent a data structure, but a data structure are not necessarily represented by a class. There's simply no comparison between them. – Kien Truong Jun 02 '12 at 10:43

3 Answers3

3

In Python, an array is both a class and a data structure.

In C++, an array isn't a class. It's just a contiguous area in memory that contains the element data.

But, you do have std::vector which is a class that wraps up an array. That is a class and a data structure.

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
  • Except for the confusingly named `std::array` in C++11, which could be considered to be a class template. – juanchopanza Jun 02 '12 at 11:12
  • 2
    No, an array is *not* the same thing as a pointer, neither in C nor in C++. See http://c-faq.com/aryptr/ for details, but the short version is that arrays are not pointers but are implicitly converted to pointers almost everywhere. –  Jun 02 '12 at 11:37
  • Even in the link you gave me. Section 6.10 .. It's just pointers with overloaded [] operator. – Yochai Timmer Jun 02 '12 at 13:59
  • @YochaiTimmer Huh? The closest thing to that I can find in Q6.10 is the "second way to think about it", which just states that arrays are second-class citizens. And the whole point of the mention of `[]` is that it's *not* overloaded - in the expression `arr[i]`, the array `arr` is converted into a pointer to the first element of the array, and the `[]` applies to that pointer. IOW there is no way to subscript an array. As A6.10 states (yet again), "Arrays and pointers are the same" is *not* a correct mental model. –  Jun 02 '12 at 14:52
2

"In Object Oriented Programming Languages there are pre-provided classes like strings and arrays in their standard library so that you don't need to define those classes yourselves before defining an object of that type."

Correct.
C++ Standard Library provides a number of template container classes so one can just use those instead of writing own classes. Since these are template based classes one needs to just use them for their own data type, as long as the type satisfy's some basic requirements laid out for the Standard library containers.
You might want to sneak a peek to what those are: Standard Library Containers

"The best data structure for solving this problem is an array"

Incorrect.
The answer to this depends on:

  • What data you want to store &
  • What operations you want to perform on that data

Good Read:
How can I efficiently select a Standard Library container in C++11?

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 1
    OK with the standard library, but, ehm, why do you say the second one is incorrect? Is an array not a data structure, or are you saying the teacher is wrong about how to solve "this problem" (which the OP never mentioned, by the way)? – Mr Lister Jun 02 '12 at 08:57
  • @MrLister: *"The answer to this depends on:..."* or am I missing something? – Alok Save Jun 02 '12 at 08:58
  • 1
    Yes, I mean you don't know what problem the teacher is solving, so you don't know it it's incorrect or not! – Mr Lister Jun 02 '12 at 08:59
  • @MrLister: Not really in a mood to bite on flame baits or pedantic mood swings on a saturday.So I am just going to say it this once, its clear from the OP's Q that his/her teacher suggests using array is the best possible solution to this problem(of various containers being made available) – Alok Save Jun 02 '12 at 09:02
  • @Als I also think that you can't say it is **Incorrect**, you yourself said that depends so saying it is incorrect depends! – jamylak Jun 02 '12 at 09:17
0

An array is a datatype. It may be implemented as a class in some languages but doesn't have to be. C++ arrays are not classes. Python arrays are implemented as classes.

Paddy3118
  • 4,704
  • 27
  • 38