Title: Lecture 7: Structure and Class
1Lecture 7 Structure and Class
- structure
- solving quadric equations as an example
- class
- air-plane and bullets.
2Computing the roots of a quadric equation
The roots of a quadric equation ax2bxc0 are
x1 (-b( b2-4ac))/2ac x2x1 (-b- (
b2-4ac))/2ac includeltiostream.hgt float x1,
x2 int flag0 void roots(float a, float b,
float c) void main(void) roots(1.0,2.0,
1.0) if(flag 0) coutltltThe
roots areltltx1ltltx2 else coutltltNo
real root
void roots(float a, float b, float c) if
(bb-4acgt0)
x1(-bsqrt(bb-4ac))/(2ac) x2
(-b-sqrt(bb-4ac))/(2ac) else
flag1 x1, x2 and flag are used
as global variables. Otherwise, function roots
must return two values (we do not know how to do
it.)
3Define structures
- Structures are aggregate data types built using
elements of other types. - We can use structure to group several variables
together
struct two_roots float x1
float x2 r
4Define a function of two_roots type
- The function roots returns a value of type
two_roots that contains two values of type float.
struct two_roots roots (float a, float b, float
c) struct two_roots r if (bb-4acgt0)
r.x1(sqrt (bb-4ac)-b)/(2ac)
r.x2(-sqrt(bb-4ac)-b)/(2ac) else
x3100 return r
5Using structures in main()
- In main function, we can print the two roots as
follows
include ltiostream.hgt int x3 struct two_roots
float x1 float x2
struct two_roots roots (float a,
float b, float c) void main(void) struct
two_roots z zroots(1.0, 2.0, 1.0)
if (x3100) coutltltThere is no real
root else coutltltThe roots areltlt
z.x1ltlt ltltz. x2
struct two_roots roots (float a, float b, float
c) struct two_roots r if (bb-4acgt0)
r.x1(sqrt (bb-4ac)-b)/(2ac)
r.x2(-sqrt(bb-4ac)-b)/(2ac)
else x3100 return r
6Time Services (Clock)
- struct the_time int hrs, int mins, int secs, int
hundth - struct the_time get_time()
- the_time tmp
- int i,j,k,l
- _AH0x2C //service 0x2C for get time
- interrupt(0x21) //interrupt 0x21
- i_CH j_CL k_DH l_DL
- tmp.hrsi tmp.minsj tmp.secsk tmp.hundthl
- return tmp
-
- void main()
- struct the_time x
- xget_time()
- coutltltThe hour is ltltx.hrsltltThe minute is
ltltx.mins - coutltltThe second is ltltx.sees
7Problem Solving Student Records
- We define a structure that can hold student
records
structure student char name20 int
student_id char grade struct student x void
main() int j coutltltPlease enter your
name with lt20 characters\n for(j0
jlt19 j) cingtgtx.namej
coutltltPlease enter your student number\n
cingtgtx.student_id coutltltPlease enter
the grade (A, B, C, D, F) \n
cingtgtx.grade Question How to print out
those input records?
The program asks users to enter a record of
students. Each record contains the name,
student_id and grade.
8Problem Solving Student Records
- We define a structure that can hold student
records
structure student char name20 int
student_id char grade struct student
x56 void main() int i,j for(i0 ilt55
i) coutltltPlease enter your name with
lt20 characters\n for(j0 jlt19
j) cingtgtxi.namej
coutltltPlease enter your student number\n
cingtgtxi.student_id coutltltPlease
enter the grade (A, B, C, D, F) \n
cingtgtxi.grade Question How to print out
those input records?
The program asks users to enter 104 records of
students. Each record contains the nemae,
student_id and grade.
9Classes
- Classes enable the programmer to model objects
that have attributes (represented as data
members) and behaviors or operations (represented
as member functions). - Object-oriented programming models real-world
objects with software counterparts--classes - Objects of the same class have the same
characteristics - e.g., cars, vehicles, air-planes, etc.
- OOP encapsulates data (attribute) and functions
(behavior) into packages called objects. - Objects have the property of information hiding.
10Classes (continued)
- information hiding objects may know how to
communicate with each other, but do not know how
other objects are implemented. - We can build software by combining standardized,
interchangeable parts --classes.
11Define air-plane as a class
void main(void) int driver
DETECT,mode int i,k,f0,f30 char
x Air y1, y2, y3 initgraph(driver,mode
,"a\\bgi") setcolor(WHITE)
line(1,400,400,400) for ( i 0 i lt 80 i
) setcolor(BLUE) y1.size2
y1.planeshow(5i, 5) xread_key() if (x 'u')
ff-8 if (x 'i') ff8 if (x'o')
f3f3-8 if (x'p') f3f38 setcolor(YELLOW)
y2.size4 y2.planeshow(5(i-8),
f) setcolor(RED) y3.size6y3.planeshow(5(i-16)
, f3) delay (300) y1.ereasep(5i, 5)
y2.ereasep(5(i-8), f) y3.ereasep(5(i-16),
f3) closegraph()
include ltgraphics.hgt includeltdos.hgt includeltio
stream.hgt includeltconio.hgt class Air public
int size void planeshow(int i,int k)
void ereasep(int i, int k) /i decides the
herizontal position / / k decides the
vertical position / void Airplaneshow(int
i,int k) int j circle(i5, 200sizek,
size) circle(i3, 2002sizek, size)
for (j0 jlt43size jjsize) circle(ij,
200k, size) circle(i5, 200-sizek,
size) circle(i3, 200-2sizek, size)
void Airereasep(int i, int k) int j
setcolor(BLACK) circle(i5, 200sizek,
size) circle(i3, 2002sizek, size)
for (j0 jlt43size jj1) circle(ij,
200k, size) circle(i5, 200-sizek, size)
circle(i3, 200-2sizek, size) char
read_key() int y1 char x
_AH0x01 geninterrupt(0x16)
y_FLAGS0x40 if(y 0)
_AH0x00 geninterrupt(0x16)
x_AL return x return '?'
12Define air-plane as a class
void main(void) int driver
DETECT,mode int sf0, i,k,f0,f30
char x Air y1, y2, y3
initgraph(driver,mode,"a\\bgi")
setcolor(WHITE) line(1,400,400,400)
for ( i 0 i lt 120 i )
setcolor(BLUE) y1.size2 y1.planeshow(5i,
5) xread_key() if (x 'u') ff-8 if (x
'i') ff8 if (x'o') f3f3-8 if (x'p')
f3f38 setcolor(YELLOW) y2.size3
y2.planeshow(5(i-8), f) setcolor(RED)
y3.size3 y3.planeshow(5(i-16),
f3) if(x'k')sf1 y2.shoot(5(i-8), f) if
(sf0) delay (300) else sf0 y1.ereasep(5i,
5) y2.ereasep(5(i-8), f) y3.ereasep(5(i-16),
f3) closegraph()
includeltdos.hgt include ltgraphics.hgt includeltio
stream.hgt includeltconio.hgt class Air public
int size void planeshow(int i,int k)
void ereasep(int i, int k) void shoot(int i,
int k) void Airplaneshow(int i,int k)
int j circle(i5, 200sizek, size)
circle(i3, 2002sizek, size) for (j0
jlt43size jjsize) circle(ij, 200k,
size) circle(i5, 200-sizek, size)
circle(i3, 200-2sizek, size) void
Airereasep(int i, int k) int j
setcolor(BLACK) circle(i5, 200sizek,
size) circle(i3, 2002sizek, size)
for (j0 jlt43size jj1) circle(ij,
200k, size) circle(i5, 200-sizek, size)
circle(i3, 200-2sizek, size)
void Airshoot(int i, int k) int j
sound(700) for(j0 jlt10 j)
setcolor(RED) line(ij30, 200k,ij3018,
200k ) delay (30) setcolor(BLACK)
line(ij30, 200k,ij3018, 200k)
nosound() char read_key() int y1
char x _AH0x01 geninterrupt(0x16)
y_FLAGS0x40 if(y 0)
_AH0x00 geninterrupt(0x16)
x_AL return x return '?'
13Define air-plane and Shoot classes
void Airereasep(int i, int k) int j
setcolor(BLACK) circle(i5, 200sizek,
size) circle(i3, 2002sizek, size)
for (j0 jlt43size jj1) circle(ij,
200k, size) circle(i5, 200-sizek, size)
circle(i3, 200-2sizek, size) void
Airshoot(int i, int k) int j sound(700)
for(j0 jlt10 j) setcolor(RED)
line(ij30, 200k,ij3018, 200k ) delay
(30) setcolor(BLACK) line(ij30,
200k,ij3018, 200k) nosound()
includeltdos.hgt include ltgraphics.hgt includeltio
stream.hgt includeltconio.hgt class Air public
int size void planeshow(int i,int k)
void ereasep(int i, int k) void shoot(int i,
int k) /i decides the herizontal position
/ / k decides the vertical position
/ void Airplaneshow(int i,int k) int
j circle(i5, 200sizek, size)
circle(i3, 2002sizek, size) for (j0
jlt43size jjsize) circle(ij, 200k,
size) circle(i5, 200-sizek, size)
circle(i3, 200-2sizek, size)
14Define air-plane and Shoot classes
void main(void) int driver
DETECT,mode int sf0, i,k,f0,f30, xi0,
yi0 char x Air y1, y2, y3 Shoot
bullet initgraph(driver,mode,"a\\bgi")
setcolor(WHITE) line(1,400,400,400)
for ( i 0 i lt 80 i )
setcolor(BLUE) y1.size2 y1.planeshow(5i,
5) xread_key() if (x 'u') ff-8 if (x
'i') ff8 if (x'o') f3f3-8 if (x'p')
f3f38 setcolor(YELLOW) y2.size3
y2.planeshow(5(i-8), f) if(x'k')sf1
y2.shoot(5(i-8), f) setcolor(RED)
y3.size3y3.planeshow(5(i-16),
f3) bullet.point(xi,yi) if(x'l')
bullet.sh(xi,yi) if (sf0) delay (300) else
sf0 y1.ereasep(5i, 5) y2.ereasep(5(i-8),
f) y3.ereasep(5(i-16), f3)
closegraph()
class Shoot public void point(int i,
int k) void sh(int i, int k) void
Shootpoint(int i, int k) setcolor(YELLOW)
circle(400i, 400k, 1) void Shootsh(int i,
int k) int j sound(900) for(j0 jlt10
j) setcolor(RED) line(400-j30i,
400k-30j,400-j30i-10, 400k-30j-10) delay
(30) setcolor(BLACK) line(400-j30i,
400k-30j,400-j30i-10, 400k-30j-10)
nosound() char read_key()
char read_key() int y1 char x
_AH0x01 geninterrupt(0x16)
y_FLAGS0x40 if(y 0)
_AH0x00 geninterrupt(0x16)
x_AL return x return '?'
15Another definition of class Air
Attributes of air-plane size, and position.
void main(void) int driver
DETECT,mode int i,k,f0,f30 char
x Air y1, y2, y3 initgraph(driver,mode
,"a\\bgi") setcolor(WHITE)
line(1,400,400,400) for ( i 0 i lt 80 i
) xread_key() if
(x 'u') ff-8 if (x 'i') ff8
setcolor(BLUE) y1.size2
y1.i5i y1.kf y1.planeshow()
/y1.planeshow(5i, f) / delay
(300) y1.ereasep()
closegraph()
void Airereasep(int i, int k) int j
setcolor(BLACK) circle(i5, 200sizek,
size) circle(i3, 2002sizek, size)
for (j0 jlt43size jj1) circle(ij,
200k, size) circle(i5, 200-sizek, size)
circle(i3, 200-2sizek, size) char
read_key() int y1 char x
_AH0x01 geninterrupt(0x16)
y_FLAGS0x40 if(y 0)
_AH0x00 geninterrupt(0x16)
x_AL return x return '?'
include ltgraphics.hgt includeltiostream.hgt inclu
deltconio.hgt class Air public int size /
the size of the air-plane / int i /
the horizontal position / int k / the
vertical position // void planeshow()
void ereasep() void Airplaneshow()
int j circle(i5, 200sizek, size)
circle(i3, 2002sizek, size) for (j0
jlt43size jjsize) circle(ij, 200k,
size) circle(i5, 200-sizek,
size) includeltdos.hgt circle(i3,
200-2sizek, size)
16Use of return statement
If the type of the function is not void, at least
one return statement must be used. The syntax of
a return statement is return value Here
value can be a number, variable or math
expression. Examples return 0 return x
return (x1-2y) Purposes 1. Terminates the
function. 2. Return a value to the parent
function.
17A tricky example of return statement
int f(int i) if (igt3) coutltltgood
return i else coutltltbad
return -999
includeltiostream.hgt int f(int i) void main()
int x xf(5)
18Local Variables
- Variables declared within the body of a function
are its local variables (we also say the scope of
these variables is the body of this function.)
(Look at an example) - Variables declared within the body of the main()
function of a program is local to the main()
function. - A local variable is completely unknown outside
its scope. Two local variables of different
scopes may have the same name.
19 An Example for Local Variables
includeltiostream.hgt int speed(int i) int
main() int x, y100 xspeed(10)
coutltlt The value of y inside mainltlty return
0
int speed(int i) int y
y2.1i-0.15ii coutltlt The value of y
inside speed()ltlty coutltlt\n return
y Output The value of y inside speed() 6 The
value of y inside main 100
20Variables in parameter list
- Variables declared in the parameter list of a
function are called formal variables. Their
scope is the body of this function. - call-by-value mechanism by default, when a
function is called, the value of the arguments
are plugged in the formal parameters. More
specifically, if the arguments are variables,
their values not the variables themselves are
plugged in. - call-by-reference mechanism (will not covered in
this course)
21 An Example for Local Variables
includeltiostream.hgt int speed(int i) int
main() int x, y100, j10 xspeed(j)
coutltlt The value of y inside
mainltlty return 0
int speed(int i) int y
y2.1i-0.15ii coutltlt The value of y
inside speed()ltlty coutltlt\n return
y Output The value of y inside speed() 6 The
value of y inside main 100
The value of j is passed to speed(), not the
variable j.
22Global Variables
- Global variable universal for the program,
declared in the program outside all functions. It
can be used and changed everywhere (by any
function). - Try to avoid using global variables.
includeltiostream.hgt int g void main() int
x,y x11 g80 y f(5)
coutltltThe values of g , x and y in main()
coutltltgltlt ltlt xltlt ltlty
int f(int i) int x10 coutltltThe values of
g and x in side f() coutltltgltlt
ltltxltlt\n ggi return g OUTPUT The
values of g and x in side f() 80 10 The values of
g , x and y in main() 85 11 85
23Another Example for Global Variables
The roots of a quadric equation ax2bxc0 are
x1 (-b( b2-4ac))/2ac x2x1 (-b- (
b2-4ac))/2ac includeltiostream.hgt float x1,
x2 int flag0 void roots(float a, float b,
float c) void main(void) roots(1.0,2.0,
1.0) if(flag 0) coutltltThe
roots areltltx1ltltx2 else coutltltNo
real root
void roots(float a, float b, float c) if
(bb-4acgt0)
x1(-bsqrt(bb-4ac))/(2ac) x2
(-b-sqrt(bb-4ac))/(2ac) else
flag1 x1, x2 and flag are used
as global variables. Otherwise, roots must return
two values that we do not know how to do it.
24Global Variables vs Local Variables
- If global variables and local variables have the
same name, the local variables are valid within
their scopes.
includeltiostream.hgt int g30 void main()
int x,y, g /g is a local variable / x11
g80 y f(5) coutltltThe values of g
, x and y in main() coutltltgltlt ltlt xltlt
ltlty
int f(int i) int x10 coutltltThe values of
g and x in side f() coutltltgltlt
ltltxltlt\n /g is a global variable /
ggi return g OUTPUT The values of g and
x in side f() 30 10 The values of g , x and y in
main() 80 11 35
25while statement
includeltiostream.hgt void main() int a
coutltltplease enter an integer in 1,9\n
cingtgta while (alt1 agt9)
coutltltplease enter an integer in
1,9\n cingtgta
(Demo the program in the lecture.The question
was given in Lab2 as extra exercise since you
have not learned while. It can be done using for
in a dirty way)
Syntax of while statement while
(logic_expression) statement1 statement
2 .
26Review and Exercises
- variables
- for statement
- while statement
- if statement
- switch statement (optional)
- while statement
- functions
27Exercises
Question 1 Which of the following words can be
used as variable names? 1x11, x11, xx12, _abc,
name_variable, for, if, for1, 1while. Question 2.
What are the outputs of the following
program? includeltiostream.hgt void main() int
i, j
for (i1 ilt5 i) for(j1
jlt3 j) coutltltiltltj
coutlt\n
28Question 3. What are the outputs of the following
program? includeltiostream.hgt void main() int i
10 if(ilt0) cout ltltnegative if
(igt100) coutltlttoo large if (igt75
ilt100) coutltltexcellent
What if the initial value is 88?
Question 4. What are the outputs of the following
program? includeltiostream.hgt void main()
int i, j for (i1 ilt5 i)
for (j1 jlti j)
coutltlt / There is one space/
cout ltlt \n
29Question 5. What are the outputs of the following
program? includeltiostream.hgt void main() int
i i1 while(ilt5)
coutltlti ii1
Question 6. What are the outputs of the following
program? includeltiostream.hgt int max (int x, int
y) void main() int x1100, x2102, x399
int temp_max, temp_max1
temp_maxmax(x1,x2) temp_max1max(temp_ma
x, x3) coutltltThe maximum is
cout ltlt temp_max1 int max (int x, int y) int
tempx if (ygttemp) tempy return
temp
30 Question 7. What are the outputs of the following
program? includeltiostream.hgt int max (int x, int
y, int z) void main() int x1100, x2102,
x399 int temp_max
temp_maxmax(x1,x2,x3) coutltltThe
maximum is cout ltlt temp_max int max
(int x, int y, int z) int tempx if
(ygttemp) tempy if(zgttemp) tempz
return temp
Question 8. What are the outputs of the following
program? includeltiostream.hgt int max (int x, int
y, int z) void main() int temp_max
temp_maxmax(99, 102, 166)
coutltltThe maximum is cout ltlt
temp_max int max (int x, int y, int z)
int tempx coutltltThe input numbers
areltltxltltyltltzltlt\n if (ygttemp) tempy
if(zgttemp) tempz return temp
31Question 9. What are the outputs of the following
program? includeltiostream.hgt int computation(int
x) void main() int z, w, w1,x10
/ x he is a variable that can be used in main()
/ zcomputation(5)
wcomputation(z) w1computation(x)
coutltltzltltwltltw1
int computation(int x) /x here is a formal
parameter / int y yxx25
coutltltinput isltltxltlt\n return y
32Question 10. What are the outputs of the
following program? includeltiostream.hgt int x,
int y, int z int max () void main() int
temp_max x90 y91 z92
coutltltzltlt\n temp_maxmax()
coutltltThe maximum is cout ltlt
temp_max coutltltz
int max () int tempx coutltltThe input
numbers are cout ltltxltltyltltzltlt\n if
(ygttemp) tempy if(zgttemp) tempz
zz1 return temp
33 Question 11. (Moderate) Write a function that
takes three integers as its input parameters and
outputs the smallest number among the three
integers. The prototype of the function is as
follows int minmum(int x, int y, int
z) Question 12. (Hard) Write a program that (1)
asks the user to input an integer from the
screen, (2) the input integer should be in the
range 0, 100 or 200, 300, and (3) if the
input integer is not in the required ranges, ask
the user to re-enter the integer until the
integer is in the required ranges.