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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home