Presentation is loading. Please wait.

Presentation is loading. Please wait.

Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.

Similar presentations


Presentation on theme: "Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003."— Presentation transcript:

1 Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003

2 Const Qualifier The const qualifier prevents an object from begin modified after its initial definition. A const-qualified object must be given a value when it is defined. A const-qualified object can be assigned to a non-const object. const int n = 25; n = 36; // error const double z; // error int m = n; m = 36;

3 Storage Allocation A variable with static storage allocation exists for the lifetime of the program. –Global variables –Variables declared with static qualifier A variable with automatic storage is created on the stack when declared inside a code block. –Local function variables –Function parameters A variable with dynamic storage allocation exists on the heap, and my be both created and removed at runtime (new and delete operators).

4 Storage Allocation Examples string FileName; void MySub( int N ) { double X; static int count; int * pN = new int; } static automatic dynamic

5 Variable Scope A global variable exists outside of any code block. –a block is described by the {... } symbols A local variable exists inside a code block. –Inside a function, for example A member variable exists inside a class definition block –class name {... }

6 References A reference is an alias for some existing object. Physically, the reference stores the address of the object it references. In the following example, when we assign a value to rN, we automatically modify N: int N = 25; int & rN = N; rN = 36; cout << N; // "36" displayed

7 Pointers A pointer stores the address of some other object. The address-of (&) operator obtains the address of an object. int N = 26; int * pN = &N; // get the address

8 26 N 29A6 pN (Address 29A6) pN points to N because pN contains N's address Implementing a Pointer

9 Pointer Variables double Z = 26; int * pN; pN = &Z; // error! A pointer variable must be declared with the same type it points to. In the following, pN cannot point to Z because Z is a double: The internal format and size of a double is not the same as an integer!

10 Dereference Operator int N = 26; int * pN = &N; cout << *pN << endl; // "26" The dereference (*) operator obtains the contents of the variable that is referenced by a pointer. Only pointers can be dereferenced.

11 Dereference Operator int N = 26; int * pN = &N; *pN = 35; cout << *pN << endl; // "35" cout << N << endl; // "35" The dereference operator can also be used to modify the contents of the referenced variable. Assigning a value to *pN changes the value of N.

12 Assigning Pointers int N = 26; int * pN = &N; int * pZ; pZ = pN; *pZ = 35; // now N = 35 One pointer may be assigned to another, as long as they point to the same type. In this example, when pZ is dereferenced, it lets us change the value of N:

13 Assigning Pointers int N = 26; int * pN = &N; int Z = 0; int * pZ = &Z; *pZ = *pN; // Z = 26 Assigning one object to another can be done by dereferencing pointers.

14 Data Conversion

15 Implicit Conversion Implicit conversion can take place when an object of one type is assigned to an object of another type C++ handles conversions automatically for intrinsic numeric types Warning messages may appear when there is a danger of losing data. –They vary from one compiler to the next Examples...

16 Conversion Examples int n = 26; double x = n; double x = 36; int b = x; // possible warning float f = 36.5; // possible warning bool isOK = 1; // possible warning int n = true; char ch = 26; // possible warning int w = 'A';

17 Cast Operation A cast operation explicitly converts data from one type to another. It is used only for "safe" conversions that might be done by the compiler automatically. Use it to avoid compiler warning messages. The traditional cast operator from the C language puts the new data type in parentheses. C++ improves this with a function-style cast. Examples...

18 Cast Examples int n = (int)3.5; // traditional C int w = int(3.5); // function-style bool isOK = bool(15); char ch = char(86); string st = string("123"); // errors: no conversions available: int x = int("123"); string s = string(3.5);

19 static_cast<> The static_cast<> operator is the preferred way to perform "safe" conversions in C++. It replaces both the traditional C cast operator and the C++ function-style cast. Examples...

20 static_cast examples int w = static_cast (3.5); bool isOK = static_cast (1); char ch = static_cast (86);

21 The End


Download ppt "Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003."

Similar presentations


Ads by Google