OS/2 eZine - http://www.os2ezine.com
June 16, 2003
Terry is originally from California, moving to Vermont in 1984. His field of expertise is electrical engineering, having worked in electronics from 1969 until 1990. Starting in the Air Force for 6 years, then becoming Plant Engineer at Econco Broadcast Service in Woodland, CA. Upon moving to Vermont, he worked at Joslyn Defense System working in the field of EMP, Electro-Magnetic Pulse. In 1990 he retired from full time electronics to enter, along with his wife Lorraine, the field of helping the handicapped for an agency of the State of Vermont.

Terry's first venture into the computer world was building his first 286 computer running DOS. He started using using OS/2 when version 2.0 was released. He's stayed with OS/2 ever since. At age 54, he now wishes to learn computer programming, a goal he has had for a number of years. Not necessarily as a career move, but for the self-satisfaction and love of OS/2 and eComstation.

If you have a comment about the content of this article, please feel free to vent in the OS/2 eZine discussion forums.

There is also a Printer Friendly version of this page.

Previous Article
Next Article

SciTech SNAP Graphics for OS/2 and Linux

C++ Class Lesson 1

Welcome to our first C++ class for beginners.

As you are all aware, this class consists of a rather unique group of people. We're all programming beginners, for the most part. I am considered the teacher for the simple reason I offered to organize this C++ class. A better term would be the "class organizer" instead of teacher. I know about as much about C++ as most of you, and surely less than some of you. I had just started to teach my self C++ programming when a thread was started on the eCS e-mail list about V.O.I.C.E and OS/2 eZine needing people to write articles for publication. I figured that since I was going to teach myself programming anyway, I'd see if I could help the cause by publishing a series of articles based on a group of people, including me, teaching ourselves C++ programming.

So here we are, actually proceeding with this class. I was rather amazed at the number of people who wanted to join me. Before we start, I have to tell you I'm not a total newbie to programming. I did teach myself Object REXX in 2000. However, I haven't done any programming since then. Unfortunately, most people need to use Classic REXX, so that was a dead end for me. But I did fall in love with the Object model of programming. Over the past few years I have wanted to join an online C++ class, for beginners, that was related to OS/2 and eCS. But none were ever offered that I am aware. The problem with trying to learn a programming language all by yourself is time, and keeping yourself motivated. A class takes care of these issues, at least for me. I now have to devote the time in order to run this class, and being the guy running the show definitely is a big motivator. For you, just having a class to virtually attend will be much better than simply buying a book, sitting at the computer, and then being totally alone.

The reference book of choice for this class is "C++ Primer Plus" 4th Edition, by Stephen Prata. This will give us the C++ basics. I choose this book based on my ease of learning from it. I have others, and online books as well. C++ Primer Plus presented the material in a much better structure without a lot of C sidetrack. But don't fret, you will definitely learn a lot about C because C++ has a foundation based on C. So how am I going to teach you? Why do you need me since I know about as much as you? You already have the book anyway, what can I add? Having some Object knowledge should allow me to help you, at least that's my hope. Also, someone has to keep the class organized. That will be my primary function. Your responsibility for learning will be reading the book. I will provide information here which I feel is extra, or specific, or elaborated for our learning requirements, especially for the absolute beginner to programming. We can all discuss the information and ask questions on the V.O.I.C.E. Discussion Forum - forums.os2voice.org/VOICE/CPlusPlus/

NOTE: Before starting, please download the file containing the corrections for the book at: www.samspublishing.com.

Chapter 1 Getting Started

Objectives for Chapter 1: After reading Chapter 1, you should understand the following concepts:
  • Procedural versus object-oriented programming
  • The object-oriented extensions that C++ added to C
  • The generic programming concepts that C++ templates add to the language

NOTE: From now on, when I say the book, I'm referring to "C++ Primer Plus."

Chapter 1 is a basic history of C and C++, and about the programming process in general. As you can see, you will be learning a great deal of C. So if your desire is to program device drivers, you're still getting the basic foundation in C.

The general concept of Classes and Objects is really quite easy. A Class is like having an instruction manual to build a car. An Object of that car manual (Class) is the car you actually make, that you can sit in and drive. With that ONE car manual (Class), you can build as many of those cars (Objects) as you want. As you begin to actually start programming, sometimes it's easy to forget that a Class isn't an Object. I have on a number of occasions, in Object REXX, figured out that some code wasn't working because I was trying to do something with the Class instead of an Object of the Class. You can't store data or make functions work in a Class (car manual), you can only do things with an Object of the Class.

The main idea in C++ is Classes, and then Objects made from those Classes. So basically, your programming will be to design the Classes you need, defining what something is, such as a car, or a savings account, and what you are allowed to do with these items. Your program will make some Objects. Then you program some code so the Objects can send messages to each other, like people do with e-mail. You are an Object, a person, of the Human Class. If you want to get another Object (person) do something, you send that person an e-mail message. Either the person will do something, such as go to the store, or send an e-mail message back to you, or even to another person.

We, as people, have been interacting with objects since we were born. Ride the bike, turn on the TV, kick the can, lay on the bed. So it only makes sense that programs we create with C++ should behave the same way. Create an Object, then make it do something, or store some information. As programmers, it's like doing a jigsaw puzzle, except we also get to make the pieces any way we like.

When I started to learn Object REXX, I believed it to be hard. As a result, I had set up a big wall in my way. But, as I described above, I finally realized it was easy. I had to tear down that wall, that mental block, and I eventually saw Object REXX for what it was, a bunch of things (Objects) talking to each other. Then it became extremely fun. I started saying, "Wow, I can get this account (Object) to hold this information (data), and then do this with it (a function or method)." (I was creating and accounting app at the time). I was using VisPro REXX to build the interface, the window(s) the user sees on the screen. Each entry field, or list window, or button, just had a little bit of code that would tell some Object to do something.

Piece of cake, right? Ah, but now we have to learn how to layout this C++ code properly so that things happen the way we plan. Time to learn C++.

Creating a Program

NOTE: One issue about the OpenWatcom compiler, your source code, and the code samples in the book. The compiler, OpenWatcom 1.0, doesn't comply with the C++ Standard yet, as of this writing, so there may be times when the code won't compile. I don't know what will or won't compile at this point, so we'll just have to deal with any nits as they comes up. Hopefully newer releases of OpenWatcom will be better as we go, especially the namespace issue. Now if you are using a different compiler that you already have that is already C++ compliant, by all means use it. OpenWatcom is just a suggested compiler that is easy to get, for free, and easy to use. If you have gcc up and running, that's great, I found it to be a total pain to deal with. Remember, I'm a beginner, too.

The text editor to write your source code: OpenWatcom will automatically use the EPM editor that comes with eCS. My personal preference is the EEE (Enhanced EE) editor from PillarSoft.

When you create a source file, use the .cpp extension. For instance, if you are creating a program that will say Hello, your source file name might be hello.cpp. Adding the .cpp tells OpenWatcom to use the C++ compiler.

The OpenWatcom compiler has a short Help tutorial. Please read this for a simple understanding of how and what OpenWatcom does with your source files. If we weren't using the OpenWatcom Integrated Development Environment (IDE), we would be manually doing the things OpenWatcom does for us automatically. Had we used the gcc compiler instead, we'd be doing these things at a command line.

Questions for Chapter 1

Following are questions to make sure you have learned the material. No, there will not be a test. Answers will be on the V.O.I.C.E. Discussion Forum.

True or False

  1. C is a procedural language.
  2. C++ emphasizes the algorithmic side of programming.
  3. C is a superset of C++.
  4. C++ generic functions are type-independent.
  5. The file containing the final runtime product is called the executable code.
  6. The source file is an ASCII text file.
  7. The term IDE is an acronym for Integrated Development Environment.

Short Answers

  1. State the fundamental purpose of the compiler.
  2. Briefly describe what is meant by top-down design.
  3. Briefly describe what is meant by bottom-up design.
  4. State the single main philosophical difference between programming in C and C++.
  5. State the steps involved in creating a C++ program.

Chapter 2 Setting Out to C++

Objectives of Chapter 2: After reading Chapter 2, you should understand the following concepts:
  • A thorough understanding of the structure of a C++ program
  • The use of the main() function
  • The use of comments and why they are used
  • Using simple input and output streams in C++ programs
  • Why header files and preprocessor directives are used
  • Formatting programs for easy understanding and maintenance
  • Statements: what they are and why they are used
  • Variables, declarations, and assignments
  • Ability to understand and predict the results of the sample programs

NOTE: In the myfirst.cpp source file on page 20, remember to remove the using directive, using namespace std; so OpenWatcom can compile it.

NOTE: To run the programs from OpenWatcom and have them remain on the screen, you will need to add the following statement right before the return statements in main():


This keeps the window open until you press ENTER, then it will close.

The main() Function

'myfirst.cpp' is just a basic C++ program. It's the basic beginning structure for all C++ programs. This shows the basic layout for any C++ program to run. It could have been even smaller by leaving out the cout statements, and as a result the #include line could have been removed as well. It would compile and run, but it wouldn't do anything useful. I'm simply making sure you know that the main() function is the absolutely required part of a C++ program. Anything else that's added is to accomplish your program's goal.

The C++ Preprocessor and the iostream File

NOTE: OpenWatcom 1.0 seems to use <iostream> just fine, despite not being able to use the using directive. However, if you feel more comfortable with <iostream.h>, that's fine.

You can view the contents of the header files OpenWatcom uses to create our eCS programs, if you are curious like me. I have my compiler installed in G:\Watcom, so I can see the header files in the directories G:\Watcom\H, and G:\Watcom\H\OS2. Right now they probably look like a foreign language. The contents of iostream.h are added to myfirst.cpp during the compiling process. That's what the #include directive does.


We may not be able to use namespaces presently, but this is a very important topic for C++, so make sure you are aware of it for when we can use it.

Declaration Statements and Variables

Variables and memory are key to our learning. You have to understand variables and memory to be able to program, so I'm going to elaborate a bit here to drive this concept home.

Variable - a name a programmer makes up to hold some data. A variable is assigned a specific address in memory, while your program is running, to hold the data you assign to the variable.

Close your eyes and picture yourself on a street of brand new houses with rows of mailboxes next to the street, one in front of each house. They all have different numbers, or addresses of course. They don't have names on them, just numbers, because no one lives in these houses yet. Now we're going to pretend that somehow we are really in the Twilight Zone (if you can't relate to the Twilight Zone, then think The Matrix) and that all those mailboxes aren't really mailboxes at all, but memory in a huge computer, my computer. Each mailbox, or address, is actually the address of a memory location, they just look like mailboxes to you.

OK, in the book they use fleas as a variable. But since I'm the programmer, and you're in the Twilight Zone looking at my computer's memory, I'm going to call one of the mailboxes, or memory addresses, Norton. That's my address. That's the same as me typing this:

int Norton;

So all of a sudden, as if by magic, one of the mailboxes now has the name Norton on it along with the address. Now I can put something in my mailbox, some integer data because I have specified that my mailbox will hold int type data. So I'll say that in my mailbox is a paper that has the number 54 on it. That's how old I am. I do it like this:

Norton = 54;

I just assigned 54 to my address, my mailbox. If you open my mailbox and look in, all you see is 54. OK, so anytime I use the variable Norton in my program, my mailbox opens and 54 is substituted. So anytime you see Norton in a program, it really means 54. Norton is just the name of an address in memory, and 54 is in it.

A Touch of Class

What do ya know! This whole time you've been using an object, cout, of the ostream class to display some text on your screen. Don't worry about the details here, later chapters will get into it much deeper.


A function is basically having a specialist do something for you. Figures 2.6 & 2.7 in the book are good at showing how a function call works, and the parts of a function call. As an example, let's say you're helping your child with math homework, and you come to a problem you can't solve. You call your neighbor on the phone to solve the math problem for you. You know a little math, but he knows more, he's a math teacher. So you call him, give him some numbers, and he returns the answer, or value, to you. You then continue on helping your child. This is a great object example. You, the parent object, sent a message to the teacher object which performed an action for you.

In the book, for Listing 2.4, the cmath header file, or library if you prefer, is introduced as containing the function definition for sqrt(). When I first started learning to program, I always felt lost. It was assumed that I, a know nothing person, knew things. So how the heck was I supposed to know that a function called sqrt() was sitting around in a file somewhere just waiting for me to use it? After all, to be able to use this stuff it would be nice to know where to easily get this information. This is the sort of thing that makes it hard to learn from a book. With a real teacher in a classroom, you can immediately asks questions and get answers. Just in case you had an uncertain fuzzy spot in your brain, all the functions available to you, no matter what library you work with, can be found in the header files if you want to grind through a bunch of strange looking code. Usually, there are files that describe what functions are available, such as the online documents that came with OpenWatcom. sqrt() can be located in the C++ Library Reference. In the folder that OpenWatcom created on your desktop is another folder called Online Documentation.

User Defined Functions

Creating your own functions will be a key component to creating Classes later. Functions are how you make the objects, that you create from your Classes, do things. When you send a message to an object, you're simply calling a function that's part of the object. That's all there is to it, no black magic.

Questions for Chapter 2

True or False

  1. The C++ language is case sensitive, so cout and Cout are different.
  2. Instructions placed between // and the end of the line are preprocessor directives.
  3. Every C++ program requires an execute() function that must be placed first before any other functions or declarations.
  4. In C++, the use of a semicolon at the end of statement lines is optional.
  5. void progress(int) means that the progress() function takes one argument of type int and returns nothing.
  6. In order to convert programs originally written in C into C++, all comments must be converted from the /* */ format to the // format.
  7. Using the preprocessor in include the iostream file is required to use cout and cin.
  8. cout is a predefined object in C++ that can handle different types of output data and is usually written to the screen.
  9. The C++ program

    using namespace std;
    int main(void) {cout << "Come see me sometime."; cout <<"\n"; return 0;}

    is exactly the same as

    using namespace std;
    int main(void)
    cout << "Come see me sometime.";
    cout <<"\n";
    return 0;

  10. All variables must be declared before use in C++ unless a value is immediately assigned to the variable.
  11. Classes are a form of data type in C++ and must be defined by the programmer.
  12. Objects are a type of function used to represent tangible things to the program.
  13. All user-defined functions must be prototyped before being called the first time.
  14. A semicolon is required at the end of a function prototype, but must not be placed at the end of a function.
  15. All functions, both user-defined and standard library functions, must return a value unless they are declared as a type return.

Short Answers

  1. Explain what each of the following does in a C++ program: cin, cout, ";", ">>", "<<", #include, "//", int, main(), "\n", return, "{", and "}".
  2. How is the return value of a function used? Why would some functions have a return value and some not?
  3. What kind of information is included in header files?

Chapter 3 Dealing With Data

Objectives of Chapter 3: After reading Chapter 3, you should understand the following concepts:
  • Uses and naming of variables in C++ programs
  • The two fundamental classes of variables: integers and floating-point
  • The types of variables available in each class, and when each should be used
  • The five basic C++ arithmetic operators and how they act with different data types
  • The changes and extensions that C++ brought to data types from C
  • Numeric constants and their place in a C++ program
  • Symbolic constants: the new and improved const replaces #define

Variable Naming Rules

You've already used variables in the sample programs. I also gave you a Twilight Zone view of how a variable is used in Chapter 2. Now you have to learn the rules for their use. Here's a quick outline:

  • They can be alphabetic characters, digits, and the underscore
  • First character can NOT be a digit
  • Hyphen NOT allowed
  • Case sensitive
  • Can NOT be a C++ keyword
  • Any length

Integer Types

I don't know about you, but when I think of an integer, the first thing that comes to mind is a number. I even looked it up in the dictionary, and it mainly referred to numbers or digits. Yet in the programming world, characters are in this integer type. They're called an integer type because a char (a single character) is stored in memory as an integer. The computer knows it's not actually a number simply because any variable that is declared as a char type will be a character, not a numeric integer. Then to really make it interesting, we're told we can do char arithmetic.

In the morechar.cpp example, you'll notice in the statement char c = 'M'; that character constants are surrounded with single quote marks. If you don't include the quote marks the compiler will see M as a variable, not the letter M. You'd get an error that you have an undeclared variable M.

NOTE: The limits.cpp source file will not compile with <climits> because there is no such header file. There is a limits.h file. So either use <limits.h> or <limits>.

A Member Function: cout.put()

Here is the first good example of sending a message to an object so that the object will do something. Pretty easy, isn't it? If you recall, cout is an object of the ostream Class. put() is a function that's part of the cout object. So you simply name the object, cout, and send a message to cout to use it's put() function. The function will output something to the screen. That's it, that's how objects work!! Very simple concept. REMEMBER THIS! Don't make this concept about object-oriented programming any harder. This is the most important thing to remember. Are you remembering? OBJECTS DO THINGS BY BEING SENT MESSAGES!!

Now go have a drink to celebrate, or have an "I GET IT" party. Tell your friends you've seen the light. Life is wonderful! For an OO programmer it doesn't get any better than this. Now go find a procedural programmer and just stick out your tongue at him. He's in the dark, poor lost soul. By the way, if I haven't mentioned it, I think OO programming is neat stuff.

OK, ok, we're only in Chapter 3. As we get into the Class stuff in later chapters, remembering this all important revelation will make creating Classes so much simpler. But for now, it's onward with learning all that necessary stuff of C++.

Integer review

Integers are the first major class of numeric data types in C++. They are:

  • short (short int)
  • int
  • long (long int)

They can be signed, meaning they can have positive or negative values, or they can be unsigned, meaning they will always be positive. If you don't specify, the default is signed.

Floating-Point Numbers

Floating-point numbers are the second major class of numeric data types in C++. They are:

  • float
  • double
  • long double

They are usually decimal numbers, such as 1236.9501, or e notation, such as 1.2369501e3.

NOTE: Some examples will not compile as written. Use ios::float and ios::floatfield instead of ios_base::float and ios_base::floatfield.

Questions for Chapter 3

True or False

  1. C++ requires that all variables have the first letter capitalized.
  2. A char data type is used in C++ strings.
  3. It's critical to make sure variables don't overflow since C++ doesn't check.
  4. char has a range of -127 to 128 by default.
  5. 5.9875e17 must be stored as a floating-point number.
  6. The statement cout << 45 / 10; will display 4.

OK folks, that's it for this issue. Answers to my questions will be on the C++ forum under Lesson 1.

As you can see, my intent is not to just repeat what's in the book. There's no point in that, you can all read. I stress some points to help prevent fuzzy, uncertain spots from forming in your brain, which is what usually happens to me when I have questions that just don't seem to be answered quite right. But then again, maybe I'm just odd.

So go to the e-mail list and correct my mistakes. After all, maybe I thought I learned something, but I have it wrong. I'm not the answer guy, we all need to help each other. If someone has a question, try to answer if you know, or think you know, the answer. We'll all learn by contributing.

I've covered three chapters in this lesson, they're relatively easy. We will get into more intense subjects next month, and I plan to pick up the pace. I don't believe we want to stretch this this class out too long. The longer we take, the easier it is to get bored. A month between lessons is quite a long time, especially when you want to learn faster. However, it also gives everyone enough time to get questions answered, and make sure no one is left behind for lack of understanding. Next month there will be two lessons covering three chapters.

Previous Article
Next Article

Copyright (C) 2003. All Rights Reserved.