c++ session 2

28
Session 2

Upload: chiranjeevi-veluru

Post on 18-Nov-2014

959 views

Category:

Education


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: C++ Session 2

Session 2

Page 2: C++ Session 2

Session Objectives

• Identify the C++ character set

• Discuss the identifiers and keywords

• Explain various data types and qualifiers

• Identify the C++ variables

Page 3: C++ Session 2

Introduction

Identifier

Page 4: C++ Session 2

C++ Character set

Page 5: C++ Session 2

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

* + - ~ ! \ /

% << >> < > <= >=

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

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

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

Page 6: C++ Session 2

Escape sequences (1)

Back space

New line

Tab

Page 7: C++ Session 2

Escape sequences (2)

Carriage returnForm feed

Single quote

Page 8: C++ Session 2

Escape sequences (3) Back slash

BellOctal number given by oooHexadecimal

number given by hh

Page 9: C++ Session 2

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

Page 10: C++ Session 2

Identifiers (1)

Variables

functions

labels

User defined objects

Page 11: C++ Session 2

Identifiers (2)

First character = alphabet

Subsequent =characters

or underscore

alphabetsnumbersor underscore

s

Page 12: C++ Session 2

Identifiers (3)

Follow Standardnaming convention

Name should definethe purpose of existence

Use prefixes and suffixesReferring is

easierAvoid using

u,i,j…Leads to confusion

Page 13: C++ Session 2

Identifiers (4)

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

Page 14: C++ Session 2

Keywords

New in C++

Page 15: C++ Session 2

Data Types and Variables

Page 16: C++ Session 2

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

Page 17: C++ Session 2

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 ;

Page 18: C++ Session 2

Character strings

char str_string[6] ;

char str_string[30]= “ Nice Day “ ;

Page 19: C++ Session 2

Variable qualifiers

Page 20: C++ Session 2

Type modifiers

longsignedunsignedshort….

long int var1 ;

Unsigned short int var2 ;

Page 21: C++ Session 2

Access modifiers

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

Page 22: C++ Session 2

Constants (1)

Page 23: C++ Session 2

Constants (2)

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

Page 24: C++ Session 2

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

Page 25: C++ Session 2

Constants (4)

Const double d_const_val =9.43E-99 ;

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

Page 26: C++ Session 2

Constants (5)

const char c_alpha = ‘A’ ;

Page 27: C++ Session 2

Constants (6)

Page 28: C++ Session 2

Constants (7)

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

“All done“