Post on 25-May-2015
description
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);
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
C++ Data Types
User-defined type
1. Structure
2. Union
3. Class
4. Enumeration
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.
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.
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.
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
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};
Enumerated Data type
• C++ also permits the creation of anonymous enums ( enums with out tag names)
• Ex : enum {off, on};
int switch_1 = off;
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”
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.
Difference Between The Two?
• Constant pointer
• Pointer to a constant
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
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.
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.
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};
Control Structures
Three control structures:
• Sequence structure ( straight line)
• Selection structure (branching)
• Loop structure ( iteration or repetition)
Selection structure (branching)
• Simple If statement
if (condition)
{ // code
}• If……else statement
If (condition)
{
// code
}
Else
{// code}
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}
Switch statement
case 2:
{ //code
}
Default:
{// code
}
}
LOOPS
Do While :exit controlled loop
Do
{
// code
}while (condition is true);
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}