Introducing C++ David Chisnall. March 15, 2011

Similar documents
G52CPP C++ Programming Lecture 12

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Data Abstraction. Hwansoo Han

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Chapter 5 Object-Oriented Programming

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

C++ Inheritance and Encapsulation

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

C++ (Non for C Programmer) (BT307) 40 Hours

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

C++ Inheritance and Encapsulation

Object Oriented Programming. Solved MCQs - Part 2

Method Resolution Approaches. Dynamic Dispatch

Today s Big Adventure

CS153: Compilers Lecture 11: Compiling Objects

And Even More and More C++ Fundamentals of Computer Science

Today s Big Adventure

Overriding Variables: Shadowing

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Introduction to C++ with content from

Supporting Class / C++ Lecture Notes

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Runtime Environments, Part II

Polymorphism. Zimmer CSCI 330

C++ internals. Francis Visoiu Mistrih - 1 / 41

Binding and Variables

Inheritance, Polymorphism and the Object Memory Model

C++ Important Questions with Answers

TDDE18 & 726G77. Functions

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

More C++ : Vectors, Classes, Inheritance, Templates

Type Hierarchy. Lecture 6: OOP, autumn 2003


More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

COMP322 - Introduction to C++

C++ Yanyan SHEN. slide 1

Object Orientated Analysis and Design. Benjamin Kenwright

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Fundamentals of Programming Languages

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

CS3157: Advanced Programming. Outline

G52CPP C++ Programming Lecture 9

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

CSE 303: Concepts and Tools for Software Development

CSE 307: Principles of Programming Languages

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

Introduction to Programming Using Java (98-388)

Java Object Oriented Design. CSC207 Fall 2014

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

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

CSE351 Winter 2016, Final Examination March 16, 2016

Final CSE 131B Spring 2004

Fast Introduction to Object Oriented Programming and C++

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

Subtyping (Dynamic Polymorphism)

Object Oriented Design

VIRTUAL FUNCTIONS Chapter 10

What about Object-Oriented Languages?

Programming in C and C++

What are the characteristics of Object Oriented programming language?

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

Functions in C C Programming and Software Tools

CS260 Intro to Java & Android 03.Java Language Basics

Procedure and Object- Oriented Abstraction

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

C++ Modern and Lucid C++ for Professional Programmers

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

CIS 190: C/C++ Programming. Classes in C++

ECE 3574: Dynamic Polymorphism using Inheritance

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

EL6483: Brief Overview of C Programming Language

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

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

Lectures 5-6: Introduction to C

Lecturer: William W.Y. Hsu. Programming Languages

Instantiation of Template class

Advanced C++ Topics. Alexander Warg, 2017

Dynamic Dispatch and Duck Typing. L25: Modern Compiler Design

Passing arguments to functions by. const member functions const arguments to a function. Function overloading and overriding Templates

Inheritance -- Introduction

Java and C CSE 351 Spring

QUIZ. Can you find 5 errors in this code?

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011

An introduction to C++ template programming

Midterm Exam CS 251, Intermediate Programming March 6, 2015

Introduction to Programming (Java) 4/12

AN OVERVIEW OF C++ 1

G52CPP C++ Programming Lecture 13

abstract binary class composition diamond Error Exception executable extends friend generic hash implementation implements

Fibonacci in Lisp. Computer Programming: Skills & Concepts (CP1) Programming Languages. Varieties of Programing Language

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

OBJECT ORIENTED PROGRAMMING

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

Transcription:

Introducing C++ David Chisnall March 15, 2011

Why Learn C++? Lots of people used it to write huge, unmaintainable code......which someone then gets paid a lot to maintain.

C With Classes Predecessor of C++ Added Simula-like classes to C Not a terrible language

Then It All Started To Go Wrong... Strong typing (kind of) References as well as pointers Reusing keywords Templates Operator overloading These are just some of the exciting features that you can use to make C++ code completely unreadable!

Object Orientation Or, how to completely miss the point C has structures C++ has classes C++ classes are C structures Only with some confusing things added

Member Functions A bit like methods, in an object-oriented language // C++ struct Foo int a; void get_a ( void ); // C struct Foo int a; void get_a ( struct Foo * this );

Subclassing? // C++ struct Foo int a; void get_a ( void ); struct Bar : Foo void get_a ( void ); void get_a ( struct Foo * this ); Which is called if I do x.get_a()? Depends on the static (declared) type of x;

Virtual? // C++ struct Foo int a; virtual void get_a ( void ); struct Bar : Foo virtual void get_a ( void ); void get_a ( struct Foo * this ); Now which is called if I do x.get_a()? Depends on the run-time (real) type of x;

How Does That Work? // C version : static void foo_ get_ a ( struct Foo *); static void bar_ get_ a ( struct Bar *); struct vtable void (* get_a )( struct Foo *); struct vtable foo_ vtable = foo_ get_ a struct vtable bar_ vtable = bar_ get_ a Each class has a vtable. Table of function pointers, used for resolving calls

How VTables are Used // C version struct Foo // Not visible to C ++ code struct vtable vtable ; int a; struct Foo f; // Compiler generates this f-> vtable = foo_vtable ; // C++ f. get_a () is ( roughly ) equivalent to: f-> vtable -> get_a (&f);

Performance of virtual Every virtual call involves an indirect call In position-independent code, so do non-virtual calls You often get better cache performance from the virtual lookup It s much harder to inline virtual calls So generally they re slower

Function Overloading // This is not allowed in C: float max ( float a, float b) return a>b? a : b; } int max ( int a, int b) return a>b? a : b; } The function that is called will be determined by the arguments

How Does This Work? $ cat simple.c int max(int a, int b) return a>b? a : b; } $ cc -c simple.c $ nm simple.o 0000000000000000 T max $ c++ -c overload.cc $ nm overload.o 0000000000000000 T _Z3maxff 000000000000003e T _Z3maxii $ c++filt _Z3maxff max(float, float) C++ version does name mangling Parameter types encoded in function names

Multiple Inheritance struct A int a; struct B float a; // C inherits from A and B struct C : A, B int c; int main ( void ) C c; printf ("A: %p\n", (A*)&c); printf ("B: %p\n", (B*)&c); printf ("C: %p\n", (C*)&c); }

Pointer Casts Do Arithmetic in C++! $ c++ inherit.cc $./a.out A: 0x7fff5fbff860 B: 0x7fff5fbff864 C: 0x7fff5fbff860 Multiple inheritance means you must be able to turn a pointer to an object into a pointer to any superclass This means that pointer casting must involve arithmetic This leads to lots of subtle problems

How Multiple Inheritance Works // Same example, this time in C struct A int a; struct B float a; struct C struct A A; int c; struct B B; Memory layout is not guaranteed by C++. Different compilers may do things differently.

No More Type Escaping C lets you cast any pointer type to and from void* C++ can t let you do this, because pointer casts can do arithmetic depending on the types

class } A Data Hiding Something C++ pretends to have private : int a; // Accessible only from this class protected : int b; // Accessible from any subclass public : int c; // Accessible from anywhere C++ has somewhat pointless access specifiers Only enforced by the compiler, not checked at runtime Can be bypassed by pointer arithmetic All fields must be specified in the header, so no actual hiding is possible

Real Data Hiding // In the header : typedef struct foo * foo_ t ; int somemethod ( foo_t a); // In a single source file : struct foo int a; Works in C too!

// In a header : class Public A Slightly More C++ Way // Factory method static Public * Create (); // Pure virtual method virtual int somemethod () = 0; // In a source file : class Private : public Public int a; virtual int somemethod () return a; } Public * Public :: Create () return new Private () ;}

Questions?