1 The UNIX date command myclock.cpp example The C/C++ time() and ctime() functions myclock2.cpp example Inline function definitions Inline class member.

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

1 Random number generation Using srand(), rand() & time(0) Searching and Sorting Demo Making searching & sorting more generic Overloading the functions.
1 DOS vs. UNIX files Ending lines with “\r\n” vs. “\n” Reading an entire line at a time getline() To skip white space or not cin >> ch; vs. ch = cin.get();
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
Abstract Data Types Using Classes Lecture-5. Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
CHAPTER 13 CLASSES AND DATA ABSTRACTION. In this chapter, you will:  Learn about classes  Learn about private, protected, and public members of a class.
ITEC 320 C++ Examples.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
11 Introduction to Object Oriented Programming (Continued) Cats.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
CS 11 C++ track: lecture 1 Administrivia Need a CS cluster account sysadmin/account_request.cgi Need to know UNIX (Linux)
 140 Total Points ◦ 100 Points Writing Programs ◦ 24 Points Tracing Algorithms and determining results ◦ 16 Points Short Answer  Similar to quizzes.
W 4 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH ; p197 – 226 Week 4 lesson 2Operators 1H ; p237 – 254 Week 5 lesson.
1 Functions  A function is a named, independent section of C++ code that performs a specific task and optionally returns a value to the calling program.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Navigating the C++ Development Environment
Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
11 Introduction to Object Oriented Programming (Continued) Cats.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
C++ Functions A bit of review (things we’ve covered so far)
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Lecture 3: Getting Started & Input / Output (I/O)
Chapter 12 Classes and Abstraction
Pointer to an Object Can define a pointer to an object:
Command Line Arguments
Command Line Arguments
CS 144 Advanced C++ Programming February 21 Class Meeting
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
SPL – PS1 Introduction to C++.
Presentation transcript:

1 The UNIX date command myclock.cpp example The C/C++ time() and ctime() functions myclock2.cpp example Inline function definitions Inline class member functions CSE Lecture 23 – Time & inline functions

2 Reading, Etc. Weekly Reading: Sections: Later this week Complex Numbers Mandelbrot and Julia Sets

3 UNIX date command Returns a formatted string indicating current date and time Default Format: DayName MonthName Day hh:mm:ss TZ Year Examples: Fri Dec 23 10:10:42 EST 1988 Tue Oct 24 23:08:30 EDT 2006

4 Using date in a program Use system command to run the date command and redirect its output into a file Open file and use extraction operator to read parts of formatted date Close and remove the file

5 Using date in a program ifstream inf; string dayOfWk, Month, Day, hhmmss, Z, Year; // run date command, save output in file system("date > xyztodayxyz.dat"); // use an odd name // open file and extract the date and time // format example: Fri Dec 23 10:10:42 EST 1988 inf.open("xyztodayxyz.dat"); inf >> dayOfWk >> Month >> Day >> hhmmss >> Z >> Year; inf.close(); // remove the file system("rm xyztodayxyz.dat");

6 Example (myclock.cpp) Determines date and launches itself over again in a separate window to display the date If argc is 1, system("date > xyztodayxyz.dat"); system(“xterm –e myclock xyztodayxyz.dat &”); exit(0); If argc is > 1, // open file and display date

7 myclock.cpp code // myclock.cpp -- JHS 2006 Notre Dame #include using namespace std; int main(int argc, char *argv[]) { if (argc == 1) { // save date in file, restart program inside xterm // with filename for date info, and exit system("date > xyztodayxyz.dat"); system("xterm -e myclock xyztodayxyz.dat &"); exit(0); } // some variables so we can read date info from file ifstream inf; string dayOfWk, Month, Day, hhmmss, Z, Year;

8 myclock.cpp code // date format example: Fri Dec 23 10:10:42 EST 1988 // read date information & dispose of file inf.open("xyztodayxyz.dat"); inf >> dayOfWk >> Month >> Day >> hhmmss >> Z >> Year; inf.close(); system("rm xyztodayxyz.dat"); // display date information (in the xterm) cout << dayOfWk << endl; cout << Month << endl; cout << Day << endl; cout << hhmmss << endl; cout << Z << endl; cout << Year << endl; // wait a bit, then quit system("sleep 5"); return 0; }

9 Time and ctime functions Defined in the header Both use the time_t data type The time(time_t *t) function has a pointer (or address) to a time_t structure as its parameter It sets t to point to the number of seconds since Jan 1, 1970 The char *ctime(time_t *t) function converts the time pointed to by t into local time as a string

10 Using time and ctime // some character arrays (C-style strings), etc char hhmmss[32], date_time[64]; char dayName[4], monthName[4], day[3], year[5]; int hour, minute, second; // a time_t variable named ltime time_t ltime; time (&ltime); // pass address of ltime to the function // now convert the ltime seconds to local time as a string // NOTE: sprintf is like printf and printw, but prints into a // char array sprintf(date_time,"%s", ctime(&ltime)); // now we can use sscanf to extract the parts out of the date sscanf(date_time,"%s %s %s %s %s", dayName, monthName, day, hhmmss, year); // get strings sscanf(hhmmss,"%d:%d:%d", &hour, &minute, &second); // get numbers

11 Inline functions We can declare functions “inline” to … Allow the compiler to optimize use of the function Using code substitution rather than functino calls and returns Note: the compiler has the final say about whether the function is implemented as a function or is used to perform inline code substitution

12 Inline example // declaration of inline function (note use of “inline”) inline int plus5 (int x) { return x + 5; } // original call on line 120 of code a = b + plus5(c); // the compiler will compile code AS IF the following was // the actual line 120 of code. That is, it substitutes // the body of the function in place of the function call // but uses the name of the actual parameter a = b + (c + 5);

13 When does the compiler NOT substitute If the inline function is too complex, the compiler will usually generate a normal function call So keep inline functions simple Just a few lines Straightforward algorithms Etc

14 Inline member functions in a C++ class Inline member functions are implemented within the class declaration In the header file Without the “inline” indicator Constructors, destructors and simple accessor functions are often done this way

15 Inline member function Example // point.h header file class point { public: // example inline member functions point(int x=0, int y=0) : xPosit(x), yPosit(y) { } int getX() const { return xPosit; } // regular member functions implemented elsewhere point operator+ (point p); point moveTo(int x, int y); private: xPosit, yPosit; };

16 Implementing a ticking clock inline void tick (int &hr, int &min, int &sec) { usleep( ); // wait a second sec++; // increment seconds if (sec == 60) { sec = 0; // reset seconds min++; // increment minutes if (min == 60) { min = 0; // reset minutes hr++; // increment hours if (hr == 24) // this is a 24 hour clock { hr = 0; // reset hours }

17 Put it all together See examples myclock.cpp Uses date command and temp file Shows details of current time in xterm myclock2.cpp Uses time and ctime functions Shows ticking clock in xterm using curses I/O

18 Current program Questions about … Images Files Make Makefiles, rules & targets …