C++ Notes for Java Programmers
This document is intended for Java programmers who are learning C++
Corrections made to this document March 11 are highlighted in yellow. [ link to corrections]

  1. Class members
  2. Source files: C++ program and class files normally consist of 2 separate source files
  3. public, private, and protected keywords used within a class definition block (curly braces)
  4. Object name variables and object pointer variables
  5. Constructing objects (instance of a class) and using object variables or object pointer variables
  6. Passing objects or references to objects as function parameters
  7. Returning objects or references to objects as function return values
    Note: a function return value is passed "by value", i.e., it is a copy of the return value defined within the function block.
  8. Arrays of objects and arrays of pointers to objects
  9. Global and static variables
  10. static class members and static member functions
  11. Example specification file: Thing.h

    #ifndef _THING_H_  // Use to avoid including the same file more than once.
    #define _THING_H_  // Note: #ifndef matches with #endif at the end of file.
                       // The first time this file name appears in a #include
                       // statement _THING_H_ is undefined, so #ifndef is true,
                       // and the C++ preprocessor includes all statements
                       // between the #ifndef and #endif. #define causes
                       // _THING_H_ to become defined. Thus, any subsequent
                       // executions of #include Thing.h will skip the statements
                       // between #ifndef and #endif because_THING_H_ will have
                       // been previously #define'd.

    #include <glut.h>

    class Thing
    {
        public:        // member function prototypes
                       // Normal coding style convention is that public function
                       // member declarations are placed at the start of a header
                       // file.

          Thing();         // default constructor
          Thing(int x);    // another constructor

          void setSize(int size);
          void setColor(float r, float g, float b);
          void draw();

        private:       // data member declarations
                       // Normal coding style convention is that data member
                       // declarations are placed at the end of a header file.

          int size:
          float color[3];
    };

    #endif _THING_H_
     
     

  12. Example implementation file: Thing.cpp

    #include "Thing.h"


    Thing::Thing()
    {
          // code for default constructor
    }

    Thing::Thing(int x)
    {
          // code for another constructor
    }

    void Thing::setSize(int s)
    {
          // code for setSize
    }

    void Thing::setColor(float r, float g, float b)
    {
          // code for setColor
    }

    void Thing::draw()
    {
          // code for draw
    }

  13. Use of static class members and static member functions with GLUT callback functions