Lecture 8: A Simple System

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
C++ Classes & Data Abstraction
Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction.
True or false A variable of type char can hold the value 301. ( F )
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
Monday, 9/23/02, Slide #1 CS 106 Intro to CS 1 Monday, 9/23/02  QUESTIONS??  Today:  Discuss Lab 3  Do Exercises  Introduction to functions  Reading:
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
General Computer Science for Engineers CISC 106 Lecture 30 Dr. John Cavazos Computer and Information Sciences 05/04/2009.
Functions. COMP104 Lecture 13 / Slide 2 Function Prototype * The function prototype declares the interface, or input and output parameters of the function,
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
1 Fall 2002 ACS 168 Problem Solving Using the Computer Weeks 9 and 10 Structures and Classes Class Diagrams Weeks 9 and 10 Structures and Classes Class.
Case Study - Fractions Timothy Budd Oregon State University.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
CS 210 DATA STRUCTURES AND ALGORITHIMS Fall 2006.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
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.
100 學年度碩士班新生暑期課程 程式設計 Object-Oriented Programming: Part I The Basics of Classes.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Example 21 #include<iostream.h> int main() { char Letter = 0;
Basic concepts of C++ Presented by Prof. Satyajit De
Structures and Classes
Review 1.
Classes and Data Abstraction
CS410 – Software Engineering Lecture #11: C++ Basics IV
This technique is Called “Divide and Conquer”.
Structures - Part II aggregate operations arrays of type struct
C++ Arrays.
CS 1430: Programming in C++.
Student Data Score First Name Last Name ID GPA DOB Phone ...
Introduction to Functions
CS 1430: Programming in C++.
CS5253 Workshop I Lecturer: Dr. Lusheng Wang
Classes and Data Abstraction
CS150 Introduction to Computer Science 1
Counting Loops.
Introduction to Programming
Implementing Classes Chapter 3.
File Review Declare the File Stream Object Name
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Object oriented programming (OOP) Lecture No. 7
Functions Divide and Conquer
CS149D Elements of Computer Science
CS150 Introduction to Computer Science 1
Fundamental Programming
CS3369 Realtime Control Computer Software/DENG Xiaotie
CS410 – Software Engineering Lecture #5: C++ Basics III
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

Lecture 8: A Simple System Software Engineering: Class member variables/functions constructor (function) A simple system -elevator 12/9/2018 CS3369 Realtime Control Computer Software/DENG Xiaotie

CS3369 Realtime Control Computer Software/DENG Xiaotie Classes and Abstract Data Types(ADT) Structures: A data type with many member variables Classes: A data type with many member variables and member functions. ADT: Data types users know functionality of public member variables and member functions but not necessary the implementation details. 12/9/2018 CS3369 Realtime Control Computer Software/DENG Xiaotie

CS3369 Realtime Control Computer Software/DENG Xiaotie Class A class is similar to a data type and an object in a class is similar to a variable in a data type. A class may have associated member variables which is accessed via a dot after the name of the object and the variable name after the dot. Example: obj1.x1 or obj1.array[6] associated member functions which is accessed via a dot after the name of the object and the function name after the dot and parameter list in parenthesis after the function name. Example: obj1.f(x1,x2) 12/9/2018 CS3369 Realtime Control Computer Software/DENG Xiaotie

CS3369 Realtime Control Computer Software/DENG Xiaotie public member variables/functions: can be accessed in any function private member variables/functions: can only be accessed in member functions of the same class. 12/9/2018 CS3369 Realtime Control Computer Software/DENG Xiaotie

CS3369 Realtime Control Computer Software/DENG Xiaotie Syntax for Prototype a Class class class_name{//prototype a class public: //public functions and variables double a_function(); //prototype a member function int i; //prototype an int private: //private functions and variables no other //functions can access it except of the same class void auxi_function(); //an auxiliary function long another_variable; //private int }; //end definition with semicolon; 12/9/2018 CS3369 Realtime Control Computer Software/DENG Xiaotie

CS3369 Realtime Control Computer Software/DENG Xiaotie class StudentRecord{//example of prototype class public: //so that other functions can access it. double final(); //prototype of member function void print_out(); //prototype output member function void get_scores(); //prototype input member function private: //no other functions can access //it except in the same class long int ID; //student id is long integer int score1, score2, exam; //scores of hmwk 1,2, exam double w1=0.25, w2=0.25, w3=0.5; }; //end definition with semicolon; 12/9/2018 CS3369 Realtime Control Computer Software/DENG Xiaotie

CS3369 Realtime Control Computer Software/DENG Xiaotie Syntax for define member functions Return_type class_name:: function_name() { //statements of what to do with the function; //member variables and functions may be used //additional auxiliary variables may also be defined //and used } 12/9/2018 CS3369 Realtime Control Computer Software/DENG Xiaotie

CS3369 Realtime Control Computer Software/DENG Xiaotie void StudentRecord:: get_scores(){ cout << “enter student ID” <<endl; cin >> ID; //use of member variable cout << “enter his scores in hmwk1, hmwk2, exam\n”; cin >> score1>>score2>>exam; //use of member variables } void StudentRecord:: print_out() { cout << “Final score of ”<<ID<<“ is ”<<final()<<endl; //member variable is used. double StudentRecord::final() {//member function definition return (w1*score1+w2*score2+w3*exam); //member variables are used} 12/9/2018 CS3369 Realtime Control Computer Software/DENG Xiaotie

CS3369 Realtime Control Computer Software/DENG Xiaotie Initialization with constructors: class StudentRecord{ public: //so that other functions can access it. double final(); //prototype of member function void print_out(); //prototype output member function void get_scores(); //prototype input member function StudentRecord(long id,int mark1, int mark2, int mark3); //a constructor prototype; private: long int ID; //student id is long integer int quiz, lab_work, exam; //marks for them double w1=0.2, w2=0.2, w3=0.6; }; //end definition with semicolon; 12/9/2018 CS3369 Realtime Control Computer Software/DENG Xiaotie

CS3369 Realtime Control Computer Software/DENG Xiaotie Calling a constructor: class_name Obj_name(arguments for constructor); Object=class_name(arguments for constructor); Default constructor: class_name(); //definition Calling default constructor: class_name Obj_name; Object=class_name(); 12/9/2018 CS3369 Realtime Control Computer Software/DENG Xiaotie

CS3369 Realtime Control Computer Software/DENG Xiaotie #include <iostream.h> #include <student.h> main(){StudentRecord x(971423,80,80,60);//constructor is // called according the following definition. x.print_out(); return 0;} StudentRecord::StudentRecord(long id,int mark1, int mark2, int mark3) {ID=id; quiz=mark1; lab_work=mark2; exam=mark3; } 12/9/2018 CS3369 Realtime Control Computer Software/DENG Xiaotie

CS3369 Realtime Control Computer Software/DENG Xiaotie #include <iostream.h> #include <student.h> char anymore(); //prototype main(){char more; StudentRecord x; do { x.get_scores(); //can be accessed in main() since it is //a public function x.print_out(); //the same reason as above more=anymore(); } while (more= =‘Y’); return 0;} 12/9/2018 CS3369 Realtime Control Computer Software/DENG Xiaotie

CS3369 Realtime Control Computer Software/DENG Xiaotie A simple System-an elevator system Define a class for an elevator array[6]- for the 6 floors of the building, if a person press ‘i’ in the elevator, then array[I-1] is set to be 1. sign-indicates the elevator is going “up” or “down”. k- the present position of the elevator. count-the number of stops in this round. 12/9/2018 CS3369 Realtime Control Computer Software/DENG Xiaotie

CS3369 Realtime Control Computer Software/DENG Xiaotie A simple System-an elevator system(continued) Description of the system: there are two (or three) elevators to serve the building Control strategies: when an elevator reaches a level, it will test if stop is required and will stop if yes. To balance the number of people/number of stops, we use count to remember the number of times it stopped in this round (from 1 to 6 or from 6 to 1). 12/9/2018 CS3369 Realtime Control Computer Software/DENG Xiaotie