Terminology for some user-defined data types

Hello All, this is my first post here, I was a novice Pascal and C programmer back in the DOS days, now I am taking a C++ class based on "Programming - Principles and Practices Using C++" by Bjarne Stroustrup. In the book he gives an example in which he is explaining about "Tokens"... I have pared that down to the bare bones and I am comparing it to an example from Wikipedia. I have made these two programs functionally equivalent.

I have two main questions: 1) What do you call these? They are not really "Tokens", that is just a term that Mr. Stroustrup came up with as a teaching concept according to my instructor.

This first program is a modified version of one of the examples from the book:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;
class person 
{
public:
    string name;
    int age;
    person(string name_str, int age_int)
        :name (name_str), age(age_int) { }
};
int main()
{
    char ch;
    person t1("Calvin", 30);
    person t2("Hobbes", 20);
    cout << t1.name << ": " << t1.age << endl;
    cout << t2.name << ": " << t2.age << endl;
 
    cin >> ch;
    return 0;
}

This one is an example taken from Wikipedia about "Classes":

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;
class person
{
public:
  string name;
  int age;
};
int main ()
{
  char ch;
  person a, b;
  a.name = "Calvin";
  b.name = "Hobbes";
  a.age = 30;
  b.age = 20;
  cout << a.name << ": " << a.age << endl;
  cout << b.name << ": " << b.age << endl;
  cin >> ch;
  return 0;
}

2) My second question is: when or why would we want to use each? It seems the first one, the "Token" one (for lack of a better term) is more work than the one that is from the "Classes" example.

Again, I would like to know how to more accurately refer to these as well as when and why to use each.

Thank you so much in advance.
Gary
A "token" is computer terms is the smallest parsable entity. The keyword "class" is a token, as is the
constant 3.14, as is the string literal "Hello World" and the variable name "foo", and the +, -, *, /, and %
operators, etc. etc.

Second question. I would rarely, if ever, use the second one. The amount of work each one is is not just
defined by the number of keystrokes needed to type it; it's also about how long it takes to debug it
when it doesn't work, how easy it is to extend in the future (I want to add height and weight to the
person class), and how long it takes to remember the details about the object.

I would therefore call the first one "right" and the second one "stupid". (There isn't really a name to
give to them; they are just different ways of writing the same program).
I'm familiar with that book, and what Stroustrup uses is certainly a token in the context of his calculator example. He uses that example to teach parsing. In that context, every parsable unit is composed of a (type, value) pair, although, if I remember correctly, the value is not always used.
Topic archived. No new replies allowed.