unit 6 - object oriented programming / c++

17
 C++ Programming Structure s ( Part V)  QF002/6/ 1 UNIT 6 General Objective : To understand and apply the fundamental concept of pointers in C++ programming. Specific Objective : At the end of the unit you should be able to :  Describe the declarations of pointer.  Ex  plain how to declare and initialize pointer.  Use pointer in program.  Write and design a simple pointer program. OBJECTIVES C++ Programming Structures (Part V)  

Upload: syafiq-fauzi

Post on 07-Apr-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

8/6/2019 Unit 6 - Object Oriented Programming / C++

http://slidepdf.com/reader/full/unit-6-object-oriented-programming-c 1/17

 

C++ Programming Structures ( Part V)  QF002/6/1 

UNIT 6

General Objective : To understand and apply the fundamental

concept of pointers in C++ programming.

Specific Objective : At the end of the unit you should be able to :

 Describe the declarations of pointer.

  Ex plain how to declare and initialize pointer.

 Use pointer in program.

 Write and design a simple pointer program.

OBJECTIVES 

C++ Programming

Structures (Part V) 

8/6/2019 Unit 6 - Object Oriented Programming / C++

http://slidepdf.com/reader/full/unit-6-object-oriented-programming-c 2/17

 

C++ Programming Structures ( Part V)  QF002/6/2 

6.0 Introduction

6.1  Declaring Pointer

  A pointer is a variable which means that a pointer has a left value and

a right value. The left and right values are addressed. The left value of 

a pointer is used to refer to the pointer itself whereas the right value

of a pointer which is the content of the pointer is the address of 

another variable.  

INPUT

Pointer is a variable whose value is used to point to another variable. It is able to assign different values to a pointer variable

and the value contained by a pointer must be an address thatindicates the location of another variable in the memory. That iswhy a pointer is also called an address variable  

OHHHHH«.

8/6/2019 Unit 6 - Object Oriented Programming / C++

http://slidepdf.com/reader/full/unit-6-object-oriented-programming-c 3/17

 

C++ Programming Structures ( Part V)  QF002/6/3 

  The general form of a pointer declaration is that 

data type *pointer name;

  The data type specifies the type of data to which the pointer points.

  The pointer name is the name of the pointer variable

  The asterik (*) indicates the variable as a pointer.

The following shows the different types of pointer:

char *ptr_c ; // declare a pointer to a character 

int *ptr_int; ; // declare a pointer to a integer 

float *ptr_flt ; // declare a pointer to a float

Can a null pointer point to validdata ?

 No««

A Null pointer cannot point to

valid data. This is so because the

value contained by a null pointer is

0. «

8/6/2019 Unit 6 - Object Oriented Programming / C++

http://slidepdf.com/reader/full/unit-6-object-oriented-programming-c 4/17

 

C++ Programming Structures ( Part V)  QF002/6/4 

6.2  Array Of Pointer And Pointer To Array

  The elements of an array may be pointers. Here is an array of 4

 pointers to type double: 

  Their elements can be allocated like any other pointers:

 We can visualize this array like this:

double * p [ 4]

p[3] = new double ( 5.1234);

p

0

1

2

35.1234

noteA pointer is a special type of variable that contains the memory

address of another variables.

Why are pointers soimportant?Pointers are used to hold the address of 

objects on the free store.

8/6/2019 Unit 6 - Object Oriented Programming / C++

http://slidepdf.com/reader/full/unit-6-object-oriented-programming-c 5/17

 

C++ Programming Structures ( Part V)  QF002/6/5 

6.3  Pointer To Pointer

  A pointer may point to another pointer. For example:

 We can visualize these variable as shown below.

char x =¶t¶;

char *yx = &x;

char **yyx = &yx;

char ***yyyx = & yyx;

***yyyx = µw¶;

yyyx

yyx

yx

x

t

 F igure 6.1 A Model Showing Visualizing Pointer To Pointer  

Change the value of c to µw´

What is a pointer?

A pointer is a variable that

holds the address of another variable.

8/6/2019 Unit 6 - Object Oriented Programming / C++

http://slidepdf.com/reader/full/unit-6-object-oriented-programming-c 6/17

 

C++ Programming Structures ( Part V)  QF002/6/6 

6.4  Pointer To Functions

  Like an array name, a function name is actua lly a constant pointer.Wecan think of its value as the address of the code that implements the

function.

  A pointer to a function is simply a pointer whose value is the address

of the function name. Since that name itself is a pointer, a pointer to a

function is just a pointer to a constant pointer. For example :

 We can visualize the function pointer as shown in figure below.

int x (int)

int (* yx) (int);

yx =&x;

Declares the function x 

Declares the function pointer yx 

Assign the address of x to yx 

yx

x

Intx

(int n){

:::::::::::::::::::::::::::::

}

 F igure 6.2: A Model Showing Visualizing Pointer To  F unctions  

8/6/2019 Unit 6 - Object Oriented Programming / C++

http://slidepdf.com/reader/full/unit-6-object-oriented-programming-c 7/17

 

C++ Programming Structures ( Part V)  QF002/6/7 

  The value of function pointer is that they allow us to define function

of functions. This is done by passing a function pointer as a parameter 

to another function.

6.5  Introduction to Module Programs

  Is a process of developing programs in terms of subprograms or 

module.

  A module is a major task of a program or process. Source files are

also called modules. Data declarations and functions prototypes

can be placed in header files. This will enable any changes to the

declarations or the prototypes into header files to be automatically

signified to all source files including the header files.

 Modular programming is to break down the design of a program

into individual components that can be programmed and tested

independently. It is a requirement for effective development and

maintenance of large programs and projects.

 Modular programming has evolved into object-oriented

  programming, which provides formal rules for developing self -

contained software modules.

note The pointer provides an indirect way to get the value held at that

address.

8/6/2019 Unit 6 - Object Oriented Programming / C++

http://slidepdf.com/reader/full/unit-6-object-oriented-programming-c 8/17

 

C++ Programming Structures ( Part V)  QF002/6/8 

6.6  Modular with functions.

  Programmer can make the functions available to any program if 

 programmers separate the function declarations into a header file

and put the function definition in the source file.  

  Figure 6.4 below shows the example of the header file of 

module1.h 

1. // module : module1.h2. //purpose : interface for

module1.cpp

3. #ifndef _ MODULE1_H4. #define _MODULE1_H5. #inculde <stdlib.h>6. #include <ctype.h>7. #include <string.h>8. // function prototype9. extern char * LCase (char * str)10.  extern char * UCase (char * str)

11. # endif // _MODULE1_H

 F igure 6.3: An Example Of Modular Statement  

 N ote: In line 3, the preprocessor directive #ifndef  asks the question, ³has

the preprocessor identifier  _MODULE1_H been defined ?´ If not,

line 4 defines the identifier and everything in the file until the# 

endif directive is included. If the symbol _MODULE1_H has

already been defined, file inclusion jumps from the #ifdef directive

to the #endif  

Preprocessor directives for 

single inclusion.

End of single inclusion

8/6/2019 Unit 6 - Object Oriented Programming / C++

http://slidepdf.com/reader/full/unit-6-object-oriented-programming-c 9/17

 

C++ Programming Structures ( Part V)  QF002/6/9 

  Figure 6.4 below shows the example of the source code ( .cpp)  

#include ³module1.h´

char *Ucase ( char *str)

{

int len = strlen (str)

for (int i = 0; I < len ; i++)str[i] = toupper (str [i]);

return str;

}

char * Lcase (char *str){

int len = strlen (str);for (int i=0; i< len; i++)

str[i] = tolower(str [i]);

return str;

}

The function interface file

These functions areisolated because they

are in a file separatedfrom any other 

functions.

 F igure 6.4: An Example Of The Source Code 

noteModular design ± a method of programming that separates programcomponents into well-defined modules that can be used to hide data

and promote code reuse.

What is the difference between theaddress stored in a pointer and the

value at that address?

The address stored in the pointer is the

address of another variable. The value

stored at that address is any value stored in

any variable. The indirection operator (*)

returns the value stored at the address,

which itself is stored in the pointer.

8/6/2019 Unit 6 - Object Oriented Programming / C++

http://slidepdf.com/reader/full/unit-6-object-oriented-programming-c 10/17

 

C++ Programming Structures ( Part V)  QF002/6/10 

  Figure 6.5 below shows the simple drive program to test the

module1 function.

1. #include <iostream.h>2. #include ³module1.h´3. void main()4. {5. cout << ³ this ia a test \n´;6. cout << Ucase ( ³ this is a test of Ucase \n´)7. cout << Lcase ( ³ THIS IS A TEST OF LCASE´);8. }

 F igure 6.5: A Simple Drive Program 

 N ote: After including the module header file in line 2, the module1.cpp

module knows about Lcase () and Ucase and can use the functions

freely on lines 5 through 7.

This approach to programming is called module design and it

keeps data in the modules where it belongs.

8/6/2019 Unit 6 - Object Oriented Programming / C++

http://slidepdf.com/reader/full/unit-6-object-oriented-programming-c 11/17

 

C++ Programming Structures ( Part V)  QF002/6/11 

Test your comprehension before continuing to the next input.

Check your answers on the next page.

6.1.  Ex plain the difference between the following two uses of the

reference operator &:

int& r =n;

 p = &n;

6.2.  Ex plain the difference between the following two uses of the

reference operator *:

int* a =b;

c = *b;

6.3.  What is wrong with the following code ?

int& j = 33;

6.4.  What is wrong with the following code ?

Int * j = &33;

6.5.  What is wrong with the following code ?

char j = µa¶;

char k = &j;

 Activity 6  

8/6/2019 Unit 6 - Object Oriented Programming / C++

http://slidepdf.com/reader/full/unit-6-object-oriented-programming-c 12/17

 

C++ Programming Structures ( Part V)  QF002/6/12 

 Make sure you have tried to answer all the questions given. You can check 

 your answers with the answers below.

6.1.  The declaration int & r = n; declares r to be a reference (alias) for 

the int variable n. the assignment  p = &n; assigns the address of n

to the pointer p.

6.2.  The declaration int  *a = b; declares a to be a pointer (memory

address) pointing to the same int   to which c points. The

assignment c = * b; assigns to n the int to which b points.

6.3.  You cannot have a reference to a constant: it¶s address is not

accessible.

6.4.  The reference operator & cannot be applied to a constant.

6.5.  The variable k  has type char , while the ex pression &j  has type

 pointer to char . To initialize k  to &j , k would have to be declared

as type char * .

F eedback 6  

8/6/2019 Unit 6 - Object Oriented Programming / C++

http://slidepdf.com/reader/full/unit-6-object-oriented-programming-c 13/17

 

C++ Programming Structures ( Part V)  QF002/6/13 

  A pointer is a special type of variable that contains the memoryaddress of another variables.

  A Null pointer cannot point to valid data. This is so because thevalue contained by a null pointer is 0.

  A pointer to a function is simply a pointer whose value is the

address of the function name  

  Module Programs is a process of developing programs in terms of subprograms or module.  

  Modular design ± a method of programming that separates program components into well-defined modules that can be used tohide data and promote code reuse .

 K ey Facts 

8/6/2019 Unit 6 - Object Oriented Programming / C++

http://slidepdf.com/reader/full/unit-6-object-oriented-programming-c 14/17

 

C++ Programming Structures ( Part V)  QF002/6/14 

You are approaching success, please answer the questions below. If you

have any problems, please discuss it with your lecturer. Wish you good luck 

and all the best.

Question 6-1

a.  Determine the value of each of the indicated variable after the

following code executes. Assume that integers occupy 4 bytes and

that x is stored in memory starting at byte 0x3fffd00

int x = 44;

int *p = &x;

int & r = x;

int n = (*p) ++;

int *q = p -1;

r = * (-- p) +1;++ *q;

i.  x 

ii.  n

iii.  &x 

iv.  *p

v.  r 

vi.  *q

 b. What is wrong with the following code?

float x = 7.12345;

float *p = &x;

 S elf- Assessment  

8/6/2019 Unit 6 - Object Oriented Programming / C++

http://slidepdf.com/reader/full/unit-6-object-oriented-programming-c 15/17

 

C++ Programming Structures ( Part V)  QF002/6/15 

short d = 69;

short *q = &d;

 p = q;c.  What is wrong with the following code ?

int * x = new int;

int *y = new int;

cout << ³x =´ << x << ³, x + y = ³ << x + y << endl;

d.  Ex plain why the following three conditions are true for array  x and

int   y:

x[y] = = * (x +y);

* ( x + y) = = y [x];

x[y] = = y[x];

e.  Ex plain the difference between the following two declarations:

double * x j( );

double (* j) ( );

f.  Write a declaration for each of the following:

i.  An array of 8 floats;

ii.  An array of 8 pointers to a float;

iii.  A pointer to an array of 8 floats;

iv.  A pointer to an array of 8 pointers to a float;

v.  A function that returns a pointer to a float;

vi.  A function that returns a float;

vii.  A pointer to a function that returns a float;

viii.  A pointer to a function that returns a pointer to a float;

8/6/2019 Unit 6 - Object Oriented Programming / C++

http://slidepdf.com/reader/full/unit-6-object-oriented-programming-c 16/17

 

C++ Programming Structures ( Part V)  QF002/6/16 

 Make sure you have tried to answer all the questions given. You can check 

 your answers with the answers below 

Answer 6-1

a. i.  x = 46

ii.  n = 44

iii.  &x = 0x3fffd00

iv.  *p =46

v.  *r =46

vi.  *q =46

 b.  It is an error to add two pointers.

c.  The value  x[  y]  returned by the subscripting operator  [   ]  is the value

stored at the address computed from the ex pression  x  +  y. In that

ex pression, x is a pointer to its base type T and y is an int , so the offset

 y * sizeof  (T) is added to the address  x . The same evaluation would be

made from the ex pression  y +  x which is what would be used for  y [x  ]. 

d.  The declaration double *  j ( ); declares j to be a function that returns a

 pointer to double, the declaration double ( *  j  ) ( ); declares *  j  to be a

 pointer to a function that returns a double int .

Feedback On Self-Assessment

8/6/2019 Unit 6 - Object Oriented Programming / C++

http://slidepdf.com/reader/full/unit-6-object-oriented-programming-c 17/17

 

C++ Programming Structures ( Part V)  QF002/6/17 

e. i.  float a[8];

ii.  float * a[8];

iii.  float (*a)[8];

iv.  float * (* a)[8];

v.  float f ( );

vi.  float * f ( );

vii.  float (* f) ( );

viii.  float * (* f) ( );

CONGRATULATIONS

May success be with you

always««..