C++ Session 2

Preview:

DESCRIPTION

 

Citation preview

Session 2

Session Objectives

• Identify the C++ character set

• Discuss the identifiers and keywords

• Explain various data types and qualifiers

• Identify the C++ variables

Introduction

Identifier

C++ Character set

Special characters[ ] ( ) . -> ++ -- &

* + - ~ ! \ /

% << >> < > <= >=

== != ^ && | || ?:

= *= /= %= += -= <<=

>>= &= ^= |= , # ##

Escape sequences (1)

Back space

New line

Tab

Escape sequences (2)

Carriage returnForm feed

Single quote

Escape sequences (3) Back slash

BellOctal number given by oooHexadecimal

number given by hh

Escape sequences (4)#include <iostream.h>

#include <conio.h> void main(void){clrscr();//This function is used to clear the screencout << "Using the newline escape sequence.\n" ;cout<<"To use backspace escape sequence,press enter" ;getch() ;//This function is used to pause for user entrycout << "\b\b\b\b\b\b\b\b\b\b\bUsed backspace" ;cout << "\nUsing the tab sequence\t\t\t\t tab here" ;getch() ;}

To use backspace escape sequence, press enterTo use backspace escape sequence, Used backspaceUsing the tab sequence tab here

Identifiers (1)

Variables

functions

labels

User defined objects

Identifiers (2)

First character = alphabet

Subsequent =characters

or underscore

alphabetsnumbersor underscore

s

Identifiers (3)

Follow Standardnaming convention

Name should definethe purpose of existence

Use prefixes and suffixesReferring is

easierAvoid using

u,i,j…Leads to confusion

Identifiers (4)

Of an identifier is the part of the program where the identifier is recognized

Keywords

New in C++

Data Types and Variables

Pre-defined Data TypesType Description Length

char Single letter character entries only 8 bits

int (short) Integer values only 16 bits

long Accepts higher range of integer values than the int data type

32 bits

float Supports decimal point notation 32 bits

double Supports decimal point notation with greater precision than Float data type

64 bits

double float

Similar to a float, with greater precision to the right of the decimal point

128 bits

void Does not return any value. N/A

Declaring variables

data_type variable name = initial value

optional

int num ;

int num1 = 3;char alpha = ‘G’ ;float float_no ;float floa = 33.33;double d_no ;double doub = 1234.121212121 ;

Character strings

char str_string[6] ;

char str_string[30]= “ Nice Day “ ;

Variable qualifiers

Type modifiers

longsignedunsignedshort….

long int var1 ;

Unsigned short int var2 ;

Access modifiers

const float pi = 3.146 ;const char three = ‘3’;const int number = 5 ;const double d_number = 8738478.9898 ;

Constants (1)

Constants (2)

const int a = 41 ; // decimal const int b = 0345 ;// octal begin with 0const int c = 0x9f ;// hexadecimal begin // with 0x

Constants (3)

//Defining a standard decimal valueconst float f_number = 12.5766 ;

Either the whole part of the number or the decimal part can be omitted …

but not both

Constants (4)

Const double d_const_val =9.43E-99 ;

Mantissa in decimal notationFollowed by letter ‘ e ’ or ‘ E ‘

Constants (5)

const char c_alpha = ‘A’ ;

Constants (6)

Constants (7)

#include <iostream.h> void main(void){// Defining a string constant const char str_message[ ] = “All done“;cout << str_message ;}

“All done“

Recommended