Upload
justina-hopkins
View
227
Download
0
Tags:
Embed Size (px)
Citation preview
1
Data Structure & Algorithm
Pointer & Class
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
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 - 46Address 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 - 46Address 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 - 46Address of s1 = 0xbffdb920 Value of p = 0xbffdb920 Address of p = 0xbffdb91c
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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: