Object oriented programming C++

Similar documents
Reference operator (&)

Pointers. Reference operator (&) ted = &andy;

12. Pointers Address-of operator (&)

[0569] p 0318 garbage

Object oriented programming C++

CMSC202 Computer Science II for Majors

Object oriented programming with C++

Homework #3 CS2255 Fall 2012

CSI33 Data Structures

Pointers, Dynamic Data, and Reference Types

DECLARAING AND INITIALIZING POINTERS

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Assist. Prof. Dr. Caner ÖZCAN

Introduction to C++ with content from

Goals of this Lecture

a data type is Types

First of all, it is a variable, just like other variables you studied

Introduction to Computer Science Midterm 3 Fall, Points

M.EC201 Programming language

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

SVG 216 Lecture Notes February 2012

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

What is an algorithm?

Basic Pointers. CSCI 112: Programming in C

BITG 1113: POINTER LECTURE 12

Fundamental of Programming (C)

M.CS201 Programming language

CS 2461: Computer Architecture I

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

(Refer Slide Time: 00:26)

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Pointers. Pointers. Pointers (cont) CS 217

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #49. Structures in C -1

Pointers. Memory. void foo() { }//return

CS201- Introduction to Programming Current Quizzes

Procedural Programming

Algorithms & Data Structures

Introduction to C: Pointers

SYSC 2006 C Winter 2012

Exam 3 Chapters 7 & 9

Scientific Computing

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

arrays review arrays and memory arrays: character array example cis15 advanced programming techniques, using c++ summer 2008 lecture # V.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

Computer Programming

Fundamentals of Programming Session 24

Procedural Programming & Fundamentals of Programming

UNIT- 3 Introduction to C++

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS )

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

EM108 Software Development for Engineers

Lecture 2, September 4

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

CSC 1300 Exam 4 Comprehensive-ish and Structs

Pointers. Pointer References

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Programming for Electrical and Computer Engineers. Pointers and Arrays

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Binghamton University. CS-211 Fall Pointers

Computer Programming

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

A brief introduction to C programming for Java programmers

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

Input And Output of C++

Object-Oriented Principles and Practice / C++

FORM 2 (Please put your name and form # on the scantron!!!!)

Language comparison. C has pointers. Java has references. C++ has pointers and references

ITP 342 Mobile App Dev. Fundamentals

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #33 Pointer Arithmetic

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

2 nd Week Lecture Notes

C introduction: part 1

Variables and literals

Pointers. 10/5/07 Pointers 1

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Dynamic Memory Allocation

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

9. Arrays. Compound Data Types: type name [elements]; int billy [5];

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook

In Java we have the keyword null, which is the value of an uninitialized reference type

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

What have we learned about when we learned about function parameters? 1-1

REFERENCES, POINTERS AND STRUCTS

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Transcription:

http://uranchimeg.com Object oriented programming C++ T.Uranchimeg Prof. Dr. Email uranchimeg@must.edu.mn Power Engineering School M.EC203* -- OOP (C++) -- Lecture 07

Subjects Pointers Pointer and array Pointer initialization Pointer to pointer Void Pointer Arithmetic of pointers M.EC203* -- OOP (C++) -- Lecture 07 2

Pointers The variable that stores the address of another variable is what we call a pointer. M.EC203* -- OOP (C++) -- Lecture 07 3

Example Pointer In the same way in which houses in a street are numbered, the operating system organizes the memory with unique and consecutive numbers, so if we talk about location 1776 in the memory, we know that there is only one location with that address and also that is between addresses 1775 and 1777. M.EC203* -- OOP (C++) -- Lecture 07 4

Address (dereference) operator (&) At the moment in which we declare a variable it must be stored in a concrete location in this succession of cells. We generally do not decide where the variable is to be placed - fortunately that is something automatically done by the compiler and the operating system at runtime. M.EC203* -- OOP (C++) -- Lecture 07 5

Definition But once the operating system has assigned an address there are some cases in which we may be interested in knowing where the variable is stored. This can be done by preceding the variable identifier by an ampersand sign (&), which literally means "address of". M.EC203* -- OOP (C++) -- Lecture 07 6

Example ted = &andy; would assign to variable ted the address of variable andy, since when preceding the name of the variable andy with the ampersand (&) character we are no longer talking about the content of the variable, but about its address in memory. M.EC203* -- OOP (C++) -- Lecture 07 7

Example We are going to suppose that andy has been placed in the memory address 1776 and that we write the following: andy = 25; fred = andy; ted = &andy; M.EC203* -- OOP (C++) -- Lecture 07 8

The result diagram M.EC203* -- OOP (C++) -- Lecture 07 9

Reference operator (*) Using a pointer we can directly access the value stored in the variable pointed by it just by preceding the pointer identifier with the reference operator asterisk (*), that can be literally translated to "value pointed by". M.EC203* -- OOP (C++) -- Lecture 07 10

Example continue beth = *ted; (that we could read as: "beth equal to value pointed by ted") beth would take the value 25, since ted is 1776, and the value pointed by 1776 is 25. M.EC203* -- OOP (C++) -- Lecture 07 11

The result diagram M.EC203* -- OOP (C++) -- Lecture 07 12

The important moment You must clearly differenciate that ted stores 1776, but *ted (with an asterisk * before) refers to the value stored in the address 1776, that is 25. M.EC203* -- OOP (C++) -- Lecture 07 13

Example beth = ted; // beth equal to ted ( 1776 ) beth = *ted; // beth equal to value //pointed by ted ( 25 ) M.EC203* -- OOP (C++) -- Lecture 07 14

& Operator of address or dereference (&) It is used as a variable prefix and can be translated as "address of", thus: &variable1 can be read as "address of variable1" M.EC203* -- OOP (C++) -- Lecture 07 15

* Operator of reference (*) It indicates that what has to be evaluated is the content pointed by the expression considered as an address. It can be translated by "value pointed by". * mypointer can be read as "value pointed by mypointer". M.EC203* -- OOP (C++) -- Lecture 07 16

Example continue andy = 25; ted = &andy; M.EC203* -- OOP (C++) -- Lecture 07 17

Declaring variables of type pointer type * pointer_name; where type is the type of data pointed, not the type of the pointer itself. M.EC203* -- OOP (C++) -- Lecture 07 18

The Example int * number; char * character; float * greatnumber; M.EC203* -- OOP (C++) -- Lecture 07 19

The program 8-1 M.EC203* -- OOP (C++) -- Lecture 07 20

Pointers and arrays In fact, the identifier of an array is equivalent to the address of its first element, like a pointer is equivalent to the address of the first element that it points to, so in fact they are the same thing M.EC203* -- OOP (C++) -- Lecture 07 21

The Example int numbers [20]; int * p; the following allocation would be valid: p = numbers; M.EC203* -- OOP (C++) -- Lecture 07 22

The difference At this point p and numbers are equivalent and they have the same properties, the only difference is that we could assign another value to the pointer p whereas numbers will always point to the first of the 20 integer numbers of type int with which it was defined. M.EC203* -- OOP (C++) -- Lecture 07 23

The Variable and constant So, unlike p, that is an ordinary variable pointer, numbers is a constant pointer M.EC203* -- OOP (C++) -- Lecture 07 24

The program 8-2 M.EC203* -- OOP (C++) -- Lecture 07 25

Pointer initialization int number; int *tommy = &number; this is equivalent to: int number; int *tommy; tommy = &number; M.EC203* -- OOP (C++) -- Lecture 07 26

Using pointer char * terry = "hello"; in this case static storage is reserved for containing "hello" and a pointer to the first char of this memory block is assigned to terry. M.EC203* -- OOP (C++) -- Lecture 07 27

Continue If we imagine that "hello" is stored at addresses 1702 and following, the previous declaration could be outlined thus: M.EC203* -- OOP (C++) -- Lecture 07 28

The diagram M.EC203* -- OOP (C++) -- Lecture 07 29

Using pointers terry[4] = '! ; *(terry+4) = '!'; M.EC203* -- OOP (C++) -- Lecture 07 30

Arithmetic of pointers Let's suppose that we have 3 pointers: char *mychar; short *myshort; long *mylong; and that we know that they point to memory locations 1000, 2000 and 3000 respectively. M.EC203* -- OOP (C++) -- Lecture 07 31

The increment mychar++; myshort++; mylong++; M.EC203* -- OOP (C++) -- Lecture 07 32

It is same mychar = mychar + 1; myshort = myshort + 1; mylong = mylong + 1; M.EC203* -- OOP (C++) -- Lecture 07 33

Pointers to pointers C++ allows the use of pointers that point to pointers, that these, in its turn, point to data. In order to do that we only need to add an asterisk (*) for each level of reference: M.EC203* -- OOP (C++) -- Lecture 07 34

The example char a; char * b; char ** c; a = 'z'; b = &a; c = &b; M.EC203* -- OOP (C++) -- Lecture 07 35

The Example this, supposing the randomly chosen memory locations of 7230, 8092 and 10502, could be described thus: M.EC203* -- OOP (C++) -- Lecture 07 36

The new thing The new thing in this example is variable c, which we can talk about in three different ways, each one of them would correspond to a different value: M.EC203* -- OOP (C++) -- Lecture 07 37

Example c is a variable of type (char **) with a value of 8092 *c is a variable of type (char*) with a value of 7230 **c is a variable of type (char) with a value of'z' M.EC203* -- OOP (C++) -- Lecture 07 38

void pointers The type of pointer void is a special type of pointer. void pointers can point to any data type, from an integer value or a float to a string of characters. M.EC203* -- OOP (C++) -- Lecture 07 39

The program 8-3 M.EC203* -- OOP (C++) -- Lecture 07 40

Pointers to functions C++ allows operations with pointers to functions. The greatest use of this is for passing a function as a parameter to another function, since these cannot be passed dereferenced. In order to declare a pointer to a function we must declare it like the prototype of the function except the name of the function is enclosed between parenthesis () and a pointer asterisk (*) is inserted before the name. M.EC203* -- OOP (C++) -- Lecture 07 41

The program 8-4 M.EC203* -- OOP (C++) -- Lecture 07 42

Have you questions? M.EC203* -- OOP (C++) -- Lecture 07 43

Summary Pointers Pointer and array Pointer initialization Pointer to pointer Void Pointer Arithmetic of pointers M.EC203* -- OOP (C++) -- Lecture 07 44

End of Thank you for ATTENTION M.EC203* -- OOP (C++) -- Lecture 07 45