Lecture 6: Structure and Class

Slides:



Advertisements
Similar presentations
Starting Out with C++, 3 rd Edition 1 Chapter 11 – Structured Data Abstract data types (ADTs) are data types created by the programmer. ADTs have their.
Advertisements

Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
Structures Spring 2013Programming and Data Structure1.
 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 11: Objects.
1 Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
1 C++ Structures Starting to think about objects...
Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams.
Introduction to Objects A way to create our own types.
CSC 270 – Survey of Programming Languages C++ Lecture 3 - Introducing Objects.
CSC 212 Object-Oriented Programming and Java Part 1.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
C++ Lecture 4 Tuesday, 15 July Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Basics of Most C++ Programs // Programmer: Clayton Price date: 9/4/ // File: fahr2celc.cpp 03. // Purpose:
Abstraction ADTs, Information Hiding and Encapsulation.
Object Oriented Programming A new way of thinking.
Struct 1. Definition: Using struct to define a storage containing different types. For example it can contain int, char, float and array at the same time.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Recursion. Problem decomposition Problem decomposition is a common technique for problem solving in programming – to reduce a large problem to smaller.
Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism © 2011 | PN AZRINA.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Array Sort. Sort Pass 1 Sort Pass 2 Sort Pass 3.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.
 The Object Oriented concepts was evolved for solving complex problems. Object- oriented software development started in the 1980s. Object-oriented design.
Basic concepts of C++ Presented by Prof. Satyajit De
Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.
Unified Modeling Language (UML)
MT262A Review.
Classes and Data Abstraction
Inheritance CMSC 202, Version 4/02.
Review: Two Programming Paradigms
Objective Revision of data structures and their implementation.
C++ Arrays.
The slides must be understood in Lecture 5
C++ Classes & Object Oriented Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CS212: Object Oriented Analysis and Design
Returning Structures Lesson xx
CS148 Introduction to Programming II
COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Lecture 5: Interrupts in Turbo C++
Lecture 7: Structure and Class
Procedural Programming
CS150 Introduction to Computer Science 1
Passing objects As arguments
Lecture 12 Oct 16, 02.
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
Starting to think about objects...
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CPS120: Introduction to Computer Science
COP 3330 Object-oriented Programming in C++
Introduction to Classes and Objects
CS150 Introduction to Computer Science 1
CS3369 Realtime Control Computer Software/DENG Xiaotie
CS3369 Real Time Control Software/DENG Xiaotie
CPS120: Introduction to Computer Science
Types of Computer Languages
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
CSCE 206 Lab Structured Programming in C
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CS150 Introduction to Computer Science 1
Presentation transcript:

Lecture 6: Structure and Class solving quadric equations as an example class air-plane and bullets. 4/29/2019 CS3369 Real Time Control Software/Wang Lusheng

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 #include<iostream.h> float x1, x2; int flag=0; void roots(float a, float b, float c) void main(void) { roots(1.0,2.0, 1.0); if(flag == 0) cout<<“The roots are”<<x1<<x2; else cout<<“No real root”; } 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; x1, x2 and flag are used as global variables. Otherwise, function roots must return two values (we do not know how to do it.) 4/29/2019 CS3369 Real Time Control Software/Wang Lusheng

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; 4/29/2019 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) { 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; } 4/29/2019 CS3369 Real Time Control Software/Wang Lusheng

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) { 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; } struct two_roots roots (float a, float b, float c) { 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; 4/29/2019 CS3369 Real Time Control Software/Wang Lusheng

CS3369 Real Time Control Software/Wang Lusheng Time Services (Clock) struct the_time {int hrs, int mins, int secs, int hundth;}; 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() { the_time x; x=get_time(); cout<<“The hour is ”<<x.hrs<<“The minute is ”<<x.mins; cout<<“The second is ”<<x.sees; 4/29/2019 CS3369 Real Time Control Software/Wang Lusheng

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? The program asks users to enter a record of students. Each record contains the name, student_id and grade. 4/29/2019 CS3369 Real Time Control Software/Wang Lusheng

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. 4/29/2019 CS3369 Real Time Control Software/Wang Lusheng

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. 4/29/2019 CS3369 Real Time Control Software/Wang Lusheng

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. 4/29/2019 CS3369 Real Time Control Software/Wang Lusheng

Define air-plane as a class 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(); #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); } 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; x=_AL; return x; } return '?'; 4/29/2019 CS3369 Real Time Control Software/Wang Lusheng

Define air-plane as a class void main(void) { int driver = DETECT,mode; int sf=0, 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 < 120; 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); setcolor(RED); y3.size=3; y3.planeshow(5*(i-16), f3); if(x=='k'){sf=1; y2.shoot(5*(i-8), f);} 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(); #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) setcolor(BLACK); for (j=0; j<=4+3*size; j=j+1) 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(); char read_key() { int y=1; char x; _AH=0x01; geninterrupt(0x16); y=_FLAGS&0x40; if(y == 0) _AH=0x00; x=_AL; return x; } return '?'; 4/29/2019 CS3369 Real Time Control Software/Wang Lusheng

Define air-plane and Shoot classes 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(); #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); } 4/29/2019 CS3369 Real Time Control Software/Wang Lusheng

Define air-plane and Shoot classes 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(); 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(); char read_key() { int y=1; char x; _AH=0x01; geninterrupt(0x16); y=_FLAGS&0x40; if(y == 0) { _AH=0x00; x=_AL; return x; } return '?'; } 4/29/2019 CS3369 Real Time Control Software/Wang Lusheng

Another definition of class Air Attributes of air-plane: size, and position. Another definition of class Air 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(); 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; x=_AL; return x; } return '?'; #include <graphics.h> #include<iostream.h> #include<conio.h> 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 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); } 4/29/2019 CS3369 Real Time Control Software/Wang Lusheng