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

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

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

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

Defining and Converting Data

Copyright Kip Irvine, 2003

Last Update: 11/4/2003

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

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; // errorconst double z; // errorint m = n; m = 36;

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

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).

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

Storage Allocation Examples

string FileName;

void MySub( int N ){ double X; static int count;

int * pN = new int;}

static

automatic

dynamic

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

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 { ... }

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

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

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

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

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

26N

29A6

pN

(Address 29A6)

pN points to N because pN contains N's address

Implementing a PointerImplementing a Pointer

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

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!

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

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.

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

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.

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

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:

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

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.

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

Data Conversion

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

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...

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

Conversion Examples

int n = 26;double x = n;double x = 36;int b = x; // possible warningfloat f = 36.5; // possible warningbool isOK = 1; // possible warningint n = true;char ch = 26; // possible warningint w = 'A';

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

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...

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

Cast Examples

int n = (int)3.5; // traditional Cint w = int(3.5); // function-stylebool isOK = bool(15); char ch = char(86);string st = string("123");

// errors: no conversions available:int x = int("123"); string s = string(3.5);

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

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...

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

static_cast examples

int w = static_cast<int>(3.5);

bool isOK = static_cast<bool>(1);

char ch = static_cast<char>(86);

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

The End