Thursday, February 10, 2011

FOP : Chapter 2

Chapter 2 : C++ Basics

The C++ language consist of two elements : Semantics and Syntax 

  • Semantics is a vocabulary of commands that humans can understand and that can be converted into machine language. Table below shows a partial list of vocabulary use in C++ program programming that are also referred to as keywords or reserved word.
  • Syntax is a language structures (or grammar) that allow humans to combine these C++ commands into a program that actually does something.
Example : 

Cout<<”Hello World.”;       (Correct C++ statement) =Correct Syntax
c Output “Hello World”.    
(Incorrect C++ statement) = Wrong Syntax

This are the keywords/reserved word :





asm
do
inline
short
typeid
auto
double
int
signed
typename
bool
dynamic_cast
log
sizeof
union
break
else
mutable
static
unsigned
case
enum
namespace
static_cast
using
catch
explicit
new
struct
virtual
char
extern
operator
switch
void
class
false
private
template
volatile
const
float
protected
this
wchar_t
const_cast
for
public
throw
while
continue
friend
register
true

default
goto
reinterpret_cast
try

delete
if
return
typedef



A simple example C++ program :
#include <iostream>
using namespace std;
 
int main()
{
        cout << "Welcome to the wonderful world of C++!!!\n";
 
        return 0;
}

Here is an example of running the program:
Welcome to the wonderful world of C++!!!

  • A C++ application program is formed by many components that are comments, the include directive and header filesfunctionsdeclarationsreserved wordsconsole input/outputescape sequences and statements.




Components
Summary
Example
Comments
Comments are non-executing statements that you add to a program for purpose of documentation.
// One line statements
/* Multiple
   line
   statements */
The preprocessor directive and header file
The preprocessor directive enables the program to use certain functions contained in the external file.
#include <iostream.h>
#include <math.h>


Functions
Functions are the basic building block in C++.
Main()
{
    // Statements
}
Declarations
All variables used in a C++ program must be declared before their use. Variables are simply reference to memory locations.
int x, y, sum;
Reserved words
Reserved words used in the C++ language have special meaning and they cannot be used for any other purpose.
Include, int, main, return
Escape sequences
An escape sequence always begins with the backslash (/) and is followed by one letter or special character.
\n, \t, \\, \”, \’
Console input & output
Data are entered into the computer using cin (console input) and the redirection operator     >>. The function cout and the redirection operator   << sends the output to the system   console.
cout<”Enter a number: “;
cin>>num;
C++ statements
Executable statements in C++ must be followed by a semicolon.
Int x, y, sum;
cout<”Enter a number: “;
cin>>x;
sum = x + y;








2 comments:

  1. Please include your notes and exercises that i given to you since from algorithm, pseudo code, flow chart and coding. I already gave u a lots of exercises...dont miss it! Make sure you completed your blog before mid-term exam.

    ReplyDelete