4 Programming in C++ Contents Part I Introduction Basics: The C part of C Motivation From C to C Classes Pointer Data Members More on Classes More Class Examples Advanced I/O Array Redesign Templates Programming in C++ Contents Part II Inheritance More on Arrays The C++ Standard Library and Generic Programming Advanced C Object-Oriented Design Class std::string Appendix English German Dictionary Index i ix

10 Introduction Some General Remarks "C++: The Cobol of the 90s" C++ is a very powerful and complex programming language hard to learn and understand can easily be mis-used, easy to make errors but It is an illusion that you can solve complex, real-world problems with simple tools You need to know the dark sides / disadvantages so you can avoid them this is why for C++ the Morality Books are important What you don t use, you don t pay for (zero-overhead rule) It is easy / possible just to use the parts of C++ you need or like non object-oriented subset only use (class / template) libraries concrete data types ("simple classes") only... Programming in C++ Dr. Bernd Mohr, FZ Jülich, ZAM Page 11 Introduction Programming Paradigms Procedural Programming [Fortran77, Pascal, C] Decide what procedures you want; use the best algorithms you can find focus on algorithm to perform desired computation Modular Programming (Data Hiding Principle) [Modula-2, C++] Decide which modules you want; partition the program so that data is hidden in modules Group data with related functions Abstract Data Types (ADT) [Ada, Clu, Fortran90, C++] Decide which types you want; provide a full set of operations for each type if more than one object of a type (module) is needed Object Oriented Programming [Simula, Eiffel, Java, C++] Decide which classes you want; provide a full set of operations for each class; make commonality explicit by using inheritance Generic Programming [Eiffel, C++] Decide which classes you want; provide a full set of operations for each class; make commonality of classes or methods explicit by using templates Programming in C++ Dr. Bernd Mohr, FZ Jülich, ZAM Page 12

11 Introduction ANSI C and C++ C++ is NOT an object-oriented language but C++ is a multi-paradigm programming language with a bias towards systems programming that supports data abstraction supports object-oriented programming supports generic programming is a better C "as close to C as possible but no closer" [Stroustroup / Koenig, 1989] ANSI C89 is almost a subset of C++ (e.g. all examples of [K&R2] are C++!) This is not true for ANSI C99! Programming in C++ Dr. Bernd Mohr, FZ Jülich, ZAM Page 13 Programming in C++ Dr. Bernd Mohr, FZ Jülich, ZAM Page 14

20 Basics Indexed Iteration ANSI C Pascal Fortran for (i=0; i<n; i++) for i:=0 to n-1 do do i=0,n-1 { begin statements statements statements end; end do for (i=n-1; i>=0; i--) for i:=n-1 downto 0 do do i=n-1,0,-1 { begin statements statements statements end; end do for (i=b; i<e; i+=s) do i=b,e-1,s { statements statements end do In Pascal and Fortran, the control variable (e.g. i) cannot be changed in the loop body Pascal has no support for step!= 1 or -1, has to be written as while loop The ANSI C for loop is actually more powerful as shown here Programming in C++ Dr. Bernd Mohr, FZ Jülich, ZAM Page 31 Basics Controlled-Exit Iteration ANSI C Pascal Fortran while (expr) while expr do do while (expr) { begin statements statements statements end; end do do { repeat do statements statements statements if (expr) exit while (!expr); until expr; end do for / while / do { do continue; cycle statements statements break; exit end do expr in ANSI C do loop must be the negated expr of the corresponding Pascal repeat loop Programming in C++ Dr. Bernd Mohr, FZ Jülich, ZAM Page 32

cplusplus.com C++ Language Tutorial Written by: Juan Soulié Last revision: June, 2007 Available online at: http://www.cplusplus.com/doc/tutorial/ The online version is constantly revised and may contain

Group 1 Question 1. What is the correct value to return to the operating system upon the successful completion of a program? A. -1 B. 1 C. 0 D. Programs do not return a value. Question 2. What is the only

An Incomplete C++ Primer University of Wyoming MA 5310 Professor Craig C. Douglas http://www.mgnet.org/~douglas/classes/na-sc/notes/c++primer.pdf C++ is a legacy programming language, as is other languages

PROBLEM SOLVING WITH SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON Addison Wesley Boston San Francisco New York London

C++ Keywords There are many keywords in C++ that are not used in other languages. bool, const_cast, delete, dynamic_cast, const, enum, extern, register, sizeof, typedef, explicit, friend, inline, mutable,

C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with the basic components of a C++ program,

CS 110B - Rule Storage Classes Page 18-1 Attributes are distinctive features of a variable. Data type, int or double for example, is an attribute. Storage class is another attribute. There are four storage

The programming language C sws1 1 The programming language C invented by Dennis Ritchie in early 1970s who used it to write the first Hello World program C was used to write UNIX Standardised as K&C (Kernighan

Appendix E Glossary of Object Oriented Terms abstract class: A class primarily intended to define an instance, but can not be instantiated without additional methods. abstract data type: An abstraction

Adjusted/Modified by Nicole Tobias Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types

C++ for Game Programmers Course Description C++ has become one of the favourite programming language for game programmers. Reasons for wide spread acceptability of C++ are plenty, but primary reasons are,

About these lectures C A short introduction Stefan Johansson Department of Computing Science Umeå University Objectives Give a short introduction to C and the C programming environment in Linux/Unix Go

Advanced Systems Programming Introduction to C++ Martin Küttler September 23, 2016 1 / 21 About this presentation 2 / 21 About this presentation This presentation is not about learning to program 2 / 21

1. Distinguish & and && operators. PART-A Questions 2. How does an enumerated statement differ from a typedef statement? 3. What are the various members of a class? 4. Who can access the protected members

A First Book of C++ Chapter 2 Data Types, Declarations, and Displays Objectives In this chapter, you will learn about: Data Types Arithmetic Operators Variables and Declarations Common Programming Errors

CandC++ 2. Functions Preprocessor Stephen Clark University of Cambridge (heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford and Bjarne Stroustrup) Michaelmas Term 2011

Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

The Fundamentals of C++ Basic programming elements and concepts JPC and JWD 2002 McGraw-Hill, Inc. Program Organization Program statement Definition Declaration Action Executable unit Named set of program

Moving from CS 61A Scheme to CS 61B Java Introduction Java is an object-oriented language. This document describes some of the differences between object-oriented programming in Scheme (which we hope you

Fundamentals of Java Programming This document is exclusive property of Cisco Systems, Inc. Permission is granted to print and copy this document for non-commercial distribution and exclusive use by instructors

Chapter 6 Variable Base Interface 6.1 Introduction Finite element codes has been changed a lot during the evolution of the Finite Element Method, In its early times, finite element applications were developed

Solution Exercise 1.1 Part I Fill in the blanks with the most appropriate answers. 1. A Program is a sequence of step-by-step instructions that a computer executes in order to solve a problem or to perform

Introduction to C Programming CS 40: INTRODUCTION TO U NIX A ND L I NUX O P E R AT ING S Y STEMS Objectives Introduce C programming, including what it is and what it contains, which includes: Command line

Common Errors in C David Chisnall February 15, 2011 The C Preprocessor Runs before parsing Allows some metaprogramming Preprocessor Macros Are Not Functions The preprocessor performs token substitution

C++ Coding Style Guide Rules and Recommendations Version 1.4 Purpose of This Document This document describes the C++ coding style employed by Applied Informatics. The document is targeted at developers

TN203 Porting a Program to Dynamic C Introduction Dynamic C has a number of improvements and differences compared to many other C compiler systems. This application note gives instructions and suggestions

Exam in EDAA25 C Programming October 30, 2014, 14-19 Inga hjälpmedel! Examinator: Jonas Skeppstedt Grading instructions In general: assess if a function or program works as intended while ignoring syntax

Tutorial on C Language Programming Teodor Rus rus@cs.uiowa.edu The University of Iowa, Department of Computer Science Introduction to System Software p.1/64 Tutorial on C programming C program structure:

Chapter 1 1.1Reasons for Studying Concepts of Programming Languages a) Increased ability to express ideas. It is widely believed that the depth at which we think is influenced by the expressive power of

IS0020 Program Design and Software Tools Midterm, Feb 24, 2004 Name: Instruction There are two parts in this test. The first part contains 50 questions worth 80 points. The second part constitutes 20 points

Curriculum Map Discipline: Computer Science Course: C++ August/September: How can computer programs make problem solving easier and more efficient? In what order does a computer execute the lines of code

1 1 Abstract Data Types Information Hiding 1.1 Data Types Data types are an integral part of every programming language. ANSI-C has int, double and char to name just a few. Programmers are rarely content

Boolean Expressions, Conditions, Loops, and Enumerations Relational Operators == // true if two values are equivalent!= // true if two values are not equivalent < // true if left value is less than the

Reading Assignment Main Program in C In C the main program is a function called main The body of the function is enclosed in left ( ) and right ( ) curly braces. K.N. King Chapter 2 K.N. King Chapter 3

C++ for Safety-Critical Systems DI Günter Obiltschnig Applied Informatics Software Engineering GmbH guenter.obiltschnig@appinf.com A life-critical system or safety-critical system is a system whose failure

Introduction to Scientific Computing Part II: C and C++ C. David Sherrill School of Chemistry and Biochemistry Georgia Institute of Technology The C Programming Language: Low-level operators Created by

CORBA Programming with TAOX11 The C++11 CORBA Implementation TAOX11: the CORBA Implementation by Remedy IT TAOX11 simplifies development of CORBA based applications IDL to C++11 language mapping is easy

Common C Errors Compiled by: Leela Kamalesh Yadlapalli This document shows some of the common errors and warnings that you may encounter during this class. Always remember to use the Wall option if you