1 Data Structure & Algorithm Pointer & Class. 2 Pointer can be used to store the address of other variables with types of int, char, float, and double.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
1 Abstract Data Types Chapter 1. 2 Objectives You will be able to: 1. Say what an abstract data type is. 2. Implement a simple abstract data type in C++
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Starting Out with C++, 3 rd Edition 1 Chapter 1. Introduction to Computers and Programming.
Multiple-Subscripted Array
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Programming Languages
Pointer Data Type and Pointer Variables
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Lecture 10 Inheritance “Absolute C++” Chapter 14.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
February 11, 2005 More Pointers Dynamic Memory Allocation.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 1 Structure Pointer Operator For accessing members in structures and classes we have.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic memory allocation and Pointers Lecture 4.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
11 Introduction to Object Oriented Programming (Continued) Cats.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Copyright © – Curt Hill Pointers A Light Introduction.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Lecture 01c: C++ review Topics: static arrays array / pointer connection dynamic arrays (and pointers) 2D arrays.
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.
11 Introduction to Object Oriented Programming (Continued) Cats.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
Fundamental Programming Fundamental Programming Introduction to Functions.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Software Engineering Algorithms, Compilers, & Lifecycle.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Objectives Identify the built-in data types in C++
CMSC202 Computer Science II for Majors Lecture 08 – Overloaded Constructors Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Learning Objectives Pointers Pointer in function call
Pointers and Pointer-Based Strings
void Pointers Lesson xx
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Variables with Memory Diagram
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Arrays November 8, 2017.
Linked List.
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
Pointers and Pointer-Based Strings
Dynamic Memory A whole heap of fun….
C Programming Lecture-8 Pointers and Memory Management
Java Programming Language
CS410 – Software Engineering Lecture #5: C++ Basics III
The Stack.
Presentation transcript:

1 Data Structure & Algorithm Pointer & Class

2 Pointer can be used to store the address of other variables with types of int, char, float, and double These types of data also known as primitive data types as it already exist/defined in C/C++ It’s possible to have a pointer that point to new data type (ADT) such as class defined by the user An introduction to class & pointer should has been explained briefly in the Abstract Data Type topic before

3 Class Definition Applying pointer to class required a new class to be defined first Below is an example of simple class definition for student class student { private: char name[10]; int mark; public: void set_info(char [], int); void print_info(); };

4 Class Implementation Next is the implementation for the student class void student::set_info(char n[], int m) { strcpy(name, n); mark = m; // reformat name int offset = 10 - strlen(name); if (offset > 0) { strncat(name, " ", offset); } void student::print_info() { cout << name << " - " << mark; }

5 Class Instance We might use the class as follows: student s1; s1.set_info("KHUZAIMAH", 46); s1.print_info(); cout << endl;

6 Class Instance We might use the class as follows: s1 is an instance of class ( student ) Like other primitive data types an instance of class also have address allocated in the memory student s1; s1.set_info("KHUZAIMAH", 46); s1.print_info(); cout << endl;

7 Class Instance Address Just follow the syntax: &var_name to get the address of s1 student s1; s1.set_info("KHUZAIMAH", 46); s1.print_info(); cout << endl; cout << "Address of s1 = " << &s1 << endl;

8 Class Instance & Pointer How to store the address of s1 into other variable (pointer)? student s1; s1.set_info("KHUZAIMAH", 46); s1.print_info(); cout << endl; cout << "Address of s1 = " << &s1 << endl;

9 Class Instance & Pointer Follow the rule that for pointer declare as T *P, P can only store address referred by other variables with type of T student s1; s1.set_info("KHUZAIMAH", 46); s1.print_info(); cout << endl; cout << "Address of s1 = " << &s1 << endl;

10 Class Instance & Pointer For the example below T is the new data type which is the class ( student ) and P can be any possible valid variable name let say p student s1; s1.set_info("KHUZAIMAH", 46); s1.print_info(); cout << endl; cout << "Address of s1 = " << &s1 << endl;

11 Class Instance & Pointer For the example below T is the new data type which is the class ( student ) and P can be any possible valid variable name let say p student s1; s1.set_info("KHUZAIMAH", 46); s1.print_info(); cout << endl; cout << "Address of s1 = " << &s1 << endl; student *p = &s1;

12 Class Instance & Pointer Add more few lines of code to get more information about s1 and p student s1; s1.set_info("KHUZAIMAH", 46); s1.print_info(); cout << endl; cout << "Address of s1 = " << &s1 << endl; student *p = &s1;

13 Class Instance & Pointer Add more few lines of code to get more information about s1 and p student s1; s1.set_info("KHUZAIMAH", 46); s1.print_info(); cout << endl; cout << "Address of s1 = " << &s1 << endl; student *p = &s1; cout << “ Value of p = " << p << endl; cout << "Address of p = " << &p << endl;

14 Class Instance & Pointer Below is the possible output for the program until the slide #13: KHUZAIMAH - 46 Address of s1 = 0xbffdb920 Value of p = 0xbffdb920 Address of p = 0xbffdb91c

15 Class Instance & Pointer Below is the possible output for the program until the slide #13: s1 is an instance of class ( student ) with its member variables name set to “ KHUZAIMAH ” and mark set to 46 KHUZAIMAH - 46 Address of s1 = 0xbffdb920 Value of p = 0xbffdb920 Address of p = 0xbffdb91c

16 Class Instance & Pointer Below is the possible output for the program until the slide #13: s1 is an instance of class ( student ) with its member variables name set to “ KHUZAIMAH ” and mark set to 46 p is a pointer with type of student and currently has the address of s1 as its value (it’s pointing to s1 ) KHUZAIMAH - 46 Address of s1 = 0xbffdb920 Value of p = 0xbffdb920 Address of p = 0xbffdb91c

17 Class Instance & Pointer Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameKHUZAIMAH mark46 set_info(n, m) print_info() Below is the table of memory representing the current state for s1 and p s1&s1 *pp&p

18 Class Instance & Pointer Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameKHUZAIMAH mark46 set_info(n, m) print_info() In the example code given there is a line s1.print_info(); which calling one of the method define in the class ( student ) s1&s1 *pp&p

19 Class Instance & Pointer Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameKHUZAIMAH mark46 set_info(n, m) print_info() In the example code given there is a line s1.print_info(); which calling one of the method define in the class ( student ) s1.print_info()&s1 *pp&p

20 Class Instance & Pointer Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameKHUZAIMAH mark46 set_info(n, m) print_info() In the example code given there is a line s1.print_info(); which calling one of the method define in the class ( student ) s1.print_info()&s1 *pp&p

21 Access Class Member Function via Pointer Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameKHUZAIMAH mark46 set_info(n, m) print_info() Is it possible to call print_info() method via *p ? s1.print_info()&s1 *pp&p

22 Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameKHUZAIMAH mark46 set_info(n, m) print_info() Will *p.print_info(); do the same as s1.print_info(); ? s1.print_info()&s1 p&p*p Access Class Member Function via Pointer

23 Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameKHUZAIMAH mark46 set_info(n, m) print_info() It will not do the same and give an error at compilation time, the correct one is: (*p).print_info(); s1.print_info()&s1 p&p*p Access Class Member Function via Pointer

24 Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameKHUZAIMAH mark46 set_info(n, m) print_info() It will not do the same and give an error at compilation time, the correct one is: (*p).print_info(); s1.print_info()&s1 p&p(*p).print_info() Access Class Member Function via Pointer

25 Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameKHUZAIMAH mark46 set_info(n, m) print_info() It will not do the same and give an error at compilation time, the correct one is: (*p).print_info(); s1.print_info()&s1 p&p(*p).print_info() Access Class Member Function via Pointer

26 Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameKHUZAIMAH mark46 set_info(n, m) print_info() There is another technique that more intuitive by changing it from (*p).print_info(); to p->print_info(); s1.print_info()&s1 p&p(*p).print_info() Access Class Member Function via Pointer

27 Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameKHUZAIMAH mark46 set_info(n, m) print_info() There is another technique that more intuitive by changing it from (*p).print_info(); to p->print_info(); s1.print_info()&s1 p&pp->print_info() Access Class Member Function via Pointer

28 Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameKHUZAIMAH mark46 set_info(n, m) print_info() Changing member data variables of s1 via p can be done as follows: p->set_info(“ADAM”, 51); Access Class Member Function via Pointer

29 Changing member data variables of s1 via p can be done as follows: p->set_info(“ADAM”, 51); Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameKHUZAIMAH mark46 set_info(n, m) print_info() Access Class Member Function via Pointer

30 Changing member data variables of s1 via p can be done as follows: p->set_info(“ADAM”, 51); Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameKHUZAIMAH mark46 set_info(n, m) print_info() Access Class Member Function via Pointer

31 Changing member data variables of s1 via p can be done as follows: p->set_info(“ADAM”, 51); Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameKHUZAIMAH mark46 set_info(n, m) print_info() Access Class Member Function via Pointer

32 Changing member data variables of s1 via p can be done as follows: p->set_info(“ADAM”, 51); Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameKHUZAIMAH mark46 set_info(n, m) print_info() Access Class Member Function via Pointer

33 Changing member data variables of s1 via p can be done as follows: p->set_info(“ADAM”, 51); Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameADAM mark46 set_info(n, m) print_info() Access Class Member Function via Pointer

34 Changing member data variables of s1 via p can be done as follows: p->set_info(“ADAM”, 51); Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameADAM mark46 set_info(n, m) print_info() Access Class Member Function via Pointer

35 Changing member data variables of s1 via p can be done as follows: p->set_info(“ADAM”, 51); Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameADAM mark46 set_info(n, m) print_info() Access Class Member Function via Pointer

36 Changing member data variables of s1 via p can be done as follows: p->set_info(“ADAM”, 51); Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameADAM mark46 set_info(n, m) print_info() Access Class Member Function via Pointer

37 Changing member data variables of s1 via p can be done as follows: p->set_info(“ADAM”, 51); Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameADAM mark51 set_info(n, m) print_info() Access Class Member Function via Pointer

38 Changing member data variables of s1 via p can be done as follows: p->set_info(“ADAM”, 51); Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameADAM mark51 set_info(n, m) print_info() Access Class Member Function via Pointer

39 Changing member data variables of s1 via p can be done as follows: p->set_info(“ADAM”, 51); Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb nameADAM mark51 set_info(n, m) print_info() Access Class Member Function via Pointer

40 Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb920 nameADAM mark51 set_info(n, m) print_info() The simplified version of visual context for s1 and p p->print_info() Instance & Pointer Simplified Visual Context

41 Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb920 nameADAM mark51 set_info(n, m) print_info() The simplified version of visual context for s1 and p p->print_info() Instance & Pointer Simplified Visual Context

42 Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb920 nameADAM mark51 set_info(n, m) print_info() The simplified version of visual context for s1 and p p->print_info() s1 ADAM 51 Instance & Pointer Simplified Visual Context

43 Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb920 nameADAM mark51 set_info(n, m) print_info() The simplified version of visual context for s1 and p p->print_info() s1 ADAM 51 Instance & Pointer Simplified Visual Context

44 Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb920 nameADAM mark51 set_info(n, m) print_info() The simplified version of visual context for s1 and p p->print_info() s1 ADAM 51 p. Instance & Pointer Simplified Visual Context

45 Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb920 nameADAM mark51 set_info(n, m) print_info() The simplified version of visual context for s1 and p p->print_info() s1 ADAM 51 p. Instance & Pointer Simplified Visual Context

46 Pointer & Class Instances Exercise student *p, *p2, *temp;

47 Pointer & Class Instances Exercise student *p, *p2, *temp; p. p2. temp.

48 Pointer & Class Instances Exercise student *p, *p2, *temp; student s1; p. p2. temp.

49 Pointer & Class Instances Exercise student *p, *p2, *temp; student s1; p. p2. temp. s1 name? mark?

50 Pointer & Class Instances Exercise student *p, *p2, *temp; student s1; s1.set_info(“ARRIF", 86); p. p2. temp. s1 name? mark?

51 Pointer & Class Instances Exercise student *p, *p2, *temp; student s1; s1.set_info(“ARRIF", 86); p. p2. temp. s1 ARRIF 86

52 Pointer & Class Instances Exercise student *p, *p2, *temp; student s1; s1.set_info(“ARRIF", 86); p = &s1; p. p2. temp. s1 ARRIF 86

53 Pointer & Class Instances Exercise student *p, *p2, *temp; student s1; s1.set_info(“ARRIF", 86); p = &s1; p. p2. temp. s1 ARRIF 86

54 Pointer & Class Instances Exercise student *p, *p2, *temp; student s1; s1.set_info(“ARRIF", 86); p = &s1; p2 = new student; p. p2. temp. s1 ARRIF 86

55 Pointer & Class Instances Exercise student *p, *p2, *temp; student s1; s1.set_info(“ARRIF", 86); p = &s1; p2 = new student; p. p2. temp. s1 ARRIF 86 ??? name? mark?

56 Pointer & Class Instances Exercise student *p, *p2, *temp; student s1; s1.set_info(“ARRIF", 86); p = &s1; p2 = new student; p. p2. temp. s1 ARRIF 86 ??? name? mark?

57 Pointer & Class Instances Exercise student *p, *p2, *temp; student s1; s1.set_info(“ARRIF", 86); p = &s1; p2 = new student; p2->set_info(“FIRDAUS”, 58); p. p2. temp. s1 ARRIF 86 ??? name? mark?

58 Pointer & Class Instances Exercise student *p, *p2, *temp; student s1; s1.set_info(“ARRIF", 86); p = &s1; p2 = new student; p2->set_info(“FIRDAUS”, 58); p. p2. temp. s1 ARRIF 86 ??? FIRDAUS 58

59 Pointer & Class Instances Exercise student *p, *p2, *temp; student s1; s1.set_info(“ARRIF", 86); p = &s1; p2 = new student; p2->set_info(“FIRDAUS”, 58); p. p2. temp. s1 ARRIF 86 ??? FIRDAUS 58 Write the next lines of code so the p and p2 will look like below.

60 Pointer & Class Instances Exercise student *p, *p2, *temp; student s1; s1.set_info(“ARRIF", 86); p = &s1; p2 = new student; p2->set_info(“FIRDAUS”, 58); p. p2. temp. s1 ARRIF 86 ??? FIRDAUS 58 The answer:

61 Pointer & Class Instances Exercise student *p, *p2, *temp; student s1; s1.set_info(“ARRIF", 86); p = &s1; p2 = new student; p2->set_info(“FIRDAUS”, 58); temp = p; p. p2. temp. s1 ARRIF 86 ??? FIRDAUS 58 The answer:

62 Pointer & Class Instances Exercise student *p, *p2, *temp; student s1; s1.set_info(“ARRIF", 86); p = &s1; p2 = new student; p2->set_info(“FIRDAUS”, 58); temp = p; p. p2. temp. s1 ARRIF 86 ??? FIRDAUS 58 The answer:

63 Pointer & Class Instances Exercise student *p, *p2, *temp; student s1; s1.set_info(“ARRIF", 86); p = &s1; p2 = new student; p2->set_info(“FIRDAUS”, 58); temp = p; p = p2; p. p2. temp. s1 ARRIF 86 ??? FIRDAUS 58 The answer:

64 Pointer & Class Instances Exercise student *p, *p2, *temp; student s1; s1.set_info(“ARRIF", 86); p = &s1; p2 = new student; p2->set_info(“FIRDAUS”, 58); temp = p; p = p2; p. p2. temp. s1 ARRIF 86 ??? FIRDAUS 58 The answer:

65 Pointer & Class Instances Exercise student *p, *p2, *temp; student s1; s1.set_info(“ARRIF", 86); p = &s1; p2 = new student; p2->set_info(“FIRDAUS”, 58); temp = p; p = p2; p2 = temp; p. p2. temp. s1 ARRIF 86 ??? FIRDAUS 58 The answer:

66 Pointer & Class Instances Exercise student *p, *p2, *temp; student s1; s1.set_info(“ARRIF", 86); p = &s1; p2 = new student; p2->set_info(“FIRDAUS”, 58); temp = p; p = p2; p2 = temp; p. p2. temp. s1 ARRIF 86 ??? FIRDAUS 58 The answer:

67 Pointer & Class Instances Exercise student *p, *p2, *temp; student s1; s1.set_info(“ARRIF", 86); p = &s1; p2 = new student; p2->set_info(“FIRDAUS”, 58); temp = p; p = p2; p2 = temp; temp = NULL; p. p2. temp. s1 ARRIF 86 ??? FIRDAUS 58 The answer:

68 Pointer & Class Instances Exercise student *p, *p2, *temp; student s1; s1.set_info(“ARRIF", 86); p = &s1; p2 = new student; p2->set_info(“FIRDAUS”, 58); temp = p; p = p2; p2 = temp; temp = NULL; p. p2. temp. s1 ARRIF 86 ??? FIRDAUS 58 The answer: