September 10,

Similar documents
Overview of C++: Part 1

Networked Embedded System Patterns for C Developers. Overview of C (& C++) Programming Styles. Douglas C. Schmidt

An Overview of C++ Douglas C. Schmidt

C++11 and Compiler Update

C++11/14 Rocks. Clang Edition. Alex Korban

Chapter 15 - C++ As A "Better C"

Today s lecture. CS 314 fall 01 C++ 1, page 1

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Advanced Systems Programming

CS

Introduction to C++ Systems Programming

RAD Studio XE3 The Developer Force Multiplier

Fast Introduction to Object Oriented Programming and C++

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

CS3157: Advanced Programming. Outline

September 19,

Tokens, Expressions and Control Structures

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Overview of today s lecture. Quick recap of previous C lectures. Introduction to C programming, lecture 2. Abstract data type - Stack example

Ch. 10: Name Control

CS349/SE382 A1 C Programming Tutorial

CMSC 4023 Chapter 11

The Foundation of C++: The C Subset An Overview of C p. 3 The Origins and History of C p. 4 C Is a Middle-Level Language p. 5 C Is a Structured

COMP 2355 Introduction to Systems Programming

Object Oriented Programming. Solved MCQs - Part 2

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

CSCI387 Data Structure Fall. Aug. 21, 2012 Sung Hee Park Computer Science Virginia State University

Interview Questions of C++

Abstract Data Types and Encapsulation Concepts

Dr. Md. Humayun Kabir CSE Department, BUET

PROGRAMMING IN C++ COURSE CONTENT

Introduction to C++11 and its use inside Qt

Chapter 19: Program Design. Chapter 19. Program Design. Copyright 2008 W. W. Norton & Company. All rights reserved.

C++11: 10 Features You Should be Using. Gordon R&D Runtime Engineer Codeplay Software Ltd.

Introduction to C++ Introduction to C++ 1

Programming in C and C++

Instantiation of Template class

Object-Oriented Programming

Pointers, Dynamic Data, and Reference Types

AN OVERVIEW OF C++ 1

C++ for System Developers with Design Pattern

An Introduction to C++

Abstract Data Types (ADTs) & Design. Selecting a data structure. Readings: CP:AMA 19.5, 17.7 (qsort)

Chapter 11. Abstract Data Types and Encapsulation Concepts

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Computer Science 306 Study Guide

6.S096 Lecture 4 Style and Structure

Introduction to Programming Using Java (98-388)

6.096 Introduction to C++ January (IAP) 2009

CS 376b Computer Vision

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Chapter 11. Abstract Data Types and Encapsulation Concepts 抽象数据类型 与封装结构. 孟小亮 Xiaoliang MENG, 答疑 ISBN

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

COEN244: Class & function templates

Important From Last Time

C The new standard

Generalized Constant Expressions

QUIZ. What is wrong with this code that uses default arguments?

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Chapter 1: Object-Oriented Programming Using C++

Page 1. Stuff. Last Time. Today. Safety-Critical Systems MISRA-C. Terminology. Interrupts Inline assembly Intrinsics

CMPSCI 187: Programming With Data Structures. Lecture #11: Implementing Stacks With Arrays David Mix Barrington 28 September 2012

Compiler construction 2009

Ch. 3: The C in C++ - Continued -

Practical C++ Programming

Fundamentals of Programming. Lecture 19 Hamed Rasifard

Introduce C# as Object Oriented programming language. Explain, tokens,

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Fundamental of Programming (C)

Grafting Functional Support on Top of an Imperative Language

C++11 Introduction to the New Standard. Alejandro Cabrera February 1, 2012 Florida State University Department of Computer Science

CS3215. Outline: 1. Introduction 2. C++ language features 3. C++ program organization

Contents of Lecture 3

Basic Types, Variables, Literals, Constants

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

Datastructuren André Deutz

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

MISRA-C. Subset of the C language for critical systems

VALLIAMMAI ENGINEERING COLLEGE

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

History C++ Design Goals. How successful? Significant constraints. Overview of C++

Abstract data types &

Abstract Data Types & Object-Oriented Programming

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

C++ Primer, Fifth Edition

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

JAVA An overview for C++ programmers

Francesco Nidito. Programmazione Avanzata AA 2007/08

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Lecture 3: C Programm

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

Inheritance, Polymorphism and the Object Memory Model

Introducing C++ to Java Programmers

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

Transcription:

September 10, 2013 1

Bjarne Stroustrup, AT&T Bell Labs, early 80s cfront original C++ to C translator Difficult to debug Potentially inefficient Many native compilers exist today C++ is mostly upward compatible extension of C Stronger typechecking Support for data abstraction Support for object-oriented programming Support for generic programming 2

Run-time efficiency No complicated run-time libraries or virtual machines are necessary Unlike Ada, Java, C#, etc. No language support for concurrency, persistence, or distribution Compatibility with C libraries Object code reuse Compatible storage layout structure Support for ANSI C library and system calls via extern blocks 3

As close to C as possible, but no closer C++ is not a proper superset of C Backwards compatibility is not entirely maintained Certain C++ design goals conflict with modern techniques for: Compiler optimization Pointers to arbitrary memory locations complicate register allocation & garbage collection Software engineering Separate compilation complicates inlining due to difficulty of interprocedural analysis Dynamic memory management is error-prone 4

C++ supports data abstraction & encapsulation The class mechanism and namespaces C++ supports object-orientated programming features Abstract classes, inheritance, and virtual methods C++ supports generic programming Parametrized types and TMP C++ supports sophisticated error handling Exception handling C++ supports identifying an object s type at runtime Run-Time Type Identification (RTTI) and dynamic_cast 5

Enforces type checking via function prototypes Provides type-safe linkage Provides inline function expansion Declare constants that can be used to define static array bounds with the const type qualifier. Built-in dynamic memory management via new & delete operators Namespace control 6

The name of a struct, class, enum, or union is a type name References allow call-by-reference parameter modes New type-secure extensible iostream mechanism Function call -style cast notation New mutable type qualifier New bool boolean type 7

Default values for function parameters Operator and function overloading Variable declarations may occur anywhere statements may appear within a block User-defined conversion operators Static data initializers may be arbitrary expressions 8

Rvalue references and move constructors constexpr Modified definition of PODs Extern templates Initializer lists and uniform initialization Type inference with auto Range-based for loops Lambda functions Alternative function syntax 9

Object construction improvement Explicit overrides and finals Null pointer constant Strongly typed enumerations Explicit conversion operators Alias templates Variadic templates Multi-threading And much more. 10

Persistence Garbage Collection Distribution 11

Focus on concepts and programming techniques Don t get lost in language features Learn C++ to become a better programmer More effective at designing and implementing Design Patterns C++ supports many different programming style Learn C++ gradually Don t have to know every detail of C++ to write a good C++ program 12

Find a good book Learn to read language documentation 13

Bare-bones implementation typedef int T; #define MAX_STACK 100 /* const int MAX_STACK = 100; */ T stack[max_stack]; int top = 0; T item = 10; stack[top++] = item; // push... item = stack[--top]; // pop Not very abstract 14

Define the interface to a Stack in stack.h /* Type of Stack element. */ typedef int T; /* Stack interface. */ int create (int size); int destroy (void); void push (T new_item); void pop (T *old_top); void top (T *cur_top); int is_empty (void); int is_full (void); 15

#include "stack.h" static int top_, size_; /* Hidden within this file. */ static T *stack_; int create (int size) { top_ = 0; size_ = size; stack_ = malloc (size * sizeof (T)); return stack_ == 0? -1 : 0; } void destroy (void) { free ((void *) stack_); } void push (T item) { stack_[top_++] = item;} void pop (T *item) { *item = stack_[--top_]; } void top (T *item) { *item = stack_[top_ - 1]; } int is_empty (void) { return top_ == 0; } int is_full (void) { return top_ == size_; } 16

Use case #include "stack.h" void foo (void) { T i; push (10); /* Oops, forgot to call create! */ push (20); pop (&i); destroy (); } Main problems The programmer must call create() first & destroy() last There is only one stack & only one type of stack Namespace pollution 17

An ADT Stack interface in C typedef int T; typedef struct { size_t top_, size_; T *stack_; } Stack; int Stack_create (Stack *s, size_t size); void Stack_destroy (Stack *s); void Stack_push (Stack *s, T item); void Stack_pop (Stack *, T *item); /* Must call before pop ing */ int Stack_is_empty (Stack *); /* Must call before push ing */ int Stack_is_full (Stack *); /*... */ 18

An ADT Stack implementation in C #include "stack.h" int Stack_create (Stack *s, size_t size) { s->top_ = 0; s->size_ = size; s->stack_ = malloc (size * sizeof (T)); return s->stack_ == 0? -1 : 0; } void Stack_destroy (Stack *s) { free ((void *) s->stack_); s->top_ = 0; s->size_ = 0; s->stack_ = 0; } void Stack_push (Stack *s, T item){ s->stack_[s->top_++] = item; } void Stack_pop (Stack *s, T *item){ *item = s->stack_[--s->top_]; } int Stack_is_empty (Stack *s) { return s->top_ == 0; } 19

Use case void foo (void) { Stack s1, s2, s3; /* Multiple stacks! */ T item; Stack_pop (&s2, &item); /* Pop d empty stack */ /* Forgot to call Stack_create! */ Stack_push (&s3, 10); s2 = s3; /* Disaster due to aliasing!!! */ /* Destroy uninitialized stacks! */ Stack_destroy (&s1); Stack_destroy (&s2); } 20