Hello World C++ with Objects

Ok… this is a “Hello World” program in C++ using objects. It illustrates a number of aspects of object oriented programming in C++ allowing you to get a quickstart into C++ programming. First there is the #include directives which tells C++ that the program uses two libraries, iostream and string (actually they specify the header files that define the libraries to be used). “iostream” provides input/output to streams, in this case we are sending a stream to the console. “string” is a string type library that provide a string datatype that is not native to the base C++, this allows us to manipulate strings in an easy manner.

#include <iostream.h>
#include <string.h>
using namespace std;

class oHello
{
    string hstr;
public:
    oHello();
    ~oHello();
    void print();
};

oHello::oHello()
{
    hstr = "Hello World\n";
}

oHello::~oHello()
{
}

void oHello::print()
{
    cout << hstr;
}

>int main()
{
    oHello myObject;
    myObject.print();
    return 0;
}

Class oHello is defined as having three methods, oHello method is the constructor that is executed when the object is created, ~oHello is the destructor that is executed when the program destroys the object (in this case, when the program terminates), lastly there is print which sends hstr to the output stream. These are declared as public so that they can be accessed from other modules in the program that are not members of the oHello object class. “hstr” is private to the class and only the class methods can access this data. It is of type string, the object string that was defined in the string class library.

The oHello method does one thing, assign hstr the value of “Hello World\n”. “\n” means add a carriage return to the end of the string. Now, when any member methods access hstr it would contain the value “Hello World\n”.

The print method contains the instruction cout << hstr, this calls the hstr object using the << method which directs it to send the value it contains to the cout stream class, which as stated earlier sends the data to the output stream.

Now that all the objects are defined… we can use them in the main() function which is automatically executed when the program is run. First we create an instance of the oHello object, called myObject. Next we call the print method of the myObject object, which prints “Hello World\n” to the output stream (in this case the console). Lastly since main() is a function it must return something before terminating, so we return the value zero.

Hello World ooREXX

Ok, as my previous post showed how to write a routine oriented hello world program in gforth, I now explain a hello world program using objects in object oriented rexx (ooRexx).

#!/usr/bin/rexx
/*
    Object Oriented Hello World in REXX
*/

myobject = .hello~new
myobject~print
exit

::class hello
 ::method init
 expose hellostring
    hellostring = "Hello World"
 return
 ::method print
 expose hellostring
    say hellostring
 return;

Like all rexx programs, the main() like section is the beginning lines of the program. The object definitions begin after the main instruction sequence. In this case we have one object called “hello”

::class hello

It contains two methods, a constructor and general purpose method named “print”. Both objects share a variable common to the object called “hellostring”, which contains the string we want the object to print. It is initially set by the constructor to the value “Hello World”. The print method does only one thing, to print the value of the variable “hellostring” to the console.

So, here we have an illustration of using objects, methods, constructors and internal data passing between objects in a rexx program. (Note, I am not going into the details of what is object oriented programming, as if you reading this you should have already studied what is object oriented programming).

The beginning part of the program does two things, create an object named “myobject” of the type “hello” and initialize it. The second instruction calls the “print” method.

Pretty simple.

Hello World Gforth (better example)

One of the issues of most of the information out there with respect to the forth language (and this includes books) is the lack of a clear illustration of how to write a routine and call it in the program in a easy to understand manner. They show how to write a routine and call it from another routine but no one tells you where your main program should be to call all the functions… do you create an main() like routine, do you put the instructions at the beginning and the routines after, or do you define the routines and call them after…. most of the forth manuals seem to skip this part in clear english. I am not a anything like an experienced forth programmer, but like all experimental programmers, I am interested in all programming languages, including forth… while most of what I might do is in other languages, I would like to say that I know something of forth, but most of the information out there leaves a lot to be desired when you do not “know” forth.

Anyway this is my illustration of routine calls in a forth program. Ye old “Hello World” program. I believe all first “Hello World” program in any programming language should illustrate how routines (subroutines or functions) call called.

: hello
." Hello World" cr ;

hello
bye

So what does this program do? First it defines a routine called “hello” that does only one thing, print the text string “Hello World” with a carriage return.

With respect to forth, the routine must be defined before being used, so it comes before any instruction that calls the routine. This is why the rest of the program follows the routine. BTW the routine definition is terminated by the semi-colon. (Note all colons and semi-colons are spaced away from any other element, as forth needs a space when parsing to determine it sytnax).

There is no main() like construct in forth, so the instructions are listed right after all routines are defined. We therefore have two instructions: “hello” and “bye”. “hello” calls the hello routine and executes it before passing it off to the next instruction “bye” which in gforth tells the forth environment to terminate, exiting the program.

There you have it, a gforth “hello world” program that illustrates the use of routines in the source program.

Batch downloading with REXX and WGET

Recent news, I had to reinstall SUSE linux on my 64-bit PC. So this means I have to get my tools that I use often, including REXX scripts. Previously I used a REXX script for reading a text file and downloading a series of files based on a filter, such as files x001.jpg to x030.jpg. The script did it effectively but was not flexible. So I decided to make the next generation version of the script. One that can handle more complex download sequences without modifying the code.

I took the modular approach, separating the program into two parts, the core download processing script and a higher level simple file filtering script that offers the ease of use as my old script.

The new core script is flexible but uses a more complex syntax that is very linux like, taking s syntax as follows:

dlfiles –start n1 –stop n2 –lz n3 –dest s1 –src s2

where….
n1 says where to start its numeric counter for downloading
n2 says where to stop its numeric counter for downloading
n3 says to use leading zeros and how many to use… eg… 001 002 003 … 099
s1 says to what directory to store the downloads
s2 is the source url of the downloads where the counting section is replaced with
a symbolic “~~”

Thus to download files x0001.txt to x0040.txt from a server the command to use would be

dlfiles –start 1 –stop 40 –lz 2 –src http://server/x00~~.txt

The second script called “getem” works by reading a specified text file and parsing the contents and then calling dlfiles with the correct parameters. Thus the file would contain records such as…
Docs 1, http://server1/doc~~.doc
Docs 2. http://server2/text~~.odt

It uses the dlfiles default settings… starting with 1 and ending with 30, no leading zeros but tells it to store the files in the Docs 1 and Docs 2 folders, if these folders do not exist, they are created by the download program WGET, of which it is dependent for downloading.

The main error that I got from this script was the limited parsing ability of REXX and its inability to handle parameters with spaces even if delimited by quotation marks, which resulted in that I had to create my own parsing code.

The end result is the following two programs that would be listed. Of course, the usual copyright notices require giving me credit if any of it is used for your legal purposes. If illegal, please do not include me in your illicit activities.

Btw. Why do I use REXX instead of sh or perl or python? Simple, I am an old OS/2 user, and a lot of the REXX programming I learned then is still my mainstay, as a result I install REXX on any computer I am using, including Windows and Linux machines. I currently use and support Open Object Rexx, as this is the direct IBM descendant as that was used on the OS/2 PCs and the OS/400 minicomputers.


— dlfiles —

#!/usr/bin/rexx
/*
   dlfiles (c) 2007 Dion Mohammed
*/

parse arg params
DEBUG = 0
v. = ""
call proc_args
s = 'wget --tries=10 --continue --timeout=10 --directory-prefix="'v.dest'/"'
c = v.start

do until c > v.stop
    parse value v.src with v.prefix "~~"v.postfix
    if v.lzeros\=0 then
    do
        fspec=v.prefix""right(c, v.lzeros,"0")""v.postfix
    end
    else
    do
        fspec=v.prefix""format(c)""v.postfix
    end
    dncmd=s' "'fspec'"'
    IF DEBUG=1 then
    do
       say "DLFILES ==> "dncmd
       say "V.SRC: "v.src
       say "PREFIX: "v.prefix
       say "POSTFIX:"v.postfix
    end
    else
       dncmd
    c = c + 1
end
exit

proc_args:
procedure expose params v.
    SE = "Syntax Error: "
    x=1
    i=1
    v.dest = "."
    v.start=1
    v.stop=30
    v.lzeros=0
    v.src=""

    do while x\=0
       y=pos("--", params, x)
       x=pos("--", params, y+1)
       if x=0 then
       do
           w.i = substr(params,y)
       end
       else
       do
           w.i = substr(params,y,(x-y))
       end
       i = i + 1
   end
   i = i - 1
   do c=1 to i
       parse value w.c with operand data
       select
           when operand="--start" then v.start=data
           when operand="--stop" then v.stop=data
           when operand="--lz" then v.lzeros=data
           when operand="--dest" then v.dest=data
          when operand="--src" then v.src=data
       otherwise
       end
   end

/* Validifying */
   if v.src="" then
   do
       say SE "Error processing Source"
       exit
   end
   if v.dest="" then
   do
       say SE "Error processing Destination"
       exit
   end
   if datatype(v.start)\="NUM" then
   do
       say SE "Start value must be numeric"
       exit
   end
   if datatype(v.stop)\="NUM" then
   do
       say SE "Stop value must be numeric"
       exit
   end
   if datatype(v.lzeros)\="NUM" then
   do
       say SE "Leading zeros must be numeric"
       exit
   end
   return

 


-- getem --

#!/usr/bin/rexx
/*
 GETEM (c) 2007 Dion Mohammed
 process download index file
*/
parse arg indexfile .
do while lines(indexfile)>0<
 currline = linein(indexfile)
 parse value currline with storepath", "fileurl
 cmdline = 'dlfiles --dest "'storepath'" --src "'fileurl'"'
 say "GETEM ==> "cmdline
 cmdline
end
exit

These are released for free use, with respect to the original owner of the source code. You are free to use and modify for your use, with acknowledgment given to me.