Presentation is loading. Please wait.

Presentation is loading. Please wait.

The C++ Data Types Fundamental Data Types

Similar presentations


Presentation on theme: "The C++ Data Types Fundamental Data Types"— Presentation transcript:

1 The C++ Data Types Fundamental Data Types
Consists of the following: 1) Integers of different sizes Type Size(bytes) Type Size char int 2 or 4 short long 4 or 8 The above can be preceded by the key words signed or unsigned to specify signed or unsigned integers, e.g.., signed char (defines integer from -127 to 128) unsigned long (defines integers from 0 to (2^64-1) 2) Floating point numbers type Size in bytes float double long double The integer and floating point types can be preceded by the key word const to specify constants, e.g., const int Max=10; // constants must be initialized when declared.

2 The C++ Data Types Type Conversion
Implicit type conversion exists but it is a potential source of errors, e.g., int I; float a=10.5; I = a; // I equals 10 unsigned long x= ; I = x; // I is undefined Explicit type conversion using casting is done as follows: int I=2, j=5; float c; c = (float) I / (float) j; // floating point divide unsigned long x, int z = 10000; x = (unsigned long) z*z; Type conversion from a wider data type to narrower data type is unsafe. Derived Types Types can be derived using the following declaration operators: 1. * used to specify pointer type objects 2. & used to define references 3. [ ] used to defined arrays 4. () used to define functions other Derived Types can be defined using structs, classes, and enum

3 The C++ Data Types Pointers
are defined using: type_name * object_name; // object_name is a pointer type // object which can be used to point to objects of the particular type examples: int *pi; // data object pi is a pointer to an integer char **pc; // pc is a pointer to a pointer to a character float *v[10]; // v is an array of 10 pointers to float type objects int (*fp)(char, char*); // fp can be used to point to any function which // takes two arguments of type char, and char*, and returns a data value of type int Initializing pointers int* pi, I=10; // Watch out, I is only an integer, whereas pi is a pointer pi = & I; // pi now points to I. & is called the address of operator The above is the same as: int I=10, *pi=&I; // here pi is declared and initialized Objects can be referenced by their pointers as follows: *pi = I + 100; // same as I = I + 100; // *pi refers to object I which pi points to

4 The C++ Data Types References
are defined using: type &object_name= object_name; // object_name can become an alternative name or an alias for objects of the particular type examples: int I = 1; int &r = I; // I and r refer to the same integer type object The use of references is often found in specifying the types of arguments and return values for functions and overloaded operators. The following examples show the use of references as types of arguments or and return values of functions. Example 1: void f(double &A) { A += 3.14;} // f() updates the value of an object //of type double referenced by A, and does not return any values double d = 0.0; // declare and initialize object d f(d); // call f and pass it the reference of d //Compare the above with code using a pointer instead of a reference

5 The C++ Data Types . // function g() returns the reference of v[I]
Example 2: int v[20]; . // function g() returns the reference of v[I] int& g(int I) { return v[I];} . . main(){ // Assume that function main can not directly access the // elements of v[ ], it can do that thr’ calling g() g(3) = 7; // same as v[3] = 7 // Notice that a function call can be placed on the left side of an // assignment operator // the expression g(3) is called an lvalue since g() returns a // reference

6 The C++ Data Types Constant Pointers, Pointers to Constants, and References to Constants A constant pointer: int I; int *const cp = &I; // cp is initialized to point to I // cp can not point to any other object A pointer to a constant: const int ci = 10; const int* pc = &ci; // pc is variable pointer to a constant integer *pc = 15 // this gives an error, ci is a constant integer reference to a constant: const float pi = 3.141; const float& unit_circle_area = pi; A reference to a constant can refer to a variable data object double x; const double& xr = x; // xr can be a function argument // xr refers to x as a const data object of type double xr = 10.0; // gives error, where x = 10.0; x *= 100.0; // are OK

7 The C++ Data Types Arrays
are defined as follows type name[const expression] int v[10];// is OK, where int size; int v[size];// gives error, size is not const const int csize=10; double x[csize];// this is OK, elements are x[0] to x[csize-1] arrays of all types can be declared, except arrays of references: int& iar[10]; //gives error initializing an array: int a[ ] = {10,15,25}; double x[csize]={0.1,0.2};// csize = 10 char str[ ]=“C++”;// str[0] = ‘C’, str[1] = ‘+’, str[2] = “+, str[3] = ‘\0’ 2-dimensional arrays: double d[2][3] = { {0.0,0.1,0.2};// row 0 {0.4,0.5,0.6};// row 1}; arrays can be referenced and manipulated using pointers: int ar[10]; int *p=ar;// ar is the same as &ar[0], now p points to the array for(int i=0; i<10; i++){cout << “input an integer value \n”;// using iostream.h cin >> p[i];}// reads the value in ar[i]

8 The C++ Data Types Dynamic storage Allocation with the new and delete operators Pointers can be used to create arrays or objects of any size at run time with dynamic memory allocation The new operator: new type; allocates memory storage for an object and returns the address of that memory: new int; new double; for an array of objects: new type [number_of_elements]; new int [1000]; the return value from new is used to initialize pointers: double *pd = new double[100]; // allocates memory for 100 double elements int n = 1000; int *p = new int [n]; // notice that n is not a const int size; cout << “input array size”; cin >> size; float *pf = new float[size]; The delete operator: delete pointer_to_object; deallocates the storage of an object delete [ ] pointer_to_array; is used when deleting an array delete[ ] pd; delete [ ] pf; // for int, float, double, and char types [ ] is not needed delete is used to avoid memory leaks which occur when allocating storage for objects defined within a function and exiting the function.

9 The C++ Data Types typedef, enum, and void*
the typedef keyword is used to define new type specifiers, typedef char* String; String a = “hello”; typedef unsigned long Age; Age Stone_Age; the enum keyword is used to define discrete data types, enum Boolean {false,true}; Boolean unordered = false; enum Long_Work_Day { Monday = 1, Tuesday, Thursday}; Long_Work_Day X = 2; // X is initialized to Tuesday void* is used to define generic pointers, int ival, *pi = 0; char *pc = 0; void *pv; pv = pi ; pv = pc // OK, implicit // conversion, pv can be made to point to different variable data types pc = pv // Error, pc must only point to char type objects pc = (char*) pv ; // Ok, since pv is casted to point to char type objects const int *pci = &ival; pv = pci // Error, pci is a pointer to a const data type pv = (void *) pci // Ok, explicit casting away from constness


Download ppt "The C++ Data Types Fundamental Data Types"

Similar presentations


Ads by Google