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

Similar documents
C++ Yanyan SHEN. slide 1

Objects, Encapsulation, Inheritance (2)

Object typing and subtypes

Subtyping (Dynamic Polymorphism)

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

The Java Programming Language

Data Abstraction. Hwansoo Han

Thanks! Review. Course Goals. General Themes in this Course. There are many programming languages. Teaching Assistants. John Mitchell.

VIRTUAL FUNCTIONS Chapter 10

Object-oriented Programming. Object-oriented Programming

C++ Part 2 <: <: C++ Run-Time Representation. Smalltalk vs. C++ Dynamic Dispatch. Smalltalk vs. C++ Dynamic Dispatch

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

CPS 506 Comparative Programming Languages. Programming Language

C++ Important Questions with Answers

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

Objects. Deian Stefan (Adopted from my & Edward Yang s CS242 slides)

G Programming Languages - Fall 2012

Advanced oo concepts Specialization of behaviour? Multiple inheritance - alternatives to. Inner classes Classes

COMP322 - Introduction to C++ Lecture 09 - Inheritance continued

Simula 67. Simula and Smalltalk. Comparison to Algol 60. Brief history. Example: Circles and lines. Objects in Simula

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

Exceptions and Continuations. Lecture #19: More Special Effects Exceptions and OOP. Approach II: Non-Standard Return. Approach I: Do Nothing

Tokens, Expressions and Control Structures

CSE 401/M501 Compilers

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Outline. Central concepts in OO languages Objects as activation records (Simula) Dynamically-typed object-oriented languages

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Programming Languages: OO Paradigm, Polymorhism and Class Members

Conformance. Object-Oriented Programming Spring 2015

September 10,

What about Object-Oriented Languages?

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

Chapter 10 :: Data Abstraction and Object Orientation

CSE 307: Principles of Programming Languages

Polymorphism Part 1 1

Types and Classes. I II From predefined (simple) and user-defined (composite) types via Abstract data types

Procedure and Object- Oriented Abstraction

Fast Introduction to Object Oriented Programming and C++

Chapter 9 :: Data Abstraction and Object Orientation

Operator Dot Wording

Inheritance, Polymorphism and the Object Memory Model

QUIZ Friends class Y;

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Instantiation of Template class

CS558 Programming Languages Winter 2013 Lecture 8

Derived Classes in C++

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University

CS-XXX: Graduate Programming Languages. Lecture 23 Types for OOP; Static Overloading and Multimethods. Dan Grossman 2012

Polymorphism. Contents. Assignment to Derived Class Object. Assignment to Base Class Object

Some instance messages and methods

CMSC 132: Object-Oriented Programming II

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

C++ Programming: Polymorphism

Major Differences between Java and C++ Programming in C++ Multiple Inheritance

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

COMP 2355 Introduction to Systems Programming

Increases Program Structure which results in greater reliability. Polymorphism

Absolute C++ Walter Savitch

COMP322 - Introduction to C++

Compiler construction 2009

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

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Object Oriented Software Design II

Object Oriented Paradigm

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

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Simula and Smalltalk. Comparison to Algol 60. Brief history. Example: Circles and lines. Objects in Simula. John Mitchell

CS-202 Introduction to Object Oriented Programming

Operator Dot Wording

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

Chapter 10 Object-Oriented Programming

Object Oriented Programming in C++ Basics of OOP

Ch. 10: Name Control

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

Lecture Notes on Programming Languages

Rushikesh K Joshi. Department of Computer Science and Engineering Indian Institute of Technology Bombay

C++ Inheritance and Encapsulation

Lecture 23: Object Lifetime and Garbage Collection

Programming Languages Third Edition. Chapter 7 Basic Semantics

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

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

ECE 3574: Dynamic Polymorphism using Inheritance

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

CSE Lecture 3: Objects 2 September Nate Nystrom University of Texas at Arlington

Object-Oriented Software Engineering. Chapter 2: Review of Object Orientation

Lecturer: William W.Y. Hsu. Programming Languages

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

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

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 2: Review of Object Orientation

CSC 533: Organization of Programming Languages. Spring 2005

Java Object Oriented Design. CSC207 Fall 2014

Resource Management With a Unique Pointer. Resource Management. Implementing a Unique Pointer Class. Copying and Moving Unique Pointers

Method Resolution Approaches. Dynamic Dispatch

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

C++ Inheritance and Encapsulation

Chapter 5 Object-Oriented Programming

Short Notes of CS201

Object Oriented Programming. Solved MCQs - Part 2

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

Transcription:

1 CS 242 History C++ John Mitchell C++ is an object-oriented extension of C C was designed by Dennis Ritchie at Bell Labs used to write Unix based on BCPL C++ designed by Bjarne Stroustrup at Bell Labs His original interest at Bell was research on simulation Early extensions to C are based primarily on Simula Called C with classes in early 1980 s Popularity increased in late 1980 s and early 1990 s Features were added incrementally Classes, templates, exceptions, multiple inheritance, type tests... Design Goals Provide object-oriented features in C-based language, without compromising efficiency Backwards compatibility with C Better static type checking Data abstraction Objects and classes Prefer efficiency of compiled code where possible Important principle If you do not use a feature, your compiled code should be as efficient as if the language did not include the feature. (compare to Smalltalk) How successful? Given the design goals and constraints, this is a very well-designed language Many users -- tremendous popular success However, very complicated design Many specific properties with complex behavior Difficult to predict from basic principles Most serious users chose subset of language Full language is complex and unpredictable Many implementation-dependent properties Language for adventure game fans Significant constraints C has specific machine model Access to underlying architecture No garbage collection Consistent with goal of efficiency Need to manage object memory explicitly Local variables stored in activation records Objects treated as generalization of structs, so some objects may be allocated on stack Stack/heap difference is visible to programmer Overview of C++ Additions and changes not related to objects type bool pass-by-reference user-defined overloading function templates

2 C++ Object System Object-oriented features Classes Objects, with dynamic lookup of virtual functions Inheritance Single and multiple inheritance Public and private base classes Subtyping Tied to inheritance mechanism Encapsulation Some good decisions Public, private, protected levels of visibility Public: visible everywhere Protected: within class and subclass declarations Private: visible only in class where declared Friend functions and classes Careful attention to visibility and data abstraction Allow inheritance without subtyping Better control of subtyping than without private base classes Some problem areas Casts Sometimes no-op, sometimes not (esp multiple inher) Lack of garbage collection Memory management is error prone Constructors, destructors are helpful though Objects allocated on stack Better efficiency, interaction with exceptions BUT assignment works badly, possible dangling ptrs Overloading Too many code selection mechanisms Multiple inheritance Efforts at efficiency lead to complicated behavior Sample class: one-dimen. points class Pt { Pt(int xv); Overloaded constructor Pt(Pt* pv); Public read access to private data virtual void move(int dx); Virtual function protected: void setx(int xv); Protected write access private: int x; Private member data Virtual functions Member functions are either Virtual, if explicitly declared or inherited as virtual Non-virtual otherwise Virtual functions Accessed by indirection through ptr in object May be redefined in derived (sub) classes Non-virtual functions Are called in the usual way. Just ordinary functions. Cannot redefine in derived classes (except overloading) Pay overhead only if you use virtual functions Sample derived class class ColorPt: public Pt { Public base class gives supertype ColorPt(int xv,int cv); ColorPt(Pt* pv,int cv); Overloaded constructor ColorPt(ColorPt* cp); int getcolor(); Non-virtual function virtual void move(int dx); Virtual functions virtual void darken(int tint); protected: void setcolor(int cv); Protected write access private: int color; Private member data

3 Run-time representation Compare to Smalltalk Point object x 3 Point vtable Code for move Point object 2 3 Point class Template x y Method dictionary newx:y:... move... ColorPoint object x 5 c blue ColorPoint vtable Code for move Code for darken ColorPoint object 4 5 red ColorPoint class Template x y color Method dictionary newx:y:c: color move Data at same offset Function pointers at same offset Why is C++ lookup simpler? Smalltalk has no static type system Code p message:pars could refer to any object Need to find method using pointer from object Different classes will put methods at different place in method dictionary C++ type gives compiler some superclass Offset of data, fctn ptr same in subclass and superclass Offset of data and function ptr known at compile time Code p->move(x) compiles to equivalent of (*(p->[1]))(p,x) if move is first function in vtable Calls to virtual functions One member function may call another class A { virtual int f (int x); virtual int g(int y); int A::f(int x) { g(i) ;} int A::g(int y) { f(j) ;} How does body of f call the right g? If g is redefined in derived class B, then inherited f must call B::g data passed to member function; see next slide This pointer (analogous to self in Smalltalk) Code is compiled so that member function takes object itself as first argument Code int A::f(int x) { g(i) ;} compiled as int A::f(A *this, int x) { this->g(i) ;} this pointer may be used in member function Can be used to return pointer to object itself, pass pointer to object itself to another function,... Non-virtual functions How is code for non-virtual function found? Same way as ordinary non-member functions: Compiler generates function code and assigns address Address of code is placed in symbol table At call site, address is taken from symbol table and placed in compiled code But some special scoping rules for classes Overloading Remember: overloading is resolved at compile time This is different from run-time lookup of virtual function

4 Scope rules in C++ Virtual vs Overloaded Functions Scope qualifiers binary :: operator, ->, and. class::member, ptr->member, object.member A name outside a function or class, not prefixed by unary :: and not qualified refers to global object, function, enumerator or type. A name after X::, ptr-> or obj. where we assume ptr is pointer to class X and obj is an object of class X refers to a member of class X or a base class of X class parent { void printclass() {printf("p "); virtual void printvirtual() {printf("p "); class child : public parent { void printclass() {printf("c "); virtual void printvirtual() {printf("c "); main() { parent p; child c; parent *q; p.printclass(); p.printvirtual(); c.printclass(); c.printvirtual(); q = &p; q->printclass(); q->printvirtual(); q = &c; q->printclass(); q->printvirtual(); } Output: p p c c p p p c Subtyping Independent classes not subtypes Subtyping in principle A <: B if every A object can be used without type error whenever a B object is required Example: Point: ColorPoint: int getcolor(); void darken(int tint); Public members Public members C++: A <: B if class A has public base class B This is weaker than necessary Why? class Point { class ColorPoint { int getcolor(); void darken(int); C++ does not treat ColorPoint <: Point as written Need public inheritance ColorPoint : public Pt Why?? Why C++ design? Function subtyping Client code depends only on public interface In principle, if ColorPoint interface contains Point interface, then any client could use ColorPoint in place of point However -- offset in virtual function table may differ Lose implementation efficiency (like Smalltalk) Without link to inheritance subtyping leads to loss of implementation efficiency Also encapsulation issue: Subtyping based on inheritance is preserved under modifications to base class Subtyping principle A <: B if an A expression can be safely used in any context where a B expression is required Subtyping for function results If A <: B, then C A <: C B Subtyping for function arguments If A <: B, then B C <: A C Terminology Covariance: A <: B implies F(A) <: F(B) Contravariance: A <: B implies F(B) <: F(A)

5 Examples If circle <: shape, then circle shape circle circle shape shape shape circle C++ compilers recognize limited forms of function subtyping Subtyping with functions class Point { virtual Point *move(int); class ColorPoint: public Point { Inherited, but repeated here for clarity int getcolor(); ColorPoint * move(int); void darken(int); In principle: can have ColorPoint <: Point In practice: some compilers allow, others have not This is covariant case; contravariance is another story Details, details This is legal class Point { virtual Point * move(int); } class ColorPoint: public Point { virtual ColorPoint * move(int); } But not legal if * s are removed class Point { virtual Point move(int); } class ColorPoint: public Point { virtual ColorPoint move(int); } Related to subtyping distinctions for object L-values and object R-values (Non-pointer return type is treated like an L-value for some reason) Subtyping and Object L,R-Values If class B : public A { } Then B r-value <: A r-value If x = a is OK, then x = b is OK provided A s operator = is public If f(a) is OK, then f(b) is OK provided A s copy constructor is public B l-value <: A l-value B* <: A* on last slide because cannot assign to return value B** <: A** Generally, X <: Y X* <: Y* is unsound. Review Why C++ requires inheritance for subtyping Need virtual function table to look the same This includes private and protected members Subtyping w/o inheritance weakens data abstraction (This is my post facto explanation; I don t know what designers think.) Possible confusion regarding inlining Cannot generally inline virtual functions Inlining is possible for nonvirtual functions These are available in C++ Not in Smalltalk since every lookup is through class Inlining is very significant for efficiency; enables further optimization. Abstract Classes Abstract class: A class without complete implementation Declare by =0 (what a great syntax!) Useful because it can have derived classes Since subtyping follows inheritance in C++, use abstract classes to build subtype hierarchies. Establishes layout of virtual function table (vtable) Example Geometry classes in appendix of reader Shape is abstract supertype of circle, rectangle,...

6 Multiple Inheritance Problem: Name Clashes Shape ReferenceCounted Rectangle RefCounted Rectangle Inherit independent functionality from independent classes class A { void virtual f() { } class B { void virtual f() { } class C : public A, public B { C* p; p->f(); // error same name in 2 base classes Possible solutions to name clash Three general approaches Implicit resolution Language resolves name conflicts with arbitrary rule Explicit resolution Programmer must explicitly resolve name conflicts Disallow name clashes Programs are not allowed to contain name clashes No solution is always best C++ uses explicit resolution Repair to previous example Rewrite class C to call A::f explicitly class C : public A, public B { void virtual f( ) { A::f( ); // Call A::f(), not B::f(); } Reasonable solution This eliminates ambiguity Preserves dependence on A Changes to A::f will change C::f vtable for Multiple Inheritance Object and classes A C B class A { int x; virtual void f(); class B { int y; virtual void g(); virtual void f(); class C: public A, public B { int z; virtual void f(); C *pc = new C; B *pb = pc; A *pa = pc; Three pointers to same object, but different static types. pa, pc pb δ C object A data B data C data A object B object C-as-A vtbl & C::f 0 C-as-B vtbl & B::g 0 & C::f Offset δ in vtbl is used in call to pb->f, since C::f may refer to A data that is above the pointer pb Call to pc->g can proceed through C-as-B vtbl δ

7 Multiple Inheritance Diamond Diamond inheritance in C++ D Text Window (A) Window (D) Text, Graphics Window (C) Graphics Window (B) Is interface or implementation inherited twice? What if definitions conflict? Standard base classes D members appear twice in C Virtual base classes class A : public virtual D { } Avoid duplication of base class members Require additional pointers so that D part of A, B parts of object can be shared A C A part B part C part D part C++ multiple inheritance is complicated in part because of desire to maintain efficient lookup B C++ Summary Objects Created by classes Contain member data and pointer to class Classes: virtual function table Inheritance Public and private base classes, multiple inheritance Subtyping: Occurs with public base classes only Encapsulation member can be declared public, private, protected object initialization partly enforced