Lecture 7: Structure and Class structure solving quadric equations as an example class air-plane and bullets. Page 1 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng Computing the roots of a quadric equation The roots of a quadric equation ax2+bx+c=0 are x1= (-b+( b2-4ac))/2ac x2=x1= (-b- ( b2-4ac))/2ac void roots(float a, float b, float c) { if (b*b-4*a*c>=0) { x1=(-b+sqrt(b*b-4*a*c))/(2*a*c); x2 =(-b-sqrt(b*b-4*a*c))/(2*a*c); } else flag=1; } #include<iostream.h> float x1, x2; int flag=0; void roots(float a, float b, float c) void main(void) { x1, x2 and flag are used as global roots(1.0,2.0, 1.0); variables. Otherwise, function roots if(flag == 0) must return two values (we do not cout<<“The roots are”<<x1<<x2; know how to do it.) else cout<<“No real root”; Page 2 5/29/2016 } CS3369 Real Time Control Software/Wang Lusheng Define 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; Page 3 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng Define 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 (b*b-4*a*c>=0) { r.x1=(sqrt (b*b-4*a*c)-b)/(2*a*c); r.x2=(-sqrt(b*b-4*a*c)-b)/(2*a*c);} else x3=100; return r; } 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng Page 4 Using structures in main() • In main function, we can print the two roots as follows; #include <iostream.h> 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; z=roots(1.0, 2.0, 1.0); if (x3==100) cout<<“There is no real root”; else cout<<“The roots are”<< z.x1<<“ ”<<z. x2; } 5/29/2016 struct two_roots roots (float a, float b, float c) { struct two_roots r; if (b*b-4*a*c>=0) { r.x1=(sqrt (b*b-4*a*c)-b)/(2*a*c); r.x2=(-sqrt(b*b-4*a*c)-b)/(2*a*c); } else x3=100; return r; } CS3369 Real Time Control Software/Wang Lusheng Page 5 Time 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; _AH=0x2C; //service 0x2C for get time interrupt(0x21); //interrupt 0x21 i=_CH; j=_CL; k=_DH; l=_DL; tmp.hrs=i; tmp.mins=j; tmp.secs=k; tmp.hundth=l; return tmp; } void main() { struct the_time x; x=get_time(); cout<<“The hour is ”<<x.hrs<<“The minute is ”<<x.mins; cout<<“The second is ”<<x.sees; 5/29/2016 } CS3369 Real Time Control Software/Wang Lusheng Page 6 Problem Solving: Student Records • We define a structure that can hold student records structure student { char name[20]; int student_id; char grade; }; struct student x; void main() {int j; cout<<“Please enter your name with <=20 characters\n”; for(j=0; j<=19; j++) cin>>x.name[j]; cout<<“Please enter your student number\n”; cin>>x.student_id; cout<<“Please enter the grade (A, B, C, D, F) \n”; cin>>x.grade; } Question: How to print out those input records? 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng The program asks users to enter a record of students. Each record contains the name, student_id and grade. Page 7 Problem Solving: Student Records • We define a structure that can hold student records structure student { char name[20]; int student_id; char grade; }; struct student x[56]; void main() {int i,j; for(i=0; i<=55; i++) { cout<<“Please enter your name with <=20 characters\n”; for(j=0; j<=19; j++) cin>>x[i].name[j]; cout<<“Please enter your student number\n”; cin>>x[i].student_id; cout<<“Please enter the grade (A, B, C, D, F) \n”; cin>>x[i].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. Page 8 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng Classes • 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. Page 9 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng Classes (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. Page 10 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng Define air-plane as a class #include <graphics.h> #include<dos.h> #include<iostream.h> #include<conio.h> 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 Air::planeshow(int i,int k) { int j; circle(i+5, 200+size+k, size); circle(i+3, 200+2*size+k, size); for (j=0; j<=4+3*size; j=j+size) circle(i+j, 200+k, size); circle(i+5, 200-size+k, size); circle(i+3, 200-2*size+k, size); } 5/29/2016 void Air::ereasep(int i, int k) { int j; setcolor(BLACK); circle(i+5, 200+size+k, size); circle(i+3, 200+2*size+k, size); for (j=0; j<=4+3*size; j=j+1) circle(i+j, 200+k, size); circle(i+5, 200-size+k, size); circle(i+3, 200-2*size+k, size); } char read_key() { int y=1; char x; _AH=0x01; geninterrupt(0x16); y=_FLAGS&0x40; if(y == 0) { _AH=0x00; geninterrupt(0x16); x=_AL; return x; } return '?'; } void main(void) { int driver = DETECT,mode; int i,k,f=0,f3=0; char x; Air y1, y2, y3; initgraph(&driver,&mode,"a:\\bgi"); setcolor(WHITE); line(1,400,400,400); for ( i = 0; i < 80; i++ ) { setcolor(BLUE); y1.size=2; y1.planeshow(5*i, 5); x=read_key(); if (x =='u') f=f-8; if (x =='i') f=f+8; if (x=='o') f3=f3-8; if (x=='p') f3=f3+8; setcolor(YELLOW); y2.size=4; y2.planeshow(5*(i-8), f); setcolor(RED); y3.size=6;y3.planeshow(5*(i-16), f3); delay (300); y1.ereasep(5*i, 5); y2.ereasep(5*(i-8), f); y3.ereasep(5*(i-16), f3); } closegraph(); } CS3369 Real Time Control Software/Wang Lusheng Page 11 Define air-plane as a class #include<dos.h> #include <graphics.h> #include<iostream.h> #include<conio.h> class Air{ public: int size; void planeshow(int i,int k); void ereasep(int i, int k); void shoot(int i, int k); }; void Air::planeshow(int i,int k) { int j; circle(i+5, 200+size+k, size); circle(i+3, 200+2*size+k, size); for (j=0; j<=4+3*size; j=j+size) circle(i+j, 200+k, size); circle(i+5, 200-size+k, size); circle(i+3, 200-2*size+k, size); } void Air::ereasep(int i, int k) { int j; setcolor(BLACK); circle(i+5, 200+size+k, size); circle(i+3, 200+2*size+k, size); for (j=0; j<=4+3*size; j=j+1) circle(i+j, 200+k, size); circle(i+5, 200-size+k, size); circle(i+3, 200-2*size+k, size); } 5/29/2016 void main(void) { int driver = DETECT,mode; int sf=0, i,k,f=0,f3=0; char x; Air y1, y2, y3; void Air::shoot(int i, int k) {int j; sound(700); for(j=0; j<=10; j++) { setcolor(RED); line(i+j*30, 200+k,i+j*30+18, 200+k ); initgraph(&driver,&mode,"a:\\bgi"); delay (30); setcolor(WHITE); setcolor(BLACK); line(i+j*30, 200+k,i+j*30+18, line(1,400,400,400); 200+k); for ( i = 0; i < 120; i++ ) } { nosound(); setcolor(BLUE); y1.size=2; y1.planeshow(5*i, 5); } x=read_key(); if (x =='u') f=f-8; if (x =='i') f=f+8; char read_key() if (x=='o') f3=f3-8; { int y=1; if (x=='p') f3=f3+8; char x; setcolor(YELLOW); y2.size=3; _AH=0x01; y2.planeshow(5*(i-8), f); geninterrupt(0x16); setcolor(RED); y3.size=3; y=_FLAGS&0x40; y3.planeshow(5*(i-16), f3); if(y == 0) if(x=='k'){sf=1; y2.shoot(5*(i-8), f);} { if (sf==0) delay (300); _AH=0x00; else sf=0; geninterrupt(0x16); y1.ereasep(5*i, 5); y2.ereasep(5*(i-8), f); x=_AL; y3.ereasep(5*(i-16), f3); return x; } } return '?'; closegraph(); Page 12 } } CS3369 Real Time Control Software/Wang Lusheng Define air-plane and Shoot classes #include<dos.h> #include <graphics.h> #include<iostream.h> #include<conio.h> 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 Air::planeshow(int i,int k) { int j; circle(i+5, 200+size+k, size); circle(i+3, 200+2*size+k, size); for (j=0; j<=4+3*size; j=j+size) circle(i+j, 200+k, size); circle(i+5, 200-size+k, size); circle(i+3, 200-2*size+k, size); } void Air::ereasep(int i, int k) { int j; setcolor(BLACK); circle(i+5, 200+size+k, size); circle(i+3, 200+2*size+k, size); for (j=0; j<=4+3*size; j=j+1) circle(i+j, 200+k, size); circle(i+5, 200-size+k, size); circle(i+3, 200-2*size+k, size); } void Air::shoot(int i, int k) {int j; sound(700); for(j=0; j<=10; j++) { setcolor(RED); line(i+j*30, 200+k,i+j*30+18, 200+k ); delay (30); setcolor(BLACK); line(i+j*30, 200+k,i+j*30+18, 200+k); } nosound(); } Page 13 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng Define air-plane and Shoot classes class Shoot { public: void point(int i, int k); void sh(int i, int k); }; void Shoot::point(int i, int k) { setcolor(YELLOW); circle(400+i, 400+k, 1); } void Shoot::sh(int i, int k) {int j; sound(900); for(j=0; j<=10; j++) { setcolor(RED); line(400-j*30+i, 400+k-30*j,400-j*30+i-10, 400+k-30*j-10); delay (30); setcolor(BLACK); line(400-j*30+i, 400+k-30*j,400-j*30+i-10, 400+k-30*j-10); } nosound(); } char read_key(); void main(void) { int driver = DETECT,mode; int sf=0, i,k,f=0,f3=0, xi=0, yi=0; char x; Air y1, y2, y3; Shoot bullet; initgraph(&driver,&mode,"a:\\bgi"); setcolor(WHITE); line(1,400,400,400); for ( i = 0; i < 80; i++ ) { setcolor(BLUE); y1.size=2; y1.planeshow(5*i, 5); x=read_key(); if (x =='u') f=f-8; if (x =='i') f=f+8; if (x=='o') f3=f3-8; if (x=='p') f3=f3+8; setcolor(YELLOW); y2.size=3; y2.planeshow(5*(i-8), f); if(x=='k'){sf=1; y2.shoot(5*(i-8), f);} setcolor(RED); y3.size=3;y3.planeshow(5*(i-16), f3); bullet.point(xi,yi); if(x=='l') bullet.sh(xi,yi); if (sf==0) delay (300); else sf=0; y1.ereasep(5*i, 5); y2.ereasep(5*(i-8), f); y3.ereasep(5*(i-16), f3); } closegraph(); } char read_key() { int y=1; char x; _AH=0x01; geninterrupt(0x16); y=_FLAGS&0x40; if(y == 0) { _AH=0x00; geninterrupt(0x16); x=_AL; return x; } return '?'; } Page 14 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng Attributes of air-plane: Another definition of class Air void Air::ereasep(int i, int k) #include <graphics.h> { #include<iostream.h> int j; #include<conio.h> setcolor(BLACK); class Air{ circle(i+5, 200+size+k, size); public: circle(i+3, 200+2*size+k, size); int size; /** the size of the air-plane **/ for (j=0; j<=4+3*size; j=j+1) int i; /** the horizontal position **/ circle(i+j, 200+k, size); int k /** the vertical position **// circle(i+5, 200-size+k, size); void planeshow(); circle(i+3, 200-2*size+k, size); void ereasep(); } }; void Air::planeshow() { int j; circle(i+5, 200+size+k, size); circle(i+3, 200+2*size+k, size); for (j=0; j<=4+3*size; j=j+size) circle(i+j, 200+k, size); circle(i+5, 200-size+k, size); #include<dos.h> circle(i+3, 200-2*size+k, size); } 5/29/2016 char read_key() { int y=1; char x; _AH=0x01; geninterrupt(0x16); y=_FLAGS&0x40; if(y == 0) { _AH=0x00; geninterrupt(0x16); x=_AL; return x; } return '?'; } CS3369 Real Time Control Software/Wang Lusheng size, and position. void main(void) { int driver = DETECT,mode; int i,k,f=0,f3=0; char x; Air y1, y2, y3; initgraph(&driver,&mode,"a:\\bgi"); setcolor(WHITE); line(1,400,400,400); for ( i = 0; i < 80; i++ ) { x=read_key(); if (x =='u') f=f-8; if (x =='i') f=f+8; setcolor(BLUE); y1.size=2; y1.i=5*i; y1.k=f; y1.planeshow(); /*y1.planeshow(5*i, f); */ delay (300); y1.ereasep(); } closegraph(); } Page 15 Use 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 (x+1-2*y); Purposes: 1. Terminates the function. 2. Return a value to the parent function. Page 16 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng A tricky example of return statement #include<iostream.h> int f(int i); int f(int i) { if (i>3) void main() { { cout<<“good” int x; return i; } else x=f(5); {cout<<“bad”; } return -999; } } Page 17 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng Local 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. Page 18 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng An Example for Local Variables #include<iostream.h> int speed(int i) int speed(int i) { int main() int y; { int x, y=100; y=2.1*i-0.15*i*i; x=speed(10); cout<< “The value of y inside speed()”<<y; cout<< “The value of y inside main”<<y; cout<<“\n”; return 0; } return y; } Output: The value of y inside speed() 6 The value of y inside main 100 Page 19 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng Variables 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) Page 20 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng An Example for Local Variables #include<iostream.h> int speed(int i) int speed(int i) { int main() int y; { int x, y=100, j=10; y=2.1*i-0.15*i*i; x=speed(j); cout<< “The value of y inside speed()”<<y; cout<< “The value of y inside main”<<y; cout<<“\n”; return 0; return y; } } Output: The value of y inside speed() 6 The value of j is passed to speed(), not the variable j. The value of y inside main 100 Page 21 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng Global 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. #include<iostream.h> int g; void main() { int x,y; x=11; g=80; y= f(5); cout<<“The values of g , x and y in main()” cout<<g<<“ ”<< x<<“ ”<<y; } int f(int i) {int x=10; cout<<“The values of g and x in side f()”; cout<<g<<“ ” <<x<<“\n”; g=g+i; 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 Page 22 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng Another Example for Global Variables The roots of a quadric equation ax2+bx+c=0 are x1= (-b+( b2-4ac))/2ac x2=x1= (-b- ( b2-4ac))/2ac void roots(float a, float b, float c) { if (b*b-4*a*c>=0) { x1=(-b+sqrt(b*b-4*a*c))/(2*a*c); x2 =(-b-sqrt(b*b-4*a*c))/(2*a*c); } else flag=1; } include<iostream.h> float x1, x2; int flag=0; void roots(float a, float b, float c) void main(void) { x1, x2 and flag are used as global roots(1.0,2.0, 1.0); variables. Otherwise, roots must if(flag == 0) return two values that we do not cout<<“The roots are”<<x1<<x2; know how to do it. else cout<<“No real root”; Page 23 5/29/2016 } CS3369 Real Time Control Software/Wang Lusheng Global Variables vs Local Variables If global variables and local variables have the same name, the local variables are valid within their scopes. #include<iostream.h> int g=30; void main() { int x,y, g; /*g is a local variable */ x=11; g=80; y= f(5); cout<<“The values of g , x and y in main()” cout<<g<<“ ”<< x<<“ ”<<y; } int f(int i) {int x=10; cout<<“The values of g and x in side f()”; cout<<g<<“ ” <<x<<“\n”; /*g is a global variable */ g=g+i; 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 Page 24 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng while statement #include<iostream.h> void main() Syntax of while statement: { int a; cout<<“please enter an integer in [1,9]\n”; cin>>a; while (logic_expression) while (a<1 ||a>9) { { cout<<“please enter an integer in [1,9]\n”; statement1; cin>>a; } statement 2; } …. } (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) Page 25 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng Review and Exercises variables for statement while statement if statement switch statement (optional) while statement functions Page 26 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng Exercises 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? for (i=1; i<5; i++) #include<iostream.h> { void main() for(j=1; j<=3; j++) { cout<<i<<j; int i, j; cout<“\n”; } } 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng Page 27 Question 3. What are the outputs of the following program? Question 4. What are the outputs of the following program? #include<iostream.h> #include<iostream.h> void main() void main() { { int i = 10; int i, j; for (i=1; i<=5; i++) if(i<0) cout <<“negative”; { if (i>100) cout<<“too large”; for (j=1; j<=i; j++) if (i>=75 && i<=100) cout<<“ ”; /* There is one space*/ cout<<“excellent”; cout <<“**** \n”; } } } What if the initial value is 88? Page 28 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng Question 5. What are the outputs of the following program? Question 6. What are the outputs of the following program? #include<iostream.h> #include<iostream.h> void main() int max (int x, int y); { void main() int i; { i=1; int temp_max, temp_max1; while(i<=5) temp_max=max(x1,x2); { temp_max1=max(temp_max, x3); } } int x1=100, x2=102, x3=99; cout<<i; cout<<“The maximum is”; i=i+1; cout << temp_max1; } int max (int x, int y) { int temp=x; if (y>temp) temp=y; return temp; } 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng Page 29 Question 7. What are the outputs of the following program? #include<iostream.h> int max (int x, int y, int z); void main() { Question 8. What are the outputs of the following program? #include<iostream.h> int max (int x, int y, int z); void main() { int x1=100, x2=102, x3=99; temp_max=max(99, 102, 166); int temp_max; cout<<“The maximum is”; temp_max=max(x1,x2,x3); cout<<“The maximum is”; cout << temp_max; } cout << temp_max; } int max (int x, int y, int z) { int max (int x, int y, int z) { int temp=x; cout<<“The input numbers are”<<x<<y<<z<<“\n”; int temp=x; if (y>temp) temp=y; if (y>temp) temp=y; if(z>temp) temp=z; if(z>temp) temp=z; return temp; int temp_max; return temp; } } 5/29/2016 Page 30 CS3369 Real Time Control Software/Wang Lusheng Question 9. What are the outputs of the following program? #include<iostream.h> int computation(int x); void main() { int z, w, w1,x=10; /* x he is a variable that can be used in main() */ z=computation(5); w=computation(z); w1=computation(x); cout<<z<<w<<w1 } 5/29/2016 int computation(int x) /*x here is a formal parameter */ { int y; y=x*x+25; cout<<“input is”<<x<<“\n”; return y; } CS3369 Real Time Control Software/Wang Lusheng Page 31 Question 10. What are the outputs of the following program? int max () { int temp=x; #include<iostream.h> cout<<“The input numbers are”; int x, int y, int z; cout <<x<<y<<z<<“\n”; int max (); if (y>temp) temp=y; void main() { if(z>temp) temp=z; int temp_max; x=90; y=91; z=z+1; z=92; return temp; cout<<z<<“\n”; temp_max=max(); } cout<<“The maximum is”; cout << temp_max; cout<<z; } 5/29/2016 Page 32 CS3369 Real Time Control Software/Wang Lusheng 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. Page 33 5/29/2016 CS3369 Real Time Control Software/Wang Lusheng