1 ENERGY 211 / CME 211 Lecture 7 October 6, 2008.

Slides:



Advertisements
Similar presentations
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
Advertisements

Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type.
Objectives In this chapter, you will:
I/O and Program Control Statements Dick Steflik. Overloading C++ provides two types of overloading –function overloading the ability to use the same function.
14 Templates. OBJECTIVES In this chapter you will learn:  To use function templates to conveniently create a group of related (overloaded) functions.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
計算機概論實習 Stream Stream: sequence of bytes Input: from device (keyboard, disk drive) to memory Output: from memory to device (monitor, printer,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
Introduction to C++CS-2303, C-Term Introduction to C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
C++ crash course Class 3 designing C++ programs, classes, control structures.
Basic Elements of C++ Chapter 2.
Introduction to C++ Systems Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
High-Level Programming Languages: C++
Darbas su failais Arnas Terekas IT 1gr. Vilniaus universitetas Matematikos ir informatikos fakultetas.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Looping and Counting Lecture 3 Hartmut Kaiser
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.
C++ Programming Part 2 Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
VARIABLES AND DATA TYPES Chapter2:part1 1. Objectives: By the end of this section you should: Understand what the variables are and why they are used.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
1 ENERGY 211 / CME 211 Lecture 18 November 2, 2007.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Chapter 15 - C++ As A "Better C"
Introduction to C++ (Extensions to C)
Enumeration Type Data type: a set of values with a set of operations on them Enumeration type: a simple data type created by the programmer To define an.
Introduction to C++ Systems Programming.
Programming Fundamentals
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Some Basics for Problem Analysis and Solutions
Some Basics for Problem Analysis
Standard Version of Starting Out with C++, 4th Edition
Variables have attributes
Exceptions, Templates, and the Standard Template Library (STL)
Std Library of C++.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
ENERGY 211 / CME 211 Lecture 18 October 31, 2008.
Presentation transcript:

1 ENERGY 211 / CME 211 Lecture 7 October 6, 2008

2 Namespaces We have seen expressions such as std::cout, that are unique to C++ std refers to a namespace C++ programmers use code written by others, and names can easily conflict Namespaces allow distinguishing names declared in different files Still, it would be nice to not have to explicitly mention them all the time!

3 The using Directive Form: using namespace namespace-name ; Allows use of names declared within a namespace, without specifying the namespace each time Compiler will look in the namespace for names that are not declared elsewhere Example: using namespace std; allows use of cout instead of std::cout

4 using Declarations Form: using namespace-name :: var-name; Specifies that any instance of var-name (without a namespace) refers to the object var-name declared within namespace-name The using directive allows the compiler to look for any name in a namespace, but a using declaration forces use of a namespace for a specific name

5 Global Names Usually, variables are declared within a function (these are local variables) or within a namespace Global variables are declared outside of any function or namespace If a global variable conflicts with a namespace variable or local variable, we can prefix the global variable with a :: (by itself) to avoid ambiguity

6 Namespace Aliases A namespace can have a very long name, for example to identify the company who created it A namespace alias allows a shorter name to be substituted for convenience Form: namespace alias = original ; Then original ::x can be replaced with alias ::x throughout

7 Input from std::cin char c; c = cin.get(); reads a character from cin and assigns it to c string s; getline(cin, s); reads a line from cin and stores it in s cin.clear(); resets cin to working state after >> fails due to bad input cin == true is true if cin is in working state, false otherwise cin >> x reads from cin, stores in x

8 Output to std::cout cout.put(c); writes char c to cout cout << x writes x to cout cout.flush(); forces any buffered output to be written immediately cout.width(n); sets field width to n cout.precision(n); sets number of decimal places printed to n (default 6) cout.fill(c); sets the fill character to c (default is space)

9 std::string In C, strings are arrays of characters The C standard library provides functions for working with strings, but they are still cumbersome The type std::string provides higher-level functionality string s can be initialized at declaration (like integer variables), and assigned, unlike in C

10 Operations on string s Can access individual characters of a string using the [] operator, which also accesses elements of an array (discussed next time) Can use + to concatenate string objects, as well as string literals Can use >> to read from an input stream into a string object, but only one word at a time

11 std::vector<> This is an incomplete type, known as a class template The type is completed by specifying the type of the elements of the vector, between the <> Generic operations are implemented once, and apply to vectors of any type Vectors can be treated as a list, or as an array (for random access), depending on the application

12 Example: Sorting Reads numbers from standard input Stores them in a vector object Uses sort function to sort them in increasing order Vector is sorted "in-place", that is, overwritten with elements in sorted order Iterates through the vector, using random access to display each one

13 Iterators An iterator is a type whose values locate objects as if they were C array elements Types of iterators: –Trivial: gives location and nothing more –Forward: can use ++ to access next location –Bi-directional: Forward, along with -- –Random-access: Bi-directional, with +, - to skip over locations, [] to access any location sort uses random-access iterators from begin() and end() member functions

14 Next Time All about functions! Types of functions (this bullet point has a double meaning) Overloading operators inline functions Passing arguments