Programming Languages
A Dave Hillman Project

[Home] [Contact Me] [Site Map] [Search] [References]

C++

 

[Up]

[Language Basics]
[C Language]
[C++]
[C#]
[Java]
[JavaScript]
[Visual Basic 6.0]
[VBScript]
[VB.Net]
[.Net Technology]

 

 

 

Introduction

C++ is one of the most complex of languages ever created while, at the same time, is based on the very basic 'C' language.  This paradox means that C++ has all of the simplicity of C while allowing very complex and powerful applications to be developed that take advantage of object oriented (OO) programming techniques.  These OO techniques enable the C++ development environment to be extended to represent real-world and logical data structures as objects.

Language Fundamentals

Program Structure and Organization

bulletC++ programs share a great deal of structural organization to C programs.
bulletProgram structure:
bulletSimilar to C, the following example illustrates a basic C++ program:

 

//a demo program // starts a line of comment
#include <iostream> creates a reference to an standard or developed library
using namespace std; makes I/O facilities available to the program
void main () initial function, starts program execution (same as C)
{ function bracket - opening
cout << "Hello World"; code for the main function, prints text in the standard output
} closing function bracket

 

bulletIdentifiers and keywords for C++ includes:
asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t
bulletSource code management (files, etc.)
bulletCode is written in ASCII (or native environment) text.
bulletVendor specific tools such as those by Intel and Microsoft have powerful development environments that assist in building and managing code, access database development tools, and generating complex user interfaces.
bulletC++ programs are compiled to executables often requiring library support for a variety of object-based tools for database access, web server and client support, etc.

Data Types

Name Bytes* Description Range*
char 1 character or integer 8 bits length. signed: -128 to 127
unsigned: 0 to 255
short 2 integer 16 bits length. signed: -32768 to 32767
unsigned: 0 to 65535
long 4 integer 32 bits length. signed:-2147483648 to 2147483647
unsigned: 0 to 4294967295
int * Integer. Its length traditionally depends on the length of the system's Word type. See short, long
float 4 floating point number. 3.4e + / - 38 (7 digits)
double 8 double precision floating point number. 1.7e + / - 308 (15 digits)
long double 10 long double precision floating point number. 1.2e + / - 4932 (19 digits)
bool 1 Boolean value. It can take one of two values: true or false NOTE: recently added by the ANSI-C++ standard, not all compilers support it. true or false
wchar_t 2 Wide character. It is designed as a type to store international characters of a two-byte character set. NOTE: recently added by the ANSI-C++ standard; not all compilers support it. wide characters

 

bulletDeclaring variables:
bulletint i;  -  initializes a variable of type integer called i
bulletint i = 3;  - initializes and i and sets it equal to 3
bulletint i1, i2;  -  initializes two variables: i1, and i2
bulletVariables are case sensitive.
bulletArrays:
bulletAll data types support array declarations.
bulletExample: char line[81];  - declares a character array with 81 positions
bulletFilling an array:  line[2] = 'A'
bulletMultidimensional arrays are possible:  e.g., short array_name[50][20][10] creates an array with 10,000 elements.
bulletVariable scope:
bulletVariables declared in functions live within the function.
bulletstatic int i;  - creates a variable scoped within the file.
bulletextern int i; - creates a variable scoped within an application.

 

Expressions and Operators

Similar to C with additional features to handle objects.

Operators
(in order of  priority)

Description

:: Scope access / resolution
() [ ] -> . sizeof  Function calls, array subscripts, etc..
++ -- Increment/decrement
~ Complement to one (bitwise)
! Unary NOT
& * Reference and Dereference (pointers)
(type)
delete (removes memory storage)
new (allocates memory storage)
Type casting
+ - Unary less sign
* / % Arithmetical operations
+ - Arithmetical operations
<< >> Bit shifting (bitwise)
< <= > >= Relational operators
== != Relational operators
& ^ | Bitwise operators
&& || Logic operators
?: Conditional
= += -= *= /= %=
>>= <<= &= ^= |=
Assignment
, Comma, Separator

Class Development Support

bulletClasses are logical methods to organize data and functions in the same structure.
bulletClasses are declared using keyword 'class' (similar to the C keyword struct, but with the possibility of including functions as members, instead of only data).
bulletThe form is:
class class_name {
  permission_label_1:
    member1;
  permission_label_2:
    member2;
  ...
  } object_name;
bulletclass_name is a name for the class (user defined type) and the optional field
bulletobject_name is one, or several, valid object identifiers.
bulletThe body of the declaration can contain members,
bulletThese can be either data or function declarations, and optionally permission labels, that can be any of these three keywords: private:, public: or protected:.
bulletThese make reference to the permission which the following members acquire:
bulletprivate members of a class are accessible only from other members of their same class or from their "friend" classes.
bulletprotected members are accessible from members of their same class and friend classes, and also from members of their derived classes.
bulletpublic members are accessible from anywhere the class is visible.
bulletIf we declare members of a class before including any permission label, the members are considered private, since it is the default permission that the members of a class declared with the class keyword acquire.

 

Control Statements

Generally the same as C...

bulletIf-then
bulletEvaluate statements if expression is true for if condition or subsequent else conditions.
bulletIncludes if, if-then logic; complex Boolean evaluation.
bulletShort-circuit if first logical expression is found false.
bulletExamples:

if (expression) statement;

if (expression)  {
    statement;
    statement;
    ....  }

if (expression)  {
    statement;
    statement;  }
else
     statement;

if (expression)  {
    statement;
    statement;  }
else  {
    statement;
    statement;  }

if (expression)  {
    statement;
    statement;  }
else if (expression)
     statement;

bulletSwitch
bulletEvaluate expression based on truth of condition.
bulletExpression is a value to be evaluated in subsequent cases.
bulletExamples:

switch (expression) statement

switch ( command )
    case 'A': statement
    case 'B': statement

 

bulletDo/While loop
bulletEvaluate until expression evaluates to true.
bulletDo/while evaluates following the loop.
bulletWhile loops evaluate prior to the loop start.
bulletExamples:

do {
    statement;
    statement;
    ...  }
while (expression)

while (expression) {
    statement;
    statement;
    ...  }

 

bulletFor loop
bulletLoop based upon initial, goal, and modification.
bulletExample and syntax:

for (expression1; expression2; expression3)
        statement...

expression1 - initial value
expression2 - goal value
expression3 - modify value or function

 

bulletGo To
bulletSyntax: goto label_name;
bulletA label is a name followed by a colon.
bulletLabel must be contained in same function.
bulletContinue
bulletOnly used in a loop; jumps over remainder of a loop to the test condition.
bulletBreak
bulletUsed in a loop to exit the loop.
bulletReturn
bulletSyntax: return (expression)
bulletEnds a function, returning value to the calling function.

Input/Output

bulletWithin the iostream.h library, there are two basic input/output mechanisms:
bulletcin  -- data input, used with the extraction operator (>>);
bullete.g.,   cout << "Hello world";
bulletcout  -- data output, used with the insertion operator (<<)
bullete.g., cin >> Name;  (waits for user input)
bulletA manipulator can be applied to cout to manage the number of digits and format
bulletC++ has support both for input and output with files through the following classes:
bulletofstream  --  file class for writing operations (derived from ostream)
bulletifstream -- file class for reading operations (derived from istream)
bulletfstream  -- file class for both reading and writing operations (derived from iostream)

 

Resources

References

bulletGoogle Directory: http://directory.google.com/Top/Computers/Programming/Languages/C%2B%2B/

Tutorials

bullet C++ language tutorial

 

Note: I am publishing this a bit early -- it still needs some work, but this is a good start.

 

 

[Home] [Language Basics] [C Language] [C++] [C#] [Java] [JavaScript] [Visual Basic 6.0] [VBScript] [VB.Net] [.Net Technology]

Copyright © 2003-2005 by Dave Hillman