cbse class 11 chap 6,7

30
Learners Support Publications www .lsp4you.com  Variables and Data  Types

Upload: amishadalal

Post on 02-Jun-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 1/30

Learners Support Publications www.lsp4you.com

 Variables and Data

 Types

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 2/30

Learners Support Publications www.lsp4you.com

Objectives of this session

Keywords Identifiers

Basic Data Types

bool & wchar_t Built-in Data Types

User-defined Data Types

Derived Data Types Symbolic Constants

Dynamic Initialization of Variables

Reference Variables

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 3/30

Learners Support Publications www.lsp4you.com

 Variables and Data Types

 Tokens

 The smallest individual units in a program are

known as tokens.

o Keywordso Identifiers

o Constants

o Stringso Operators

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 4/30

Learners Support Publications www.lsp4you.com

Keywords

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 5/30

Learners Support Publications www.lsp4you.com

Identifiers

 A valid identifier is a sequence of one or moreletters, digits or underscore characters (_).

Neither spaces nor punctuation marks or

symbols can be part of an identifier. Onlyletters, digits and single underscore

characters are valid. In addition, variable

identifiers always have to begin with aletter. They can also begin with an

underline character (_ ).

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 6/30

Learners Support Publications www.lsp4you.com

Identifiers

 The name of a variable: Starts with an underscore “_” or a letter, lowercase or

uppercase, such as a letter from a to z or from A to Z.Examples are Name, gender, _Students, pRice.

Can include letters, underscore, or digits.Examples are: keyboard, Master, Junction, Player1,total_grade, _ScoreSide1.

Cannot include special characters such as !, %, ], or $.

Cannot include an empty space. Cannot be any of the reserved words. Should not be longer than 32 characters (although

allowed).

continue… 

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 7/30Learners Support Publications www.lsp4you.com

Basic Data Types

C++ Data Types

User-defined Type Built-in Type Derived Type

Integral Type Void Floating Type

structure

union

class

enumeration

array

function

 pointer

reference

int char float double

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 8/30

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 9/30Learners Support Publications www.lsp4you.com

Data Type - bool

 A variable with bool type can hold a Boolean value true orfalse.

Declaration:

bool b1; // declare b1 as bool typeb1 = true; // assign true value to b1

bool b2 = false; // declare and initialize

 The default numeric value of

true is 1 and

false is 0.

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 10/30

Learners Support Publications www.lsp4you.com

Data Type –  wchar_t

 The character type wchar_t has been defined tohold 16-bit wide characters.

 wide_character uses two bytes of memory.

 wide_character literal in C++

begin with the letter L

L„xy‟ // wide_character literal 

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 11/30

Learners Support Publications www.lsp4you.com

Built-in Data Types

int, char, float, double are known as basic  orfundamental   data types.

Signed, unsigned, long, short modifier for integer

and character basic data types.

Long modifier for double.

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 12/30

Learners Support Publications www.lsp4you.com

Built-in Data Types

 Type void was introduced in ANSI C.

 Two normal use of void:o  To specify the return type of a function when it is not

returning any value.

o  To indicate an empty argument list to a function.

o eg:-  void function-name ( void )

continue… 

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 13/30

Learners Support Publications www.lsp4you.com

Built-in Data Types

 Type void can also used for declaring generic pointer.

 A generic pointer can be assigned a pointer value of anybasic data type, but it may not be de-referenced.

 void *gp; // gp becomes generic pointer

int *ip; // int pointer

gp = ip; // assign int pointer to void pointer Assigning any pointer type to a void pointer is allowed inC & C++.

continue… 

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 14/30

Learners Support Publications www.lsp4you.com

Built-in Data Types

 void *gp; // gp becomes generic pointer

int *ip; // int pointer

ip = gp; // assign void pointer to int pointer

 This is allowed in C. But in C++ we need to use a castoperator to assign a void pointer to other type pointers.

ip = ( int * ) gp; // assign void pointer to int pointer

// using cast operator

*ip = *gp;  is illegal

continue… 

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 15/30

Learners Support Publications www.lsp4you.com

User-Defined Data Types

Structures & Classes:

struct

union

class

Legal data types in C++.

Like any other basic data type to declare variables.

 The class variables are known as objects.

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 16/30

Learners Support Publications www.lsp4you.com

User-Defined Data Types

Enumerated Data Type:Enumerated data type provides a way for attaching namesto numbers.

enum keyword automatically enumerates a list of wordsby assigning them values 0, 1, 2, and so on.

enum shape {circle, square, triangle};

enum colour {red, blue, green, yellow};

enum position {off, on};

continue… 

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 17/30

Learners Support Publications www.lsp4you.com

User-Defined Data Types

Enumerated Data Type:

enum colour {red, blue, green, yellow};

In C++ the tag names can be used to declare new variables.

colour background;

In C++ each enumerated data type retains its ownseparate type. C++ does not permit an int value tobe automatically converted to an enum value.

continue… 

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 18/30

Learners Support Publications www.lsp4you.com

User-Defined Data Types

Enumerated Data Type:

colour background = blue; // allowed

colour background = 3; // error in C++colour background = (colour) 3; // OK

int c = red;  // valid

continue… 

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 19/30

Learners Support Publications www.lsp4you.com

User-Defined Data Types

Enumerated Data Type:

By default, the enumerators are assigned integer

 values starting with 0. We can override the default value by explicitly assigning integer values to theenumerators.

enum colour { red, blue=4, green =8};

enum colour {red=5, blue, green};

continue… 

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 20/30

Learners Support Publications www.lsp4you.com

User-Defined Data Types

Enumerated Data Type:

C++ also permits the creation of anonymous

enum (i.e., enum with out tag name).enum {off, on}; here off 0 and on 1

int switch_1 = off;int switch_2 = on;

continue… 

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 21/30

Learners Support Publications www.lsp4you.com

Derived Data Types

 Arrays

 The application of arrays in C++ is similar to that

in C.Functions

top-down - structured programming ; to reduce

length of the program ; reusability ; function over-loading. 

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 22/30

Learners Support Publications www.lsp4you.com

Derived Data Types

Pointers

Pointers can be declared and initialized as in C.

int * ip;  // int pointerip = &x;  // address of x assigned to ip

*ip = 10;  // 10 assigned to x through indirection

continue… 

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 23/30

Learners Support Publications www.lsp4you.com

Derived Data Types

Pointers

C++ adds the concept of constant pointer and

pointer to a constant.char * const ptr1 = “GOODS”; // constant pointer 

int const * ptr2 = &m; // pointer to a constant

continue… 

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 24/30

Learners Support Publications www.lsp4you.com

Symbolic Constants

 Two ways of creating symbolic constant in C++.

Using the qualifier const

Defining a set of integer constants using  enum  keyword.

 Any value declared as const can not be modified by theprogram in any way. In C++, we can use const in a

constant expression.

const int size = 10;

char name[size]; // This is illegal in C.

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 25/30

Learners Support Publications www.lsp4you.com

Symbolic Constants

const allows us to create typed constants.

#define - to create constants that have no typeinformation.

 The named constants are just like variables except thattheir values can not be changed. C++ requires a const to

be initialized.

 A const in C++ defaults, it is local to the file where it isdeclared. To make it global the qualifier extern is used.

continue… 

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 26/30

Learners Support Publications www.lsp4you.com

Symbolic Constants

extern const int total = 100;

enum { X, Y, Z };

 This is equivalent to

const int X = 0;

const int Y = 1;

const int Z = 2;

continue… 

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 27/30

Learners Support Publications www.lsp4you.com

Reference Variables

 A reference variable provides an alias for apreviously defined variable.

For eg., if we make the variable sum a reference tothe variable total, then sum and total can be usedinterchangeably to represent that variable.

data-type & reference-name = variable-namefloat total = 100;

float &sum = total;

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 28/30

Learners Support Publications www.lsp4you.com

Reference Variables

 A reference variable must be initialized at the timeof declaration. This establishes the correspondencebetween the reference and the data object which itnames.

int x ;

int *p = &x ;int & m = *p ;

continue… 

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 29/30

Learners Support Publications www.lsp4you.com

Reference Variables

 void f ( int & x ){

x = x + 10;

}int main ( )

{

int m = 10;

f (m);

}

continue… 

 When the function call f(m) isexecuted,

int & x = m;

8/11/2019 Cbse Class 11 Chap 6,7

http://slidepdf.com/reader/full/cbse-class-11-chap-67 30/30

Learners Support Publications www lsp4you com

 Thank You