Sie sind auf Seite 1von 4

Dr.S.J.M.Yasin/CE206_lecture_02_basics/ 28 March 2009/P-1 Dr.S.J.M.

Yasin/CE206_lecture_02_basics/ 28 March 2009/P-2

A sample C++ Program When we run the above program the following appears in an output
window on the monitor:
//Program - P01 When we run the
#include<iostream> above program the
using namespace std; following appear in
void main() the output window on
{ the monitor :
cout<< "Trial program";
}
The cursor blinks at the 1st position of the 3rd line (just below T on 2nd
line). Suppose we type 20 as shown below.

................................................................
//Program - P04 Next we have to press Enter button on the keyboard and the output
/* Program that finds the average of two numbers */ window will look like :
#include<iostream>
using namespace std;
int main()
{
double num1, num2, average;
cout<<"Program to find the average of two numbers" Now suppose we typed 30 and pressed enter button. The output window
<<endl; will be like
cout<<"The first number ="<<endl;
cin>>num1; Now if you type 30 and press Enter, you will see the following:
cout<<"The second number ="<<endl;
cin>>num2;
average = (num1 + num2)/2;
cout<< "Trial program"<<endl;
cout<<"Average =" <<average
<<endl; //Prints the value stored in variable average
return 0;
}
..........................................................................
Dr.S.J.M.Yasin/CE206_lecture_02_basics/ 28 March 2009/P-3 Dr.S.J.M.Yasin/CE206_lecture_02_basics/ 28 March 2009/P-4

Using the above example program, now I shall explain some features of You must declare a variable name before you can use it.
C++ programming.
Examples of variable declaration:
characters and character set
int student_no;
The characters that you can use in the C++ program are int age;
upper and lower case letters, A to Z and a to z -------------------------------------
digits 0 to 9 int student_no, age;
the space character -------------------------------------
control characters- new line, horizontal and vertical tab, form feed, string name;
bell double length, width, area;
special characters (or symbols) --------------------------------------
_ { } [ ] # ( ) < > $ : ; . ? * + - / ^ &| ~! = , \ " int count=0;
' ----------------------
int a=5, b=2;
variable
When we run a program, we need to store data (either numeric or text) Note :
in the memory (RAM). We do this by use of a variable. There must be at least one space between the type name and the variable.
A variable is a name given to a memory location where you can store a Multiple variables of the same type can be declared in one statement and
data of a particular type. The type is specified when the variable is these are to be separated by comma (with or without additional spaces).
declared. You can initialize a variable when you declare it.
The name you can give to a variable can consist of any combination of
upper or lower case letters, underscores ( _ ) and the digits ( 0 to 9 ). Any statement that introduces a name into a program is a declaration
A variable name is case sensitive. (C++ is a case sensitive language). (as shown in the examples). A declaration is also called a definition
This means length and Length, LENGTH are different names. because it allocates memory for the variable.
A variable name cannot begin with a digit.
Do not use variable names that begin with an underscore followed by a Basic data types
capital letter or which contain two successive underscores (these
forms are reserved for use within the libraries). char There are also data Typically float will
Tips: short types such as: provide 7 digit
You are free to assign any name (other then a keyword) to a variable. int precision, double
But if you choose names that are meaningful and name your variables long unsigned long will provide 15
in a consistent manner, it will make your program more readable and float unsigned short digit precision and
less error prone. double unsigned int, long double will
long double signed long, provide 19 digit
Examples of variable names: string signed short, precision
signed int
monthly_salary, temperature, pay, mark, mark_ce206,
Dr.S.J.M.Yasin/CE206_lecture_02_basics/ 28 March 2009/P-5 Dr.S.J.M.Yasin/CE206_lecture_02_basics/ 28 March 2009/P-6

keywords whitespace

These are reserved words that have particular significance and when Whitespace refers to spaces, horizontal and vertical tabs and newline
needed are to be used as it is. In the above program include, iostream, characters. Whitespaces are required in a C++ statement for the
using, namespace, std, int, cout, return are keywords. compiler to be able to distinguish between the elements of the
statement.
at least one white space is required here
C++ Statements and statement blocks int main()
A statement is analogous to a sentence in any language. As you know a No whitespace is essential here
sentence expresses a complete meaning. Similarly a C++ statement
number_1=5;
expresses a complete instruction to the computer. Statements are
basic units for specifying what our program is to do. average = (number_1 + number_2)/2;
Most C++ statements end with a semicolon.
A single statement can continue into multiple lines. Also two statements
may be written in one line (I would suggest to avoid it). Notice the style Function, function name and function body
followed in the example programs for writing the statements.
The above program consists of a single function named main. A
We can enclose several statements between a pair of curly braces {}, in
function is a self-contained block of code referenced by a name.
which case they are referred to as a statement block. A statement
There may be more than one function in a C++ program, but there
block is also referred to as a compound statement. A statement block
shall be at least one function named main and not more than one
has important effect on the variables that we use to store data (more
function shall have the name main.
details when we discuss variable scope).
The brackets ( ) after the function name is essential. A function may
You must declare the name of a variable before you use it.
have arguments that are placed within these brackets. In the above
Whitespaces may be essential between certain elements of a statement
example the function main has no argument.
or between statements.
The function has a type. In the above example it is of int type.
There are different sorts of statements in C++ (examples shown below).
The braces { } delimit a block of code which in this case is the function
body
Examples of C++ statement and statement block:
comment
#include<iostream> The first line of the program is a comment. Everything from the first //
in a line to the end of that line is ignored (i.e. not converted to
double result; //declaration machine code), including further occurrences of //. We use comments
double result=0.0; //declaration and initialization to document our code. You can temporarily remove a line of code
length = 5.0; // assignment from your program just by placing a // to the beginning of that line.
There is another from of comment called multi-line comment or block
if(marks>=40) //decision making statement comment or C-style comment. If you put a part of code between /*
cout<<”pass”<<endl; and */ then this block will become a comment.
else
cout<<”fail”;
Dr.S.J.M.Yasin/CE206_lecture_02_basics/ 28 March 2009/P-7

header files and the #include directive

#include<iostream> includes the contents of the header file iostream


into the program.
iostream is one of several standard header files that are supplied with
C++ compiler.

The header files contain information (code) for the available library
functions. The name cout is defined in the header file iostream.
This is a standard header file that provides the definitions necessary
for standard input and output facilities. The standard input device
is the keyboard and the standard output device is the monitor.
Header files are usually distinguished by the filename extension .h.
(this is not mandatory; ANSI standard header files has no
extension)
These files are called header files because, when needed, they are
included at the beginning (the head) of the source file.
There may be several #include statement in a program.
Note that there are no spaces between the angles brackets and the
standard header file name. Spaces may cause problem in case of
some compilers.

namespace
The namespace refers to a group of several names (or entities). The
entities in the C++ standard library are all defined within a namespace
called std. You may also consider namespace as a surname. You may
consider that the full name of cout is std::cout. If we omit the using
directive we can write the output statement as

std::cout<< “Trial program”<<endl;


std::cout<<”Average =”<<average; //Prints the value stored in
variable average

return statement

The return statement ends the function and returns control to the
operating system. It also returns a value (0 in the above example) to the
operating system.

Das könnte Ihnen auch gefallen