Deborah R. Fowler
Syntax Quick
Reference
Posted 2013
Update April 22 2014
Sample files exist in the dropbox. This table
is not a substitute for your textbook or you own notes but
simply a quick reference for those of you who are less familiar
with the syntax or are switching back and worth between
languages to look up the syntax quickly.
Variables
|
Constants
Random Numbers #include
<cstdlib> srand( time(0)
); |
Notation
|
||||
Blocks
} |
If/then/else if ( condition ) { statements; } else { statements; } |
Looping
|
||||
Functions int myFunction() { statements; return 0; // return some value } Coding Style: declartion and definition to identify the function at the top and define below main (later to be in their own files, ultimately wrapped in classes) int myFunction(); // declaration int myFunction() { // some code to do something return 0; // return value must match } |
Procedures (functions that
act on data but don't explicitly return a value) - now
mostly lumped under the word functions void myFunction() { statements; } |
Parameters/Arguments When you have information to pass back and forth, these go in the (), for example int myFunction( int x, float y ) { int resultVariable; // x and y are parameters and are local to the function // they take on the value of the argument return resultVariable; } To call a function (or procedure) you call its name, for example to run this piece of code int resultFromCalling = myfunction( 10, 12.6 ) Pass by Value is default Pass by Reference and Pass by Pointer will be heavily used and are discussed below |
||||
Arrays int myArray[3]; int myArray[5] = { 10, 15, 2, 35, 41 }; remember C++ starts at 0 myArray[0] = 5; |
Vectors (new
improved "lists") #include <vector> using namespace std; vector<int> myVector; // declare myVector.push_back(0); // make a space myVector.size(); myVector.begin(); myVector.end(); Also available: #include <algorithm> // to allow use of find, sort etc. |
Interators (hinting at
pointers) vector<int>::iterator myIterator; for ( myIterator = grades.begin(); myIterator != grades.end(); ++myIterator ) { cout << *myIterator << endl; // note the dereference } This has the same functionality as: for ( int i = 0; i < grades.size(); ++i ) { cout << grades[i] << endl; } myIterator = find( grades.begin(), grades.end(), searchValue ); sort ( grades.begin(), grades.end() ); |
||||
Pointers
For example: * to access the
variable pointed to or declare a pointer References provide another
name for a variable (like a nickname) - not to be
confused with pointers. |
Classes
class NameOfClass { public: int x; int y; }; referred to data members and member functions with the dot operator NameOfClass ThisOne; ThisOne.x = 3; Default invisible constructors. If you want to explicitly define: NameOfClass::NameOfClass Destructors use the symbol ~ |
Notation for Classes
// usually in a .h file class SomeClass { public: int x; int myFunction(); } // usually in a .cpp file int SomeClass:: myFunction() { // usual function stuff here, including return } SomeClass MyObject; SomeClass *pMyObject = &MyObject; cout << (*pMyObject).x; cout << MyObject.x; Since pointers to objects are used so often, two conveniences are: SomeClass *pMyObject = new SomeClass(); cout << pMyObject->x; delete pMyObject; |
||||
Inheritance and
Polymorphism (more in class) Use the : after the class name to inherit polymorphism is using the pointer to figure out the right thing to do |
Buzz words in a nutshell Modularity - repeating code effectively, testing as you go (Functions help with all these) Encapsulation - scoping/hiding to protect data/errors Abstraction - don't sweat the details/(plan using algorithms, outline-refine) |
Note: Structure vs OOP
A structured program is organized by functions, main calls other functions - it is a series of functions and relationships between them.
OOP (Object-oriented programming) - code is organized by objects - an object contains both variable and functions, but now the program is a series of object (with a little bit of code in main() typing them together to say how they are used.
Which to use? Ask does your program do something (with minimal user intervention) or does it react to something (like user input). Most do both, but which dominates?
Note about shorthand initialization of variables in constructors ie. NameOfClass::NameOfClass( int x ) : memberData(x) found here.