68
1 Data Structure & Algorithm Pointer & Class

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

Embed Size (px)

Citation preview

Page 1: 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

1

Data Structure & Algorithm

Pointer & Class

Page 2: 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

2

Pointer & Class

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

Page 3: 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

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();};

Page 4: 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

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;}

Page 5: 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

5

Class Instance

We might use the class as follows:

student s1;

s1.set_info("KHUZAIMAH", 46);s1.print_info(); cout << endl;

Page 6: 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

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;

Page 7: 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

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;

Page 8: 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

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;

Page 9: 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

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;

Page 10: 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

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;

Page 11: 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

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;

Page 12: 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

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;

Page 13: 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

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;

Page 14: 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

14

Class Instance & Pointer

Below is the possible output for the program until the slide #13:

KHUZAIMAH - 46Address of s1 = 0xbffdb920 Value of p = 0xbffdb920 Address of p = 0xbffdb91c

Page 15: 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

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 - 46Address of s1 = 0xbffdb920 Value of p = 0xbffdb920 Address of p = 0xbffdb91c

Page 16: 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

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 - 46Address of s1 = 0xbffdb920 Value of p = 0xbffdb920 Address of p = 0xbffdb91c

Page 17: 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

17

Class Instance & Pointer

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name KHUZAIMAHmark 46set_info(n, m)print_info()

Below is the table of memory representing the current state for s1 and p

s1&s1

*p p&p

Page 18: 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

18

Class Instance & Pointer

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name KHUZAIMAHmark 46set_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

*p p&p

Page 19: 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

19

Class Instance & Pointer

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name KHUZAIMAHmark 46set_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

*p p&p

Page 20: 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

20

Class Instance & Pointer

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name KHUZAIMAHmark 46set_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

*p p&p

Page 21: 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

21

Access Class Member Function via Pointer

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name KHUZAIMAHmark 46set_info(n, m)print_info()

Is it possible to call print_info() method via *p?

s1.print_info()&s1

*p p&p

Page 22: 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

22

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name KHUZAIMAHmark 46set_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

Page 23: 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

23

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name KHUZAIMAHmark 46set_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

Page 24: 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

24

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name KHUZAIMAHmark 46set_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

Page 25: 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

25

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name KHUZAIMAHmark 46set_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

Page 26: 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

26

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name KHUZAIMAHmark 46set_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

Page 27: 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

27

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name KHUZAIMAHmark 46set_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

Page 28: 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

28

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name KHUZAIMAHmark 46set_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

Page 29: 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

29

Changing member data variables of s1 via p can be done as follows:

p->set_info(“ADAM”, 51);

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name KHUZAIMAHmark 46set_info(n, m)print_info()

Access Class Member Function via Pointer

Page 30: 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

30

Changing member data variables of s1 via p can be done as follows:

p->set_info(“ADAM”, 51);

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name KHUZAIMAHmark 46set_info(n, m)print_info()

Access Class Member Function via Pointer

Page 31: 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

31

Changing member data variables of s1 via p can be done as follows:

p->set_info(“ADAM”, 51);

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name KHUZAIMAHmark 46set_info(n, m)print_info()

Access Class Member Function via Pointer

Page 32: 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

32

Changing member data variables of s1 via p can be done as follows:

p->set_info(“ADAM”, 51);

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name KHUZAIMAHmark 46set_info(n, m)print_info()

Access Class Member Function via Pointer

Page 33: 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

33

Changing member data variables of s1 via p can be done as follows:

p->set_info(“ADAM”, 51);

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name ADAMmark 46set_info(n, m)print_info()

Access Class Member Function via Pointer

Page 34: 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

34

Changing member data variables of s1 via p can be done as follows:

p->set_info(“ADAM”, 51);

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name ADAMmark 46set_info(n, m)print_info()

Access Class Member Function via Pointer

Page 35: 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

35

Changing member data variables of s1 via p can be done as follows:

p->set_info(“ADAM”, 51);

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name ADAMmark 46set_info(n, m)print_info()

Access Class Member Function via Pointer

Page 36: 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

36

Changing member data variables of s1 via p can be done as follows:

p->set_info(“ADAM”, 51);

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name ADAMmark 46set_info(n, m)print_info()

Access Class Member Function via Pointer

Page 37: 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

37

Changing member data variables of s1 via p can be done as follows:

p->set_info(“ADAM”, 51);

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name ADAMmark 51set_info(n, m)print_info()

Access Class Member Function via Pointer

Page 38: 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

38

Changing member data variables of s1 via p can be done as follows:

p->set_info(“ADAM”, 51);

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name ADAMmark 51set_info(n, m)print_info()

Access Class Member Function via Pointer

Page 39: 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

39

Changing member data variables of s1 via p can be done as follows:

p->set_info(“ADAM”, 51);

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

. . . . . . . . . . . .

name ADAMmark 51set_info(n, m)print_info()

Access Class Member Function via Pointer

Page 40: 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

40

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

name ADAMmark 51set_info(n, m)print_info()

The simplified version of visual context for s1 and p

p->print_info()

Instance & Pointer Simplified Visual Context

Page 41: 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

41

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

name ADAMmark 51set_info(n, m)print_info()

The simplified version of visual context for s1 and p

p->print_info()

Instance & Pointer Simplified Visual Context

Page 42: 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

42

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

name ADAMmark 51set_info(n, m)print_info()

The simplified version of visual context for s1 and p

p->print_info()

s1ADAM

51

Instance & Pointer Simplified Visual Context

Page 43: 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

43

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

name ADAMmark 51set_info(n, m)print_info()

The simplified version of visual context for s1 and p

p->print_info()

s1ADAM

51

Instance & Pointer Simplified Visual Context

Page 44: 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

44

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

name ADAMmark 51set_info(n, m)print_info()

The simplified version of visual context for s1 and p

p->print_info()

s1ADAM

51

p.

Instance & Pointer Simplified Visual Context

Page 45: 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

45

Variable Name Type Address Value

. . . . . . . . . . . .

s1 student 0xbffdb920

p student * 0xbffdb91c 0xbffdb920

name ADAMmark 51set_info(n, m)print_info()

The simplified version of visual context for s1 and p

p->print_info()

s1ADAM

51

p.

Instance & Pointer Simplified Visual Context

Page 46: 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

46

Pointer & Class Instances Exercise

student *p, *p2, *temp;

Page 47: 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

47

Pointer & Class Instances Exercise

student *p, *p2, *temp;

p

.

p2

.

temp

.

Page 48: 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

48

Pointer & Class Instances Exercise

student *p, *p2, *temp;student s1;

p

.

p2

.

temp

.

Page 49: 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

49

Pointer & Class Instances Exercise

student *p, *p2, *temp;student s1;

p

.

p2

.

temp

.

s1

name?

mark?

Page 50: 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

50

Pointer & Class Instances Exercise

student *p, *p2, *temp;student s1;

s1.set_info(“ARRIF", 86);

p

.

p2

.

temp

.

s1

name?

mark?

Page 51: 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

51

Pointer & Class Instances Exercise

student *p, *p2, *temp;student s1;

s1.set_info(“ARRIF", 86);

p

.

p2

.

temp

.

s1

ARRIF

86

Page 52: 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

52

Pointer & Class Instances Exercise

student *p, *p2, *temp;student s1;

s1.set_info(“ARRIF", 86);

p = &s1; p

.

p2

.

temp

.

s1

ARRIF

86

Page 53: 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

53

Pointer & Class Instances Exercise

student *p, *p2, *temp;student s1;

s1.set_info(“ARRIF", 86);

p = &s1; p

.

p2

.

temp

.

s1

ARRIF

86

Page 54: 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

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

Page 55: 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

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?

Page 56: 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

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?

Page 57: 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

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?

Page 58: 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

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

Page 59: 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

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.

Page 60: 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

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:

Page 61: 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

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:

Page 62: 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

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:

Page 63: 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

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:

Page 64: 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

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:

Page 65: 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

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:

Page 66: 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

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:

Page 67: 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

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:

Page 68: 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

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: