2

I am starting data structures in C++ and while reading, I came up to the following snippet,

template <class Node_entry>
struct Node {
// data members
Node_entry entry;
Node<Node_entry> *next;
// constructors
Node( );
Node(Node_entry, Node<Node_entry> *link = NULL);
};

Can anyone please elaborate why the author choose a structure and not a class for the implementation of the singly linked list? Thanks.

  • possible duplicate of [C/C++ Struct vs Class](http://stackoverflow.com/questions/2750270/c-c-struct-vs-class) – Alexandre C. Aug 12 '11 at 14:11
  • 3
    Probably because s/he wanted to teach algorithms and data structures, and didn't want to distract with OO design issues. – john Aug 12 '11 at 14:12
  • @Alexandre C. I think it's not a duplicate because this question is specifically about linked lists. –  Aug 12 '11 at 14:13
  • @WTP: What ? This question is specifically about class vs struct. Linked lists are not part of any sensible answer to this question. – Alexandre C. Aug 12 '11 at 14:14
  • @Alexandre: I know the difference between structure and class. What I wanted to know is that why has the author preferred structure here in case of linked list. Thanks –  Aug 12 '11 at 14:23
  • If you know the difference between structure and class, I don't understand why you accepted the answer that only explains this difference. If what you really want to know is "why does the author chose to make all members public", then I think @john's comment is the closest to an answer (to which I would add "brevity"). – Luc Touraille Aug 12 '11 at 15:26
  • @John please post it as a answer. –  Aug 21 '11 at 09:50
  • @JustAnotherProgrammer: Sure, I could do with some reputation having just lost some due to not taking this site seriously enough. Thanks. – john Aug 21 '11 at 09:53

7 Answers7

5

He wanted the default access to be public - that's the only difference between classes and structures in C++

Nemanja Trifunovic
  • 24,346
  • 3
  • 50
  • 88
3

Probably because in a struct all members are public by default. In a class they are private by default.

If the author choose a class, he would've written this:

template <class Node_entry>
class Node {
public: // note this! <------------
// data members
Node_entry entry;
Node<Node_entry> *next;
// constructors
Node( );
Node(Node_entry, Node<Node_entry> *link = NULL);
};
1

struct and class both define classes, and can have methods, members, constructors, etc.

The only difference is that structure members are public by default, and that structures inherit publicly by default.

See this question.

Community
  • 1
  • 1
Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
1

By default members of a struct are public i.e. visible to everything outside them whereas members of a class are by default private. This is the only difference between the two keywords (I believe).

Dan
  • 12,857
  • 7
  • 40
  • 57
1

From algorithms and data structures point of view, there is no difference between doing it using structure or classes! Books that are talking about algorithms or data structure don't care about OOP, for example, in Introduction to Algorithms they are using pascal and sometimes pseudo code. The important thing is to deliver the idea. The author may choose to use structures because he doesn't want to bother the reader about OOP principles and best practices, he doesn't want you to say hey why he defined this field public and not private with setters and getters. by this way you are getting far from data structure and algorithms.

K''
  • 5,020
  • 7
  • 33
  • 43
0

If you are doing it from algorithms and data structures point of view anything is fine but when it comes to production members of a struct are public so visible to everything outside but classes are by default private

0

Probably because s/he wanted to teach algorithms and data structures, and didn't want to distract with OO design issues.

john
  • 85,011
  • 4
  • 57
  • 81