Monday, April 29, 2002

MFC Approach





Once you’ve completed your initial application design, you’ll typically perform the following tasks to develop the application with the development environment and the Microsoft Foundation Class Library (MFC):





  • Use AppWizard to create a set of C++ starter files and associated Windows resources — a starter application that you can build and run immediately.





  • Use the resource editors to construct the objects that make up the user interface, such as menus and dialog boxes.





  • Use elements of the development environment to generate and edit application-specific code:




    • The text editor





    • ClassView





    • ClassWizard





    • WizardBar








  • Build, browse, test, and debug your project files — then add more code.






The steps tend to be iterative: You’ll go back and forth between editing the user interface and writing code all through the development process. You can also do the steps in a different order, depending on your working style.

Wednesday, April 10, 2002

Chapter 1. - Basic C Programming





How to Write a C Program



To use a class you include the header file.

The iostream is the standard library.

Use

#include 
to do so.

The main() return type is int and 0 returned indicates success.

using namespace std
encapsulates all the library to prevent

same name conflicts.

A class is a user-defined data type.

A class is typically divided into two parts: header file and

program text file.

A predefined class object cout outputs to terminal with "<<"

operator.

Use
#include 
to include the String header class

file.

cin >> objName
takes input and puts it into objName

object (variable).

Character literals use single quotes like
'\n', '\t'
for

newline and tab indications.





Defining and Initializing a Data Object





To define an object, name it and give it a data type. (note* author uses word "data object" in place of word variable always.)

Initialize a data object with



int objName = 0;

OR

int objName( 0 );

.

C++ 4 basic data types are:

Boolean, Character, Interger, and Floating Point.

A template class allows us to define a class without declaring data

types. Three members of class complex are 1.Float 2. Double. 3.

Long Double and done like:



#include



Boolean and Constants declared respectively like:



bool go_for_it = true;

const int max_tries = 3;

const double pi = 3.14159;







Writing Expressions





int numbers are truncated with NO rounding off.

The % operator has always confused me. The author didn't

help here either. After wrestling with it the best way I've found to

understand it is by reading the statement with "divided by . . . the

remainder equals . . ."
. An example: 5 % 2 = 1 reads "five divided

by two leaves a remainder of one." Linguistic read outs tend to explain

meanings... why authors don't utilize it more I'll never

know.


cnt = cnt + 1


is the same as

cnt++


...both add one to cnt

And



cnt = cnt + 2



is the same as

cnt += 2


...both add two to the current object value of cnt

There is a prefix and a postfix version of this. ++cnt adds an

increment of one BEFORE the object is evaluated. cnt++ does so

AFTER object is evalutaed.



Object names (variables) are case sensitive, so to test for like a

letter "n" input, use:

if ( objVar == 'N' || objVar == 'n' ) 

//proceed on...



A conditional expression is considered as evaluating to FALSE if the

value returned is 0. This means that arithmathic operations can be used

as on/off boolean flags or switches. The parallel to this is an boolean

object can evaluate to a zero (0) or a non-zero (anynumber). These are

used for counting like in loops, conditional branching and recursions.

So, if X % 2 = anything, then it equals TRUE. Read this like:

"Is there a remainder?"





&& // logical comparison check for AND; like a question

|| // logical comparison check for OR; again, like a question

! // logical NOT



== // logical test of equivalence

= // this is an ASSIGNMENT of a VALUE



    ORDER OF PRECEDENCE

  1. logical NOT

  2. arithmatic (*,/,%)

  3. arithmatic (+, -)

  4. relational (<, >, <=, >=)

  5. relational (==, !=)

  6. logical AND

  7. logical OR

  8. assignment





    LITERALS (note* escape character works for only the 1 next

    character)

  1. '\n' newline

  2. '\t' tab

  3. '\0' null

  4. '\'' single quote

  5. '\"' double quote

  6. '\\' backslash





Remember - 0 equals FALSE and FALSE equals 0



Writing Conditional and Loop Statements



Most of this is standard loop syntax and logic. The WHILE loop and

FOR loop are the primary two. There also is the SWITCH condition check

that is equivalent to if-else-if clause, like:



switch (objPerson)

{

case "Fred":

cout << "Hi Fred.\n";

break;

case "Joe":

cout << "Hey Joe!\n";

break;

case "Bob":

cout << "Whoa Bob!!\n";

break;

default:

cout << "What is your name?\n";

break;

}





break keyword serves to kick you out of the testing.

continue keyword in a loop kicks you out of that iteration ONLY.

The looping will continue till a break or a condition is

satisfied.



How to Use Arrays and Vectors



Two types of arrays. Built-in Array (specify type of element, a name, and a dimensiion.) and Vector (a class object where the vector header file must be included).



Vectors are included as a class. They do not support initialization

from a list. Author confuses me somewhat on this, but shows to use the

normal built-in array to then initialize a vector class.



Pointers Allow for Flexibility



Author explains this poorly. I went to the Cplusplus dot com tutorial for a good explanation.



In simple terms: the ampersand before a variable name means this is

the ADDRESS that holds the variable (object) value, NOT the value

itself like normal. This is called Dereference. The asterisk

before a variable makes that variable (object) a POINTER type object,

and NOT a regular variable. It instead points to another

variable instead of actually being a normal variable itself.



The confusion comes from two aspects.

  1. Pointer objects are often

    used to point to Addresses of other objects. Thus both of these operants

    are usually but not neccessarily used together.
  2. Pointer

    objects can be directly created by using the asterisk also, but it just

    happens to be an asterisk to do this. It's not the same meaning as when

    attached to the variable name. It is an asterisk used in a different

    way.

Examples:



At this point, and following with the same example initiated above where:





andy = 25;

ted = &andy;



you should be able to clearly see that all the following expressions are true:

andy == 25

&andy == 1776

ted == 1776

*ted == 25





Writing and Reading Files



#include 
...is required. To open a file for output we define an ostream class object and pass it the name of the file to open.



// seq_data.txt is opened in the output mode

ofstream outfile( "seq_data.txt" );



Confirmation that the file is properly opened and can be tested by the "true" value of the class object.




if ( ! outfile) // if evaluates to false, file could not be opened

cerr << "Oops, unable to save session data!\n";



else

// ok, outfile is open, write the data

outfile << usr_name << ' '

<< num_tries << ' '

<< num_right << end1;



"end1" is a predefined manipulater from the iostream library that

inserts a newline character and flushes the output buffer.... the "cerr"

prints straight to the screen - NOT buffered. There is also an APPEND

mode for i/o stuff and also other manipulaters in the iostream such as,

"hex", "oct", and "setprecision(n)".



----- END -----

note*



This ends my notes on the first chapter of "Essential C++". Due to

the density of some of the material, I may go through the Cplusplus

online tutorial before going further in this book. But I still like the

outline here and the looks of the up and coming chapters so I'll be back

for "chapter 2 - Procedural Programming" soon.

Compiling from command line with Microsoft Visual C

In summary, if you want to compile a unique C source code file into an executable like for example test.cpp and you have already executed VCVARS32.BAT it would be enough to write at the command line:

CL test.cpp


that would generate the file test.exe.

...trying again to explain simply.... CL is actually a file called CL.EXE that comes with Visual Studio.... all you are trying to do with the VCVAR32.BAT file (it also comes with the VCStudio and is in BIN folder) is to set the computer's environmental variables so you can execute that CL.EXE compiler program from the root (e.g. C:\).... all this does is saves you from having to type in the whole path everytime you want to run the compiler (ala C:\Program Files\Microsoft Visual Studio\VC98..blah blah whatever...).... get it?



Compiling from command line with Microsoft Visual C







Visual C supports the possibility to compile 32 bits programs with no need to use the integrated development environment, simply from the comman line.

For that, first of all we need that a series of system environment variables be suitably defined. More concretely they are the variables %INCLUDE% and %LIB% that define the directories where the include and library files are, as well as it is also recommendable to add the directories where the executable file that we need to compile are to the path.

Luckily, during the initial installation of Visual C a BAT file called VCVARS32.bat would have been automatically created defining all these environment variables for us. This BAT file is located at the subdirectory BIN that hangs from the directory where you have installed Visaul C , that by default would be something similar to:

C:\Program Files\Microsoft Visual Studio\VC98\BIN 


Maybe this file is already being automatically executed whenever our operating system starts up, to know that we can just try this test: from the command line type:

set INCLUDE


if the system shows a series of paths separated by commas the file has already been executed. If a message telling that the environment variable has not yet been defined (is not defined) is shown you will have to execute the file ,b>VCVARS32.bat manually.

Tuesday, April 09, 2002

Structure of This Book.



A Note on the Source Code.

Acknowledgments.

Where to Find More Information.

Typographical Conventions.

1. Basic C Programming.



How to Write a C Program.

Defining and Initializing a Data Object.

Writing Expressions.

Writing Conditional and Loop Statements.

How to Use Arrays and Vectors.

Pointers Allow for Flexibility.

Writing and Reading Files.





2. Procedural Programming.



How to Write a Function.

Invoking a Function.

Providing Default Parameter Values.

Using Local Static Objects.

Declaring a Function Inline.

Providing Overloaded Functions.

Defining and Using Template Functions.

Pointers to Functions Add Flexibility.

Setting Up a Header File.





3. Generic Programming.



The Arithmetic of Pointers.

Making Sense of Iterators.

Operations Common to All Containers.

Using the Sequential Containers.

Using the Generic Algorithms.

How to Design a Generic Algorithm.

Using a Map.

Using a Set.

How to Use Iterator Inserters.

Using the iostream Iterators.





4. Object-Based Programming.



How to Implement a Class.

What are Class Constructors and the Class Destructor?

What are mutable and const?

What is the this Pointer?

Static Class Members.

Building an Iterator Class.

Collaboration Sometimes Requires Friendship.

Implementing a Copy Assignment Operator.

Implementing a Function Object.

The Code Project - An interview with Microsoft's new Visual C Architect Stanley Lippman - Interviews



The author of the primary book I am using on this adventure is Stanley Lippman who recently has taken the Microsoft Visual C++ Architect positon.

Pointers in C++



Pointers in C++ are a major conceptual stumblingblock. Lippman's book was very confusing on this. And even Bruce Eckel left me confused. My third resource, C language tutorial, by Juan Soulié finally gets it right explaining C++ pointers.





This following simple paragraph made all the difference in the world clarifiying the declaration of a pointer [e.g. int * mypointervariable ] versus using a pointer variable to access the value of the variable being pointed to [e.g. variable2 = *mypointervariable] or loading an address into a pointer variable [e.g. mypointervariable = &addressOfVariable] made all the difference in the world!



I emphasize that this asterisk (*) that we put when declaring a pointer means only that: that it is a pointer, and does not have to be confused with the reference operator that we have seen a bit earlier and that is also written with an asterisk (*). They are simply two different tasks represented with the same sign.





Thanks Juan Soulié!



Wednesday, April 03, 2002

Jello World







#include

#include

using namespace std;



int main()

{

string user_name;

cout << "What is your name: ";

cin >> user_name;

cout << '\n'

<< "Hello, "

<< user_name

<< "... and goodbye! \n ";



return 0;

}





This runs fine in VC++ using the iostream only, I guess by refering to the standard class only std instead of the Microsoft stdfx class ???



This sample comes from one of two books I'm using: Essential C++ by Stanley B. Lippman ISBN 0-201-48518-4 from Addison-Wesley C++ In-Depth Series. The other book is Bruce Eckel's Thinking in C++ which can be downloaded free from his site and bought in bookstores.



The covererage in this sample code is the "#include" command (or is it a keyword?) that brings in an object. The iostream itself is the standard C++ library of objects and is what gives understanding to the cin and cout keywords; notice the directions of the brackets used for each. Also the int TYPE declaration on the function MAIN() is declaring what will be the data type of the return value. In this case RETURN is zero, thus signaling success. Anything non-zero indicates failure. The RETURN 0 doesn't have to be specified as here if the keyword VOID is used instead of INT. That's what VOID means - nothing returned; whereas here just the fact that MAIN() has nothing in its () brackets - that itself declares that the function has no RETURN.



Also note the use of NAMESPACE to restrict these specific keywords used as coming from the STD library only.



String characters are shown proper format along with the "newline" (backslash N) declaration.

Hell-oh Whirled





#include

void main()

{

cout << "Welcome to C++ \n";

}



This runs fine in compiled in Microsoft Visual C++ studio. If you change iostream.h to iostream only, it errors out with no understanding of the cout.



Tuesday, April 02, 2002

C++ . . .



I've always known that the true heavy duty programmers code in C++ (assuming we're past the age of the elitist Assembly programmers) and I've always tried hard to avoid learning it (as in REALLY learning it, not just Visual C++ write a few components per instructions kind of thing). I hate JAVA (it's so dang popular and hip, not to mention slow and NOT so cross platform as everyone likes to think. And Visual Basic is just too darn distanced from any real Object Orientated code that it's frustrating to learn that GUI-drag n' drop-ActiveX-controls-box-drawing crap. And all these VB guys that I know don't seem to have a programming clue - so what the heck are they doing?


So I'm biting the bullet and experimenting and logging my progress learning C++. We'll seeing if I'm wasting time, fooling myself, or pursuing (what I believe is) the true heart of all the programming languages [note*] .





The following posts should log my progress and serve to prompt me to continue my development. My goal is to end this experiment in 6 weeks to see what progress can be made with a crash course self-taught method such as this.






note* Java and C# are both based on C++, and VB is just a giant work around to avoid it.