Professional Documents
Culture Documents
Basic C++ Syntax: College of Informatics and Virtual Education (CIVE)
Basic C++ Syntax: College of Informatics and Virtual Education (CIVE)
(CIVE)
1
General C++ Program
//Program description is first
#include directives go next
using namespace std;
int main()
{
constant declarations go here
variable declarations go here
3
General C++ Language..
Compiler Directive: #include
4
Libraries
#include loads the code from the standard libraries:
#include <iostream> // new I/O library
#include <iostream.h> // old I/O library
#include <stdio.h> // standard functions
#include <math.h> // math functions
#include <stdlib.h> // contains random funct
#include <time.h> // time function
using namespace std; indicates that the new C++
libraries should be used. If this line is left out, then the
old iostream library is loaded:
#include <iostream.h>
Namespace
The #include <iostream> command is where cin
and cout are declared.
They are declared within a namespace called std
When we specify using namespace std;
Then we need NOT preface the cin and cout commands with
std::cin and std::cout
6
Keywords
Keywords have predefined purpose in the language.
Therefore, keywords should not be used for naming
purposes.
That is, do not use keywords as variable, constant,
function or program/file name.
Some of the keywords in C++ include:
bool, break, case, char, const, continue, do, default, double,
else, extern, false, float, for, if, int, long, namespace, return,
short, static, struct, switch, typedef, true, unsigned, void,
while
Note that all keywords are in lowercase. 7
Comments
Comments are explanatory notes; they are not part of
the program.
The compiler ignores all comments.
There are two ways to write comments in a C++
program:
Single line comments: Everything after the // until the line return
is a comment. This is the preferred method for single line
comments
Multiline comments: Everything between the /* and */ is a
comment. Can be used to comment out a block of code when
debugging a program, and to add sample output at the end of a
program
8
Case Sensitivity
A programming language where upper and
lower case make a difference is called case
sensitive.
C++ is a case sensitive language, therefore
treats differently identifiers like Muce,
MUCE, mUcE.
9
Identifiers
Identifiers are the names given to programmer-
defined entities within the program – variables,
constants, functions, etc.
Rules for naming identifiers:
Names in C++ are case sensitive.
Names may be very long up to 255 characters
Names may contain letters, numbers and only the special character
underscore
Names may NOT begin with a number
Names must begin with a letter (at this stage in your C++ knowledge)
Names may NOT contain blanks
Names may not be a reserved word
10
Identifiers: Style Conventions
12
Data Types
13
Data Types..
14
Constants
Examples
const float TAXRATE = 0.0675;
const int NUMSTATES = 50;
16
Variables
Examples:
int age;
double salary;
Examples:
int n, m, k; // declaration
n = 5;
m = 6 + (4 * 6);
k = (n * 2) + m;
k = k / 2;
Assignment Statements..
int NewStudents = 6;
NewStudents 6
int OldStudents = 21;
OldStudents 21
int TotalStudents;
TotalStudents -
24
Displaying Information on the Screen..
Other Escape Sequences include:
25
Getting Data From the User
cin - the standard input stream is used to get
data from the user.
It is declared in the header file iostream
Syntax:
cin >> variable;
Input operator “>>”
extracts data from input “stream” (the keyboard by
default)
skips over white spaces
extracts only characters of the right form and
performs automatic conversion to the type
specified
26
Getting Data From the User..
Often used with cout to display a message asking
the user to input data.
Can be used to input more than one value:
cin >> height >> width;