You are on page 1of 16

BASIC FEATURES OF C++

Characters and character set


The characters used in the C++ program are upper and lower case letters, A to Z and a to z digits 0 to 9 the space character control characters- new line, horizontal and vertical tab, form feed, bell special characters (or symbols) _ { } [ ] # ( ) < > $ : ; . ? * + / ^ &| ~! = , \ " '

Variable
A variable is a name given to a memory

location where you can store a data of a particular type. The type is specified when the variable is declared.

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 ).

A variable name is case sensitive. (C++ is a

case sensitive language). This means length and Length, LENGTH are different names.
A variable name cannot begin with a digit. Examples of variable names:

monthly_salary, temperature, pay, mark, mark_ce204,

Declaration of Variable
Examples of variable declaration:
int student_no; int age;

int student_no, age;


string name; double length, width, area; int count=0; int a=5, b=2;

Basic data types


char

short
int long float double

long double
string

There are also type such as unsigned long, unsigned short, unsigned int signed long, signed short, signed int.

Typically float will provide 7 digit precision, double will provide 15 digit precision and long double will provide 19 digit precision

Keywords

These are reserved words that have particular

significance and when needed are to be used as it is. In the above program include, iostream, using, namespace, std, int, cout, return are keywords.

C++ Statements and statement blocks


A statement is analogous to a sentence

in any language.
A C++ statement expresses a complete

instruction to the computer. Statements are basic units for specifying what our program is to do.
Most C++ statements end with a

semicolon.

Examples of C++ statement and statement block:


#include<iostream>
double result; //declaration double result=0.0; //declaration and initialization

length = 5.0; // assignment


if(marks>=40) cout<<pass<<endl; else

cout<<fail;
//decision making statement

Whitespace
Whitespace refers to spaces, horizontal and vertical tabs and newline characters. Whitespaces are required in a C++ statement for the compiler to be able to distinguish between the elements of the statement.

Function, function name and function body


A function is a self-contained block of code

referenced by a name. There may be more than one function in a C++ program, but there shall be at least one function named main and not more than one function shall have the name main. The brackets ( ) after the function name is essential. A function may have arguments that are placed within these brackets. The function has a type.

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.

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.

EXAMPLE

/* Program that finds the average of two numbers */

#include<iostream>
using namespace std; int main() { double num1, num2, average; cout<<"Program to find the average of two numbers" <<endl; cout<<"The first number ="<<endl; cin>>num1; 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;

You might also like