Basic of c &c++

22
C++ Data Types Built-In Type Integral type - int, char Floating type – float, double Void – Used to specify the return type of a function when it is not returning any value – To indicate an empty argument list to a function. Example : void fun (void);

description

a

Transcript of Basic of c &c++

Page 1: Basic of c &c++

C++ Data TypesBuilt-In Type

• Integral type - int, char

• Floating type – float, double

• Void– Used to specify the return type of a function

when it is not returning any value– To indicate an empty argument list to a

function. Example : void fun (void);

Page 2: Basic of c &c++

C++ Data Types

– Used in declaration of generic pointers.• Void *gp;• A generic pointer can be assigned a pointer

value of any basic data type, but it may not be dereferenced.

• For ex: int * ip; gp = ip; // assigns int pointer to void pointer.

• But, *ip = *gp; is illegal;– It would not make sense to dereference a pointer

to a void value.

LOOPS

Page 3: Basic of c &c++

C++ Data Types

User-defined type

1. Structure

2. Union

3. Class

4. Enumeration

Page 4: Basic of c &c++

C++ Data Types

• Structure and Union in C.

• They are used in C++ with some added features of object oriented languages.

• Class is used in c++.

• The class variables are known as objects.

Page 5: Basic of c &c++

Enumerated Data type

• It is user defined data type which provides a way for attaching names to numbers.

• It increases the comprehensibility of the code.

• The enum keyword automatically enumerates a list of words by assigning them values 0,1,2 and so on.

• It is used to create symbolic constants.

Page 6: Basic of c &c++

Enumerated Data type

• The enum statement is similar to the struct statement.

• Ex : enum shape {circle, square, triangle}

enum color{ red, blue, green, yellow}

• They differ from enum in C .

• In C++, they can be used as new type names.

• By using, tag names we can declare new variables.

• shape ellipse; // ellipse is of type shape.

Page 7: Basic of c &c++

Enumerated Data type

• ANSI C defines the types of enums to be ints.

• In c++, each enumerated data type retain its own separate type.

• C++ does not permit an int value to be automatically converted to an enum value.

• Ex : color background = blue; // allowed

• color background = 7; // error in c++

• color background = (color) 7; // ok

Page 8: Basic of c &c++

Enumerated Data type

• An enumerated value can be used in place of an int value.

Ex : int c = red; // valid, color type promoted to int

• By default, enumerators are assigned integer values starting from 0. We can override these values by :

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

Page 9: Basic of c &c++

Enumerated Data type

• C++ also permits the creation of anonymous enums ( enums with out tag names)

• Ex : enum {off, on};

int switch_1 = off;

Page 10: Basic of c &c++

Derived data types

Array : contiguous/ continuous memory space.

• Character arrays are initialized in different way:

• Ex in C :char string[3] = “xyz” // programmer intends to leave out the null character \0.

• Ex in C++ : char string[4] = “XYZ”

Page 11: Basic of c &c++

Derived data types

Function: A block of code that is to be executed to serve some purpose.

Pointers:

int * ip; // int pointer

ip = &x; // address of x assigned to p;

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

Page 12: Basic of c &c++

Difference Between The Two?

• Constant pointer

• Pointer to a constant

Page 13: Basic of c &c++

Constants

• Constants refer to fixed values that do not change during the execution of a program.

• They include integers, characters, floating point numbers and strings.

Literal constant do not have memory locations.

Examples:

• 123 // decimal integer

• 12.34 // floating point integer

• 037 // Octal integer

Page 14: Basic of c &c++

Constants

• 0x2 //hexadecimal integer• “C++” //string constant• ‘A’ // character constant• L’ab’ // wide-character constant• wchar-_t type is a wide-character literal

introduced by ANSI C++ and is used for character sets that cannot fit a character into a single byte.

• Wide-character literals begin with the letter L.

Page 15: Basic of c &c++

Symbolic constants

They can be created as follows:

1. Using the qualifier const, and

2. Defining a set of integer constants using enum keyword.

• Any value declared as const cannot be modified by the program in any way.

ex : Const int size = 10;• C++ requires a constant to be initialized.• ANSI C does not require an initialization, it will

give a default value 0.

Page 16: Basic of c &c++

Symbolic constants

• Enumeration example :

enum { x, y, z};

const x = 0;

const y = 1;

const z =2;

• Or explicitly, enum {x=100, y = 50,z=200};

Page 17: Basic of c &c++

Control Structures

Three control structures:

• Sequence structure ( straight line)

• Selection structure (branching)

• Loop structure ( iteration or repetition)

Page 18: Basic of c &c++

Selection structure (branching)

• Simple If statement

if (condition)

{ // code

}• If……else statement

If (condition)

{

// code

}

Else

{// code}

Page 19: Basic of c &c++

Switch statement• This is multiple branching statement, based

on a condition.• The control is transferred to one of the

many possible points.• Syntax: switch (expression){ case 1:{ //code}

Page 20: Basic of c &c++

Switch statement

case 2:

{ //code

}

Default:

{// code

}

}

Page 21: Basic of c &c++

LOOPS

Do While :exit controlled loop

Do

{

// code

}while (condition is true);

Page 22: Basic of c &c++

LOOPS

• While Loop : entry controlled loopwhile (condition is true){ // code}• For Loop : entry controlled and action will

repeated a predetermined no. of timesfor( initial value; test; increment){ // code}