C++: Functions, Program Compilation, Libraries Modified from CS101 slides, which are by JPC and JWD © 2002 McGraw-Hill, Inc.

Slides:



Advertisements
Similar presentations
Introduction to Programming in C++ John Galletly.
Advertisements

J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Functions Computational assistants.
COSC 120 Computer Programming
Iterative Constructs – chapter 4 pp 189 to 216 This lecture covers the mechanisms for deciding the conditions to repeat action. Some materials are from.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
Writing and Testing Programs Drivers and Stubs Supplement to text.
Function basics – pp Computational assistants – improves clarity and enable software reuse. JPC and JWD © 2002 McGraw-Hill, Inc.
Three types of computer languages
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Programmer-defined functions Development of simple functions using value parameters.
Iterative Constructs Mechanisms for deciding under what conditions an action should be repeated JPC and JWD © 2002 McGraw-Hill, Inc.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
 Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and output.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
COMPUTER PROGRAMMING. Introduction to C++ History Merges notions from Smalltalk and notions from C The class concept was borrowed from Simular67 Developed.
Chapter 7 Functions.
COMPUTER SCIENCE I C++ INTRODUCTION
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
History of C and C++ C++ evolved from C ANSI C C++ “spruces up” C
Chapter 1 Introduction to Computers and C++ Programming Goals: To introduce the fundamental hardware and software components of a computer system To introduce.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
File I/O ifstreams and ofstreams Sections 11.1 &
1 Simple Functions Writing Reuseable Formulas. In Math Suppose f (x) = 2 x 2 +5Suppose f (x) = 2 x 2 +5 f(5)=?f(5)=? f(5) = 2* =55f(5) = 2*
1 COMS 261 Computer Science I Title: String Class Date: October 3, 2005 Lecture Number: 14.
Chapter 6: Programmer- defined Functions Development of simple functions using value and reference parameters JPC and JWD © 2002 McGraw-Hill, Inc. Modified.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
Chapter 2 part #1 C++ Program Structure
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 05, 2005 Lecture Number: 4.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Hardware and Software Programming. COMP104 Lecture 2 / Slide 2 Hardware and Software l Why should we bother with hardware, while we are having a programming.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
Vectors One-Dimensional Containers. Problem A file contains a sequence of names and scores: Ann92 Bob84 Chris89... Using OCD, design and implement a program.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
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.
CHAPTER 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
By Kundang K Juman Hardware & Software. COMP102 Prog. Fundamentals I: Software / Slide 2 l Four components of a computer system: n CPU - central processing.
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
ifstreams and ofstreams
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE
Chapter 1: Introduction to computers and C++ Programming
CS-103 COMPUTER PROGRAMMING
Chapter 2 part #1 C++ Program Structure
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
COMS 261 Computer Science I
Intro to Programming Week # 4 Control Structure Lecture # 8
1.13 The Key Software Trend: Object Technology
Programming with Data Files
Hardware & Software Programming. COMP102 Prog. Fundamentals I: Software / Slide 2 l Four components of a computer system: n CPU - central processing unit.
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Programs written in C and C++ can run on many different computers
COMS 261 Computer Science I
Iterative Constructs Mechanisms for deciding under what conditions an action should be repeated JPC and JWD © 2002 McGraw-Hill, Inc.
COMS 261 Computer Science I
Capitolo 1 – Introduction C++ Programming
C++ Programming Basics
ifstreams and ofstreams
COMS 261 Computer Science I
Chapter 8 Functions.
Chapter 1 c++ structure C++ Input / Output
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

C++: Functions, Program Compilation, Libraries Modified from CS101 slides, which are by JPC and JWD © 2002 McGraw-Hill, Inc.

Translation System Set of programs used to develop software A key component of a translation system is a translator Some types of translators Compiler  Converts from one language to another Linker  Combines resources Examples Microsoft Visual C++ ®, C++Builder ®, g++, Code Warrior ®  Performs compilation, linking, and other activities.

Software Development Activities Editing Compiling Linking with precompiled files Object files Library modules Loading and executing Viewing the behavior of the program

Software Development Cycle

IDEs Integrated Development Environments or IDEs Supports the entire software development cycle  E.g., MS Visual C++, Borland, Code Warrior Provides all the capabilities for developing software Editor Compiler Linker Loader Debugger Viewer

Function Prototypes Before a function can appear in an invocation its interface must be specified Prototype or complete definition int Max(int a, int b); ;

Function Prototypes Before a function can appear in an invocation its interface must be specified Prototypes are normally kept in library header files int Max(int a, int b); ;

Libraries Library Collection of functions, classes, and objects grouped by commonality of purpose Include statement provides access to the names and descriptions of the library components Linker connects program to actual library definitions Examples String: STL’s string class Graphics libraries GUI libraries (like Qt, Windows Forms)

Basic Translation Process

Some Standard Libraries fstream File stream processing assert C-based library for assertion processing iomanip Formatted input/output (I/O) requests ctype C-based library for character manipulations math C-based library for trigonometric and logarithmic functions Note C++ has many other libraries

Library Header Files Describes library components Typically contains Function prototypes  Interface description Class definitions Sometimes contains Object definitions  Example: cout and cin in iostream

Library Header Files Typically do not contain function definitions Definitions are in source files Access to compiled versions of source files provided by a linker

#include using namespace std; int main() { cout << "Enter Quadratic coefficients: "; double a, b, c; cin >> a >> b >> c; if ( (a != 0) && (b*b - 4*a*c > 0) ) { double radical = sqrt(b*b - 4*a*c); double root1 = (-b + radical) / (2*a); double root2 = (-b - radical) / (2*a); cout << "Roots: " << root1 << " " << root2; } else { cout << "Does not have two real roots"; } return 0; } Invocation Library header files

#include #include // file stream library using namespace std; int main() { ifstream fin("mydata.txt"); int ValuesProcessed = 0; float ValueSum = 0; float Value; while (fin >> Value) { ValueSum += Value; ++ValuesProcessed; } // to be continued next slide… }

#include #include // file stream library using namespace std; int main() { ifstream fin("mydata.txt"); int ValuesProcessed = 0; float ValueSum = 0; float Value; while (fin >> Value) { ValueSum += Value; ++ValuesProcessed; } if (ValuesProcessed > 0) { ofstream fout("average.txt"); float Average = ValueSum / ValuesProcessed; fout << "Average: " << Average << endl; return 0; } else { cerr << "No list to average" << endl; return 1; }

ifstream sin("in1.txt"); // extract from in1.txt ofstream sout("out1.txt"); // insert to out1.txt string s; while (sin >> s) {// what’s this do? sout << s << endl; }

ifstream sin("in1.txt"); // extract from in1.txt ofstream sout("out1.txt"); // insert to out1.txt string s; while (sin >> s) {// what’s this do? sout << s << endl; } sin.close(); // done with in1.txt sout.close(); // done with out1.txt

ifstream sin("in1.txt"); // extract from in1.txt ofstream sout("out1.txt"); // insert to out1.txt string s; while (sin >> s) {// what’s this do? sout << s << endl; } sin.close(); // done with in1.txt sout.close(); // done with out1.txt sin.open("in2.txt"); // now extract from in2.txt sout.open("out.txt", // now append to out2.txt (ios_base::out | ios_base::app));

ifstream sin("in1.txt"); // extract from in1.txt ofstream sout("out1.txt"); // insert to out1.txt string s; while (sin >> s) {// what’s this do? sout << s << endl; } sin.close(); // done with in1.txt sout.close(); // done with out1.txt sin.open("in2.txt"); // now extract from in2.txt sout.open("out.txt", // now append to out2.txt (ios_base::out | ios_base::app)); while (sin >> s) {// what’s this do? sout << s << endl; } sin.close(); // done with in2.txt sout.close(); // done with out2.txt