C++ Programming Part 2 Michael Griffiths Corporate Information and Computing Services The University of Sheffield

Slides:



Advertisements
Similar presentations
C++ Introduction.
Advertisements

C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Introduction to C++CS-2303, C-Term Introduction to C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
C++ for Engineers and Scientists Third Edition
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
C++ fundamentals.
C++ crash course Class 3 designing C++ programs, classes, control structures.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
Introduction to C++ Systems Programming.
You gotta be cool. C++ ? C++ Standard Library Header Files Inline Functions References and Reference Parameters Default Arguments and Empty Parameter.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Data Structures Using C++ 2E
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Beginning C++ Through Game Programming, Second Edition
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction 15.2C A Simple Program: Adding Two Integers.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
1 ENERGY 211 / CME 211 Lecture 7 October 6, 2008.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
C++ Programming Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Chapter 15 - C++ As A "Better C"
Introduction to C++ (Extensions to C)
C++ Templates.
Objectives In this chapter, you will:
Introduction to C++ Systems Programming.
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.
Standard Template Library (STL)
C++ in 90 minutes.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
(5 - 1) Object-Oriented Programming (OOP) and C++
CSC1201: Programming Language 2
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
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.
Operator Overloading.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
(5 - 1) Object-Oriented Programming (OOP) and C++
Chapter 15 - C++ As A "Better C"
Arrays Arrays A few types Structures of related data items
CSC1201: Programming Language 2
Lesson 25 Miscellaneous Topics
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
SPL – PS1 Introduction to C++.
Presentation transcript:

C++ Programming Part 2 Michael Griffiths Corporate Information and Computing Services The University of Sheffield

Scoping and Namespaces The Standard template library Presentation Outline

Namespaces and Scopes Scoping and the Scope Resolution Operator :: Scope of an identifier is that portion of the application within which the identifier can be referenced When a local variable is defined within a block (e.g. in a function or in between {} –Can only be used in that function or block –This is the scope

Types of scope function block file global Function-prototype

The scope resolution operator :: Using the :: operator –::varname –Scopename::varname Example – global variable defined outside main float var=0; //Global variable main() { float var=1.0; //Local variable to main cout << "Local var in outer scope of main is " << var << endl; cout << "using the unary scope resolution operator " << endl << "Global var is " << ::var << endl; …….. }

Namespaces Mechanism to avoid name collisions between functions, variables and overloaded binary operations Enable grouping of global classes, objects, functions etc. under a name –Global scope is split into sub-scopes known as namespaces

Using namespaces Declaration namespace identifier { namespace-body }

namespaces example namespace first { int var=5; } namespace second { double var=3.141; } int main() { cout << "Namespace first " << first::var << endl; cout << "Namespace second " << second::var << endl; return 0; }

using namespace Specify the namespace being used –Scope resolution operator namespace::variable –using namespace identifier Namespace std –ANSI C++ standard ALL classes, objects and functions of standard C++ library defined within namspace std Example ANSI C++ compliant Hello World (note omits.h from include statement) #include int main () { std::cout << "Hello world in ANSI-C++\n"; return 0; }

using namespace std #include using namespace std; //if this is omitted //need to use the operator std:: //to access the iostream library functions //such as cout. namspace declarations ….. main() { }

Referring to different namespace s int main() { cout << "Namespace first " << first::var << endl; cout << "Namespace second " << second::var << endl; { using namespace first; std::cout << "Using namespace first " << var << std::endl; } { using namespace second; std::cout << "Using namespace second " << var << std::endl; } return 0; }

Standard Template Library ANSI-compliant way to include the standard libraries is highly recommended for STL users Templates – A new feature introduced by ANSI-C++ standard. –C++ compiler must comply with the standard –Range of container and general utilities available –Efficient use of these templates can lead to rapid development well engineered applications. Templates are described and demonstrated briefly below.

Reasons for Using STL Efficient use of these templates can lead to rapid development well engineered applications. Templates are described and demonstrated briefly below. Easy to maintain Portable

Standard libraries with C++

STL Collection Classes

STL Template Classes

String manipulation using STL Namespace std defines –typedef basic_string string; Initialisation –string text(“Hello”); –string name(8, ‘x’); Provides default –Constructor –Copy Constructor

String operations Assignment –mystring.assign(string, pos, size) –mystring = anotherstring; –mystring.append(anotherstring); –mystring+=anotherstring; Streaming –cout << mystring; Comparison –Mystring.compare(anotherstring); Swap two strings –mystring.swap(anotherstring)

vector Declaration #include using namespace std; Define a vector of integers. The template name is "vector" –the type of object it contains is "int"; The fully specified container data type is –vector

Initialising a Vector vector vec_one; int a = 2; // Some integer data objects int b = -5; vec_one.push_back(a); // Add item at end of vector vec_one.push_back(9); vec_one.push_back(b);

Accessing Vector Elements Use [] operator to access elements // vec_one now contains three int values: 2, 9, -5 unsigned int indx; for (indx = 0; indx < vec_one.size(); indx++) { cout << vec_one[indx] << endl; Write out vector item }

STL Iterators iterator pointer to an item that is part of a larger container of items. Containers support a function called begin, –returns an iterator pointing to the beginning of the container (the first element) and Containers support a function called end, –returns an iterator corresponding to having reached the end of the container. Access elements by "dereferencing" the iterator with a *, just as you would dereference a pointer.

Initialising an Iterator To request an iterator appropriate for a particular STL templated class, you use the syntax std::class_name ::iterator name where name is the name of the iterator variable you wish to create class_name is the name of the STL container you are using template_paramters are the parameters to the template used to declare objects that will work with this iterator. –because the STL classes are part of the std namespace, you will need to either prefix every container class type with "std::", as in the example, or include "using namespace std;" at the top of your program. namespace

Example vector Iterator For instance, if you had an STL vector storing integers, you could create an iterator for it as follows: std::vector myIntVector; std::vector ::iterator myIntVectorIterator;

Using An Iterator vector myIntVector; vector ::iterator myIntVectorIterator; // Add some elements to myIntVector myIntVector.push_back(1); myIntVector.push_back(4); myIntVector.push_back(8); for(myIntVectorIterator = myIntVector.begin(); myIntVectorIterator != myIntVector.end(); myIntVectorIterator++) { //Should output cout<<*myIntVectorIterator<<" "; }

Iterator Types Different operations and containers support different types of iterator behaviour. Several different classes of iterators –slightly different properties. –distinguished by whether you can use them for reading or writing data in the container. –Some types of iterators allow for both reading and writing behavior, though not necessarily at the same time. forward, backward and the bidirectional iterators. –used as either input or output iterators, –The forward iterator only allows movement one way – from the front of the container to the back. To move from one element to the next, the increment operator, ++, can be used.

Recommendation Do not mix C and C++ Programming Styles –External c libraries can still be called For C++ programs use the ANSI C++ header nad not the ANSI C headers standard libraries Keep things simple –Build Create new classes when necessary –Take care when using inheritance Avoid re-inveting the wheel, use SDK’s –See the code repository references below

Conclusion Advantages –Powerful development language for HPC applications –Portable –Reusable –Requires careful design –Wide range of libraries enabling high performance and rapid development. Disadvantage –Very easy to develop sloppy and inefficient code!!!!!!!!

References Standard template libraries – Bjarn Stroustrups homepage – Documentation for Sun Compilers –

Code Respositories