Inline Functions - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Inline Functions

Description:

Create the class 'queue' with inline functions. class queue { int q[100]; int sloc, rloc; ... class definition. Array of Objects (NOT an array) int main() queue ... – PowerPoint PPT presentation

Number of Views:808
Avg rating:3.0/5.0
Slides: 13
Provided by: lyon
Category:

less

Transcript and Presenter's Notes

Title: Inline Functions


1
Inline Functions
  • //Create the class "queue with inline functions
  • class queue
  • int q100
  • int sloc, rloc
  • public
  • queue()rloc sloc 0 cout ltlt "New Queue\n"
  • queue() cout ltlt "Queue Destroyed\n"
  • void qput(int i)
  • int qget()
  • // Notice that the function code is inside the
  • // class definition.

2
Array of Objects(NOT an array)
  • int main()
  • queue a, b, c
  • ...

a
b
c
3
Array of Objects
  • int main()
  • queue Q3
  • ...

Q0
Q1
Q2
4
Array of Objects
  • Q0.qput(1)
  • Q1.qput(2)
  • Q2.qput(3)
  • Q0.qput(11)
  • Q1.qput(12)
  • Q2.qput(13)

Q0
11
1
Q1
2
12
Q2
3
13
5
Pointers to Objects
  • queue q, Q3, p //p is a pointer
  • q.qput(0)
  • Q0.qput(1)
  • Q1.qput(2)
  • Q2.qput(3)
  • p q

p
q
0
Q0
1
Q1
2
Q2
3
6
Pointers to Objects
  • p q
  • cout ltlt p-gtqget()
  • p Q0
  • cout ltlt p-gtqget()
  • p
  • cout ltlt p-gtqget()
  • p
  • cout ltlt p-gtqget()

p
q
0
Q0
1
Q1
2
Q2
3
7
Pointers to Objects
  • p q
  • cout ltlt p-gtqget()
  • p Q0
  • cout ltlt p-gtqget()
  • p
  • cout ltlt p-gtqget()
  • p
  • cout ltlt p-gtqget()

p
q
0
Q0
1
Q1
2
Q2
3
8
Pointers to Objects
  • p q
  • cout ltlt p-gtqget()
  • p Q0
  • cout ltlt p-gtqget()
  • p
  • cout ltlt p-gtqget()
  • p
  • cout ltlt p-gtqget()

p
q
0
Q0
1
Q1
2
Q2
3
9
Pointers to Objects
  • p q
  • cout ltlt p-gtqget()
  • p Q0
  • cout ltlt p-gtqget()
  • p
  • cout ltlt p-gtqget()
  • p
  • cout ltlt p-gtqget()

p
q
0
Q0
1
Q1
2
Q2
3
10
Exercise
  • int count 0
  • //Create the class "obj"
  • class obj
  • public
  • obj() count cout ltlt count ltlt endl
  • obj()--count cout ltlt count ltlt endl
  • int id // Unique identifier for each object
  • void main()
  • obj A
  • cout ltlt "begin block" ltlt endl
  • obj B
  • cout ltlt "end block" ltlt endl

11
Exercise
  • int count 0
  • //Create the class "obj"
  • class obj
  • public
  • obj() count cout ltlt count ltlt endl
  • obj()--count cout ltlt count ltlt endl
  • int id // Unique identifier for each object
  • void main()
  • Instantiate an array of 3 objs
  • Declare an obj pointer p
  • Sequentially point p to each object as
  • you assign the obj a unique id
  • Sequentially print out each unique id

12
Exercise - a solution
  • class obj
  • public
  • obj() count cout ltlt count ltlt endl
  • obj()--count cout ltlt count ltlt endl
  • int id
  • void main()
  • obj A3, p
  • p A0
  • p-gtid 151
  • p
  • p-gtid 152
  • p
  • p-gtid 153
  • for (int i 0 i lt 3 i)
  • cout ltlt Ai.id ltlt endl
Write a Comment
User Comments (0)
About PowerShow.com