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:

#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":

#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

ps - this is a super-nice forum, the best I have seen!

Recommended Answers

All 5 Replies

Tokens are just a group of characters that have been delimited in some way. So for instance you might talk about tokenising a string of comma separated values into tokens. You would mean extracting the strings appearing between the commas as separate values.

Tokenising is often, but not necessarily, done 1 token at a time.

Your first listing is better, however both of them have the class data members as public. In the real world you tend to keep all your class data either protected or private to implement the OO principle of encapsulation.

As such the first listing is better because it alread has some of the required work to make the data private done (i.e. a constructor that allows the object to be constructed with default values. Properly developed I would expect this class to end up as

class person 
{
public:
    person(const string& name_str, int age_int)
        :name (name_str), age(age_int) { }

    void setName(const string& name_str){ name = name_str; }
    string getName(){ return name; }

    void setAge(int age_int){ age = age_int; }
    int getAge(){ return age; }

private:
    string name;
    int age;
};

Actually you may still want to add a default constructor, copy constructor and assignment operator to this.

A "token" is simply a unit of information that has meaning. For example, in C++ if you want to output to the console, you use the keyword cout. The keyword cout is a "token" that the compiler detects to tell it that you want to use the standard output stream/console.

When you define a custom class (i.e. "person"), the word "person" becomes meaningful to your program and the compiler. Now that it has meaning, it can be used as a "token" (or "keyword") in your program's statements.

Question 1):
You have defined a "person" class. When you create a variable that has data type "person", you are said to be "instantiating" the class. When you "instantiate a class", you create an object of the class' type. Example using your person class:

class person {
 public:
   std::string myName;  //a "member variable", an instance of (object of type) std::string, called myName
   int myAge;  //a "member variable", an instance of (object of type) int, called myAge

   person();  //prototype for default constructor, a "member function"
   person(std::string, int); //prototype for a specified constructor, also a "member function"
   int getAge();  //another prototype, a "member function" called "getAge"
   /*...etc...*/
};

int main() {
  person mother;  //an instance of "person", a person object called "mother"
  //this declaration calls person::person()

  person daughter("Angela", 22); //an instance of "person", a person object called "daughter"
  //this declaration calls person::person(std::string, int)

  /*...etc...*/
}

Question 2):
The second version, while functional, is an over-simplification of what classes are, how they work, and what they are capable of. Under normal circumstances, your class definition would be more like the first, with several other added functions and variables.

Thank you both for the replies, I certainly have a lot to learn. C++ seems a quantum leap from Pascal or the C that I used to tinker with.

Fbody, is that "F-body" as in the GM "F-body" class of cars? if so, what is your favorite?

LOL! I've been using "Fbody" as a handle for years. I think you're the first to finally make that association! (or at least vocalize it...)

My dad has a red '77 TransAm (400 cid) coupe, and I have a black-on-red '97 TransAm WS6 convertible.

LOL! I've been using "Fbody" as a handle for years. I think you're the first to finally make that association! (or at least vocalize it...)

My dad has a red '77 TransAm (400 cid) coupe, and I have a black-on-red '97 TransAm WS6 convertible.

That's cool... I bought a '79 Z28 in 1979 that had 3000 miles on it. I later wrecked it :( fixed it with all brand-new GM parts, painted it and sold it to a friend who soon turned it into a pro-streeter with an Alston tube frame. He still owns it. ...and the paint still looks great!!

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.