OpenGL Programming

C + + from the Ground Up

Switch (expression)

{

case 'I' : break;

default goto home;

}

Continue - bypasses the normal iteration of a loop -> if (x=2) continue;

Pointers:
int *p; // numerical

cout < < &p; // (points to memory address) If the location is a memory address the value does not change

Address (pointer) ----> value (variable) Single Indirection

Address (pointer) ----> Address (pointer) ----> value (variable) Multiple Indirection

Example: int x, *p,  **q, x = 10; p = &x; q =&p;

cout < < *a; // This reference points to the value of x

Passing command line arguments:

main (int argc, char *argv[]) { } Usage: number of arguments, argument string

Must convert string to desired value.

To pass numbers use atof defined in stdio.h Ex. double a=atof(argv[1]);

Recursion: when a function calls itself. This is slower than iterative (line for line)

Function Overloading:

many different forms of a function, supports polymorphism, compiler must be able to distinguish from different forms of each function.

Example: void f (int i) void f (int i, intj) void f (double k)

Const keyword - cannot change the value of the variable

Volatile - value is obtained each time the variable is referenced Ex. Volatile int clock;

Extern keyword - Supports functions from many different files

Register keyword - allows faster access to at least two variables in the register

Enum week {mon, tue, wed, thur, fri, sat, sun}; // values correspond (0, 1 2, 3, 4, 5, 6)

The '?' keyword - shorthand for the if statement, if true then exp2 / if false then exp3

Form: Exp1 ? Exp2 : Exp3; <-- must be one line statements

Multiple assignment ---> a = b = c = 4:

Sizeof var; --> computes the size in bytes of the data occupied by the variable

New and delete keywords - the C + + version of malloc() and free()

Example: int *p; p=new int (99); if (!p) {cout << "Access Failed"; } cout << *p; delete *p;

Structures:

Struct inv_type {

char item[40];

double cost;

int on_hand;

} paint, tools; // All members are public, objects may be declared at ending bracket

Union - a memory location that is shared by two or more variables

union U_type { int i; char ch; }; // <---- Members of the union

Class music {

int beats;

char song;

public:

void show (int x);

}  Object_List; // May also use form music obi_name; to declare an object of class music

All members are private until made public.

music: show (int x) {} // <--declares a function

music.show(4); //                 accesses the function

 

 

Constructor initialized every time a class is called, it may NOT return any value
class_name() {code} //placed after the public identifier with other functions
Destructor called every time the class is exited
~class_name {code} //also placed in the function definitions
Inline keyword -
Used before a function definition to expand the code to iterary form NOTE: cannot be used with loops! Helps with speed and clarity, but increases size. A function is also considered inline if it is declared inside of a class.
Friend functions -
allow a non-member function to access the private members of a class. Friend is placed inside the public declarations of a class in the function def.
Dynamic initialization - variables that are initialized when they are declared.

Inheritance -

Allows a broad class structure to be added and defined by a specific

Inherited class.

Form : Class base_class:access_derived_class {code};

 

Notes: The base class is general, access is either public, private, or protected.

Public: All members of the base class become public members of the derived class
Private: All public members of the derived class become private members of the derived class.
Protected: the member is not accessible to other non-member elements of the program with the exception that access to a protected member can only be accessed by other members of the class of which it is a part.
 

To access a derived function us the form: void derived :: function ()

Inheriting multiple base classes - See example in multbase.cpp

Form: class derived: public base 1, public base2 { code } ;

Polymorphism - one interface, many methods

Pointers to derived types - form the basis of polymorphic principals

Declaration:
B_class *p; B_class b_obi; p = &b_obj // address of base

p->put_author("William Shakespeare");

See example in pointer.cpp

Virtual keyword - used to prevent a derived class from obtaining two copies of the base See example virtual.cpp

Virtual functions - Defined in the base and redefined in the derived classes. Each derived class has its own version of the function.

Declaration: place function def. inside the base class by preceding it with virtual.

The base class dictates the general interface that any object derived from the class will have, but it lets the derived class dictate the actual method itself. The base and the derived classes form a hierarchy.

In complex code polymorphism allows that only a few interfaces to be memorized, instead of a myriad of unrelated functions. See example Polymorp.cpp

Pure virtual function - Forces all derived classes to define their own versions of the function specified as pure.

Form: virtual type func_name (parameters) = 0; <--- place in the base class declarations

Example: virtual void show area()= 0; // pure

 

Stream - a common logical interface to the various devices that run the computer.
Two types of streams:
text - allows character formations

binary - actual representation

 

<< insertor operator

>> extraction operator

Inserter function - See example Inserter.cpp

General form:

ostream &operator << (ostream &stream, class_type obj) {

/* Body of function*\

return stream;

}
To allow incertor access to private data inside a class declare the incertor as:
friend ostream &operator <<(ostream &stream, class class_type obj) { Code }
Extractor function
general form: istream &operator >> (istream &stream, obj_type, &obj) {
/* Body of function */

return stream;

}
See also: Scanned Formatted I/O Table streams.jpg

Manipulator functions: allow complex format to be placed in a prototype

Form: ostream &manip_name(ostream &stream) { /*code*/ return stream; }
NOTE: although the declaration takes a parameter, none is used to call the function.

See short example in manip.cpp

 

File I/O

ifstream --> input ofstream --> output Istream --> input / output
 

ios::app -> Append to end of file

ios::nocreate -> open fails if NO file exists

ios::ate -> Seek end of file on open

ios::noreplace -> fails if file exists

ios::binary -> Sets binary open mode

ios::trunc - destroys file contents

 

To open for input and output:

fstream fp;

fp.open("test", ios::in ios::out);

/* OR USE */ fstream fp("test", ios::in | ios::out); // opens as the object is defined
Generic Functions - allow the definition the data type at run-time. The function performs the same task no matter what type of data is defined.
Form: template <class type> ret_type func_name(parameters) { body }
template <class X> void swap (X &a, X &b) { body of function } See example Template.cpp
Note: The complier expands the code to allow for each data type provided.

Use function overloading if there is a different algorithm for each type of data.

Array-based I/O - Uses RAM instead of CPU main memory <-- Faster than file access
All functions must include <strstrea.h>

istrstream - input

ostrstream - output

strstream - Input and Output

Example:
char str [80];

ostrstream outs(str,sizeof(str));

outs << DATA;
Dynamic allocated arrays - length is calculated when str() is called.
See examples: Arraystr.cpp - Dynamic.cpp
Pointers to functions - allow a function to be called as a parameter to another function. See example in Pfunct.cpp

 


Questions? Comments? email: nick@ohiofirst.com