Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 Data Structure & Algorithm Pointer & Class

2 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 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 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 5 Class Instance We might use the class as follows: student s1; s1.set_info("KHUZAIMAH", 46); s1.print_info(); cout << endl;

6 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 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 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 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 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 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 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 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 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 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 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 17 Class Instance & Pointer Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb920... 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 18 Class Instance & Pointer Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb920... 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 19 Class Instance & Pointer Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb920... 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 20 Class Instance & Pointer Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb920... 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 21 Access Class Member Function via Pointer Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb920... 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 22 Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb920... 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 23 Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb920... 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 24 Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb920... 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 25 Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb920... 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 26 Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb920... 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 27 Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb920... 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 28 Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb920... 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 29 Changing member data variables of s1 via p can be done as follows: p->set_info(“ADAM”, 51); Variable NameTypeAddressValue... s1student0xbffdb920 pstudent *0xbffdb91c0xbffdb920... nameKHUZAIMAH mark46 set_info(n, m) print_info() Access Class Member Function via Pointer

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

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

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

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

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

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

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

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

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

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

40 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 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 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 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 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 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 46 Pointer & Class Instances Exercise student *p, *p2, *temp;

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

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

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

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

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

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


Download ppt "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."

Similar presentations


Ads by Google