CSCE 121-200 Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.

Slides:



Advertisements
Similar presentations
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Advertisements

C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 7: Errors 1 Based on slides created by Bjarne Stroustrup.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 5: Functions 1 Based on slides created by Bjarne Stroustrup.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 ; Programmer-Defined Functions Two components of a function definition.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
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.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Introduction to C++CS-2303, C-Term Introduction to C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Abstract Data Types and Encapsulation Concepts
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 8: Classes 1 Based on slides created by Bjarne Stroustrup.
Chapter 8 Technicalities: Functions, etc. John Keyser’s Modifications of Slides by Bjarne Stroustrup
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
CSCE 121: Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 3: Objects, Types, and Values 1 Based on slides.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
1 C++ Syntax and Semantics, and the Program Development Process.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 14 Functions.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
C++ Programming Part 2 Michael Griffiths Corporate Information and Computing Services The University of Sheffield
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 4: Computation 1 Based on slides created by Bjarne Stroustrup.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 17: Vectors and Arrays 1 Based on slides created by Bjarne Stroustrup.
CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Slides adapted from: Bjarne Stroustrup, Programming – Principles and Practice using C++ Chapter 8 Technicalities: Functions, etc. Hartmut Kaiser
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Basic Elements of C++.
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.
Basic Elements of C++ Chapter 2.
Abstract Data Types and Encapsulation Concepts
Variables with Memory Diagram
Chapter 8 Technicalities: Functions, etc.
Chapter 8 Technicalities: Functions, etc.
Anatomy of Templates CSCE 121 J. Michael Moore
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.
STL: Traversing a Vector
Namespaces How Shall I Name Thee?.
Chapter 8 Technicalities: Functions, etc.
Guidelines for Writing Functions
Functions Imran Rashid CTO at ManiWeber Technologies.
C++ Programming Basics
What Is? function predefined, programmer-defined
Presentation transcript:

CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup and Jennifer Welch

CSCE 121: Set 6: Miscellaneous Range-Based Loops Simpler way to iterate over all elements of a vector: vector v; // push_back items into v for (int x : v) { // for all items x in v cout << x; for (auto x : v) { // compiler figures out type of x cout << x; 2

CSCE 121: Set 6: Miscellaneous Declarations A declaration – introduces a name into a scope (informally, a part of the program; more later) – Specifies a type for the named object – Might include an initializer A name must be declared before it can be used int a = 7; // int variable double sqrt(double); // function, double arg and ret vector v; // vector of doubles 3

CSCE 121: Set 6: Miscellaneous Placement of Functions Since names must be declared before being used, you must declare functions used in main before main actually appears. If function f1 calls function f2, then f2 must be declared before f1 is defined. See nested-calls.cpp 4

CSCE 121: Set 6: Miscellaneous Definitions A definition is a declaration that also fully specifies the entity int a = 7; vector v; // empty vector of doubles double sqrt(double) { … } // function with a body Examples of declarations that are not definitions: double sqrt(double); // function w/o a body T x; // user-defined type T is specified elsewhere 5

CSCE 121: Set 6: Miscellaneous Declaration vs. Definition You cannot define something twice (in the same scope) – Example: you cannot define variable “a” twice in a function, or function “sqrt” twice in a file You can declare something twice – Example: you can declare a function at one point and then later define it (we’ll see a use for this later) 6

CSCE 121: Set 6: Miscellaneous Why Both Declarations and Definitions? To refer to something, we need only its delaration Often we want the definition “elsewhere”: – Later in a file – In another file Declarations specify interfaces – To your own code – To libraries In larger programs, put all declarations in header files to ease sharing 7

CSCE 121: Set 6: Miscellaneous Kinds of Declarations Variables Constants (coming up very soon) Functions Namespaces (coming up very soon) Types (coming up soon) Templates (coming up later) 8

CSCE 121: Set 6: Miscellaneous Header Files Header file contains declarations (of functions, types, constants, etc.) giving an interface to other parts of the program Provides abstraction: don’t have to know details of a function to use it The actual functions, types, etc. are defined in other source code files (yours or libraries) The construct #include “std_lib_facilities_4.h” is a “preprocessor directive” that adds the contents of the file to your program 9

CSCE 121: Set 6: Miscellaneous 10 // declarations: double sqrt(double); … #include ”mymath.h" //definitions: double sqrt(double x) { /* code to calculate square root */ } … #include ”mymath.h" … double x = 122; double y = sqrt(x); … mymath.h: mymath.cpp: use.cpp:

CSCE 121: Set 6: Miscellaneous Scope A scope is a region of program text – Global scope: outside any language construct – Class scope: inside a class (coming soon) – Local scope: between { and } – Statement scope: e.g., in a for-statement A name in a scope is visible (can be used) in that scope and in all nested scopes – But only after the declaration – Exception: class members can be used before they are declared 11

CSCE 121: Set 6: Miscellaneous Scope Example See scope.cpp 12

CSCE 121: Set 6: Miscellaneous Benefits of Scoping A scope keeps names local Prevents my variables, functions, etc. from interfering with yours Real programs have thousands of entities, so we need help from the programming language Locality is good! Keep names as local as possible – Avoid global variables! 13

CSCE 121: Set 6: Miscellaneous Constants To declare a variable whose value should not be changed after initialization: – If value can be determined at compile-time: constexpr double pi = ; – If value cannot be determined at compile-time (e.g., it depends on user input): const double cd = 8.7; These “variables” must be assigned a value when declared and cannot be assigned to later Use named constants instead of literals! (E.g., is unclear but speed_of_light is good) 14

CSCE 121: Set 6: Miscellaneous Namespaces A namespace is a scope with name The :: syntax is used to specify which namespace you are using and which (of many possible) objects with the same name you are referring to This lets different scopes use the same names without clashes 15

CSCE 121: Set 6: Miscellaneous C++ Standard Library Facilities of the C++ standard library include: input/output, strings, vectors, and much more It’s really a collection of libraries – E.g.,,, All these libraries are in the std namespace To use them in your program, do this: #include To use names in one of these, prefix with std::, e.g., std::cout 16

CSCE 121: Set 6: Miscellaneous using Declarations and Directives To avoid the tedium of prefixing names with std:: Using declaration for one particular name: using std::cout; cout << “hi”; Using directive for all names in the namespace: using namespace std; cout << “enter value for x”; cin >> x; 17

CSCE 121: Set 6: Miscellaneous Acknowledgments Slides are based on those for the textbook: 18