C++ without Classes. CMSC433, Fall 2001 Programming Language Technology and Paradigms. More C++ without Classes. Project 1. new/delete.

Similar documents
Administrivia. CMSC433, Fall 2001 Programming Language Technology and Paradigms. Administrivia (cont.) Project 1. Copy constructor.

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

C++ Programming: Polymorphism

OBJECT ORIENTED PROGRAMMING USING C++

CS3157: Advanced Programming. Outline

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

Polymorphism. Zimmer CSCI 330

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

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

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

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

Java Object Oriented Design. CSC207 Fall 2014

Conformance. Object-Oriented Programming Spring 2015

COMP 2355 Introduction to Systems Programming

Instantiation of Template class

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009

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

Tokens, Expressions and Control Structures

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

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

Lecture Topics. Administrivia

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

Data Abstraction. Hwansoo Han

Dr. Md. Humayun Kabir CSE Department, BUET

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

These new operators are intended to remove some of the holes in the C type system introduced by the old C-style casts.

Written by John Bell for CS 342, Spring 2018

C++ Casts and Run-Time Type Identification

Object Oriented Software Design II

CMSC 132: Object-Oriented Programming II

CSE 303: Concepts and Tools for Software Development

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

CS105 C++ Lecture 7. More on Classes, Inheritance

Chapter 5 Object-Oriented Programming

INSTRUCTIONS TO CANDIDATES

Where do we stand on inheritance?

Programming in C and C++

2 ADT Programming User-defined abstract data types

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

G52CPP C++ Programming Lecture 16

CMSC 132: Object-Oriented Programming II

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

Increases Program Structure which results in greater reliability. Polymorphism

ECE 3574: Dynamic Polymorphism using Inheritance

CS11 Advanced C++ Fall Lecture 3

02 Features of C#, Part 1. Jerry Nixon Microsoft Developer Evangelist Daren May President & Co-founder, Crank211

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

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

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Ch. 12: Operator Overloading

C++ Inheritance and Encapsulation

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

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

Laboratorio di Tecnologie dell'informazione

C++ Coding Standards and Practices. Tim Beaudet March 23rd 2015

What is Inheritance?

Lecture 16: Static Semantics Overview 1

CS304- Object Oriented Programming LATEST SOLVED MCQS FROM FINALTERM PAPERS. MC

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Absolute C++ Walter Savitch

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

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

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

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

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

377 Student Guide to C++

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

W3101: Programming Languages C++ Ramana Isukapalli

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Critique this Code. Hint: It will crash badly! (Next slide please ) 2011 Fawzi Emad, Computer Science Department, UMCP

Object-Oriented Programming

QUIZ. Can you find 5 errors in this code?

Laboratorio di Tecnologie dell'informazione. Ing. Marco Bertini

Chapter 1 Getting Started

Making Inheritance Work: C++ Issues

Making Inheritance Work: C++ Issues

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

Lecture 13: more class, C++ memory management

Object-Oriented Principles and Practice / C++

EL2310 Scientific Programming

COMP322 - Introduction to C++ Lecture 10 - Overloading Operators and Exceptions

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

CH. 2 OBJECT-ORIENTED PROGRAMMING

EL2310 Scientific Programming

CPS 506 Comparative Programming Languages. Programming Language

Lecture 10: building large projects, beginning C++, C++ and structs

Polymorphism. Arizona State University 1

Object-Oriented Programming, Iouliia Skliarova

VIRTUAL FUNCTIONS Chapter 10

CMSC 330: Organization of Programming Languages

04-24/26 Discussion Notes

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Declarations and Access Control SCJP tips

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

G52CPP C++ Programming Lecture 13

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

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

PIC 10A Objects/Classes

Transcription:

CMSC433, Fall 2001 Programming Language Technology and Paradigms Adam Porter Sept. 4, 2001 C++ without Classes Don t need to say struct New libraries function overloading confusing link messages default parameters void * C++ requires explicit conversion from void * CMSC 433, Fall 2001 - Adam Porter 3 More C++ without Classes Project 1 Dynamic initialization of globals no way to control order between files References Compiler treats as pointers Programmer doesn t Once initialized to reference an address, cannot be made to reference a different address Used for pass-by-reference parameters Project 1 - C++ - due Wed, Sep. 19th C++ Linked List and Expanding Int Array don t use other C++ libraries Expanding Int array should used shared representation, copy on write so that copying is fast Info on web page under Projects link CMSC 433, Fall 2001 - Adam Porter 4 CMSC 433, Fall 2001 - Adam Porter 7 int x,y; int *p = &x; int &q = y; References q++; // increments y (*p)++; // increments x q = x; // assigns value of x to y p=&y; // makes p point to y; CMSC 433, Fall 2001 - Adam Porter 18 new/delete use new A to allocate one A use new A[size] to allocate an array of size A s use delete to free one A use delete[] to free an array of A new/delete invoke constructors/destructors for all elements of an array delete[] must be given pointer to first element CMSC 433, Fall 2001 - Adam Porter 19 1

Data Abstraction Avoid name clashes Hide implementation Override standard functionality CMSC 433, Fall 2001 - Adam Porter 20 Without OO: IntArray example With OO: class IntArray{ public: void IA_init(IntArray *ia); IntArray(); void IA_cleanup(IntArray * ia); ~IntArray(); void IA_setSize(IntArray * ia, int value); void setsize(int value); int IA_getSize(IntArray * ia); int getsize(); void IA_setElem(IntArray * ia, void setelem(int index, int index, int value); int value); int IA_getElem(IntArray * ia, int index); int getelem(int index); private: CMSC 433, Fall 2001 - Adam Porter 21 Access control Access levels A member can be public/protected/private Once declared, access specification remains in effect until changed (e.g., for following members For classes, default access is private For structs, default access is public no other differences between classes and structs public any function/method allowed access private - only methods in the class allowed access protected - like private, except that subclasses (derived classes) can access inherited members CMSC 433, Fall 2001 - Adam Porter 22 CMSC 433, Fall 2001 - Adam Porter 23 Protected access Class A { private: int x; protected: int y; Class B : public A { static int foo(a a, B b) { int i = a.x; // illegal int j = a.y; // illegal int k = b.x; // illegal int n = b.y; // LEGAL CMSC 433, Fall 2001 - Adam Porter 24 Friends A class X can declare a class Y or a function f as a friend Gives Y or f access to all of X s private and protected data Friendship is not transitive If X declares Y as a friend, and Y declares Z as a friend, Z can t see X s private data It s not inherited either a class derived from Y cannot access X s private or protected data CMSC 433, Fall 2001 - Adam Porter 25 2

Hidden functions void/default constructor class A { A(); // void constructor A(const A &); // copy constructor A& operator=(const A &) // operator= ~A(); // destructor Invoked whenever an instance is created without being initialized. If no constructor is provided, default public one is created invokes void constructor on each base class and instance variable (member) members of built-in types not initialized CMSC 433, Fall 2001 - Adam Porter 26 CMSC 433, Fall 2001 - Adam Porter 27 copy constructor Invoked to create a new value from an old value for initialization for parameters passed by value for objects returned as values If no copy constructor supplied, default public one created invokes copy constructor for each base class and instance variable (and copies bits for built-in types) almost never correct if you have pointers If implicit copy constructor overridden, must also override implicit default constructor CMSC 433, Fall 2001 - Adam Porter 28 operator= If no operator=() defined, defines default public one does operator= on each base class and instance variable Watch for a = a A::operator=(const A&) usually returns A& allows a = b = c; CMSC 433, Fall 2001 - Adam Porter 29 Pointer obligations For the hidden functions, if you have a member variable that is a pointer, the default functions are often inappropriate For each pointer, decide if it is the unique pointer to an object if not, need to control updates to that object and to figure out when to free referenced object if so, need to duplicate object rather than pointer CMSC 433, Fall 2001 - Adam Porter 33 Pointer obligations, with concise syntax class IntArray { int *rep; int size; public: IntArray(int sz) : rep(new int[sz]), size(sz) {; IntArray(const IntArray &a) : rep(new int[a.size]), size(a.size) {memcpy(rep,a.rep,sizeof(int)*a.size); ~IntArray() { delete [] rep; CMSC 433, Fall 2001 - Adam Porter 34 3

operator = IntArray& operator=(const IntArray &a) { if (this == &a) return *this; if (size!= a.size) { delete [] rep; rep = new int[a.size]; size = a.size; memcpy(rep, a.rep, sizeof(int)*size); return *this; CMSC 433, Fall 2001 - Adam Porter 35 One arg constructors A one argument constructor is also used for type conversion A(int sz) allows an int to be used anywhere an A is expected only one level of conversion is supported if a B is required and an int is supplied, the system won t convert an int to an A, which is then converted to a B Can mark constructor as explicit to avoid this feature CMSC 433, Fall 2001 - Adam Porter 36 Subtyping Derived classes if class D has class B as a public base type a pointer to a D can be provided anywhere a pointer to a B is expected a reference to a D can be provided anywhere a reference to a B is expected class D should fulfill B s public contract something that expects a pointer to a Band is given a pointer to a D shouldn t be surprised Virtual functions Without virtual functions, B s functionality will be invoked on objects pointed to by a B pointer determined from explicit type of pointer With virtual functions dispatching based on run-time type of object pointed to compile-time type of arguments must match argument types exactly to override in derived class CMSC 433, Fall 2001 - Adam Porter 37 CMSC 433, Fall 2001 - Adam Porter 38 Non-virtual functions Common Subytping Errors Non-virtual functions have confusing semantics function you get depends on type of pointer used to reference the object But virtual functions may be more expensive to invoke Should probably make functions non-virtual only if never overridden final in Java An array of D s can be given to someone who expects an array of B s B s public contract may involve assigning another B to it What is the right thing to do when a D is assigned a B? when a B is assigned a D? CMSC 433, Fall 2001 - Adam Porter 39 CMSC 433, Fall 2001 - Adam Porter 40 4

Operator= and subtyping struct B { B& virtual operator=(const B & b) { ; struct D : public B { D & virtual operator=(const D & d) { ; B b; D d; B* pbb = &b; B* pbd = &d; b = d; d = b; *pbb = *pbd; *pbd = *pbb; // B::operator=(const B&) // Illegal (why?) // B::operator=(const B&) // B::operator=(const B&) Virtual operator= virtual declaration didn t matter; arguments not identical struct B { B& virtual operator=(const B & b) { ; struct D : public B { D & virtual operator=(const B & b) { ; D & virtual operator=(const D & d) { ; CMSC 433, Fall 2001 - Adam Porter 41 CMSC 433, Fall 2001 - Adam Porter 42 Similar problems elsewhere Copy constructor overriding not an issue watch out for omitting & for a reference parameter operator== almost identical set of problems to operator= Covariance of return types When you override a function, your return type can be a subtype of the method you are overriding struct B { virtual B* getchild(); struct D : public B { virtual D * getchild(); CMSC 433, Fall 2001 - Adam Porter 43 CMSC 433, Fall 2001 - Adam Porter 44 Covariance Covariance of return types is OK Somebody who expects a B*, is given a D* invokes getchild() on it Expects a B*, is given a D* No surprise Not OK for argument types Argument types can t be covariant Consider if D::f overrode B::f in struct B {virtual void f(b* p); struct D : public B {virtual void f(d* p); Somebody who expects a B*, is given a D* invokes f(new B()) on it D::f would be surprised! expected a D*, got a B* CMSC 433, Fall 2001 - Adam Porter 45 CMSC 433, Fall 2001 - Adam Porter 46 5

Function hiding Object Hierarchy Design In class B, if you define f(int) In derived class D, you define f(char *) f(int) can t be invoked on objects of type D except though a B* pointer violates contractual obligation Why? Hides operator=? prevents surprises composition/aggregation have components as instance variables derivation have components as base classes CMSC 433, Fall 2001 - Adam Porter 47 CMSC 433, Fall 2001 - Adam Porter 48 Engine vs. Car Should a Car have an Engine as a component or as a base type? Animal class Say we have classes for Fish, Dog, Eagle, Whale and Hippo. Should they have a common base class? What methods should it support? age()? speed()? fly()? eat()? CMSC 433, Fall 2001 - Adam Porter 49 CMSC 433, Fall 2001 - Adam Porter 50 Heterogeneous collections Will you ever need a collection that contains objects that all is-a/has-a/are-a A If so, use is-a (derivation) Will you need a collection of engines and cars? Will you need a collection of animals? CMSC 433, Fall 2001 - Adam Porter 51 Casts in C++ static_cast C style cast, with different syntax dynamic_cast cast from base class ptr to derived class ptr returns null if not instance of derived class const_cast to remove/add const attribute from a pointer reinterpret_cast interpret bits CMSC 433, Fall 2001 - Adam Porter 55 6

Casting between classes static_cast In C++ with multiple inheritance, casting between a ptr to a base type and a ptr to a derived type may require adding/subtracting offset may require run-time lookups if you have virtual base classes Reusing the bit pattern likely to be badly wrong So C++ provides various cast operators to provide some safety guarantees For pointers, assumes correct, doesn t look at object B *b = static_cast<b *>(d); // OK D *d = static_cast<d *>(b); //? Also used for casting between built-in types integers, doubles,... replaces C style cast also can be used to cast from a void* CMSC 433, Fall 2001 - Adam Porter 56 CMSC 433, Fall 2001 - Adam Porter 57 dynamic_cast const_cast Looks at object to determine: if legal (returns null otherwise) pointer value adjustment needed Generally used for safely casting from base type to derived type Requires that code be compiled with RTTI Run time type information To remove or add const or volatile declarations sometimes necessary because const is part of a (member) function s type signature can be useful to invoke a non-const member function on a const object assuming that the function behaves properly in this situation CMSC 433, Fall 2001 - Adam Porter 58 CMSC 433, Fall 2001 - Adam Porter 59 reinterpret_cast Abstract classes interpret bits VERY dangerous, no checks No adjustments applied one valid use is to cast out of void* and potentially useful for explicitly placing object in memory at a fixed location with new An abstract class cannot be directly instantiated although subclasses may In C++, any class with an abstract function is abstract define a method to be abstract by setting it = 0 (no implementation) CMSC 433, Fall 2001 - Adam Porter 60 CMSC 433, Fall 2001 - Adam Porter 61 7

Constructor chaining What does B() print? For a constructor, you can give the arguments to the constructors for: the base classes the instance variables If nothing provided, void constructor used Ex. class B : public A { C c1, c2; D d1; public: B() : A(42), c1( hello ), c2( hi ), d1() {. struct A { A() { g(); ; virtual void f() { cout << A ; ; virtual void g() { f() ; class B : public A { B() { g(); ; virtual void f() { cout << B ; ; CMSC 433, Fall 2001 - Adam Porter 62 CMSC 433, Fall 2001 - Adam Porter 63 What happened? Constructor B() chains to constructor for A() while doing constructor for A(), object is an A Then, perform void constructor for B now, object is a B Namespaces What if I ve defined a List class And you ve written a different List class Can my code and your code be used in the same program? No - Can t have two different classes named the same Namespaces to the rescue Similar to packages in Java CMSC 433, Fall 2001 - Adam Porter 64 CMSC 433, Fall 2001 - Adam Porter 65 Using namespaces namespace Foo { ; namespace Foo { class List { ; ; namespace Bar { class List { ; ; Foo::List queue = new Foo::List(); All new definitions are in namespace Foo Make all definitions previously declared in Foo available OK to open and close a namespace: namespace A { ; namespace B { ; namespace A { ; CMSC 433, Fall 2001 - Adam Porter 66 CMSC 433, Fall 2001 - Adam Porter 67 8

namespace needed to add definitions Can use :: to give definition of a name previously defined But not to define a new name void Foo::f(int x) { ; Foo::f(int) must have been previously declared using namespace using Foo; makes all names from Foo available until end of file or namespace using Foo::List; makes List from namespace Foo available as List CMSC 433, Fall 2001 - Adam Porter 68 CMSC 433, Fall 2001 - Adam Porter 69 Inheriting namespaces namespaces aren t databases namespace Foo { using A; using B; class List { ; ; using Foo { // Foo::List and all names from A // and B available You see X in namespace Foo only if the compiler sees X declared in Foo at some point earlier in the file. doesn t affect need to carefully link header files CMSC 433, Fall 2001 - Adam Porter 70 CMSC 433, Fall 2001 - Adam Porter 71 namespace aliases Exceptions What if two developers both write code using the namespace Parser? back to our old problem of name clashes Partial solution using namespace aliases; namespace Parser = als_parser; Makes long names bearable still have to come up with unique long names Java style: edu_umd_cs_als_parser? throw exp; -- throws an exception exp try { catch (ExceptionType e) { if, while executing a try block, an exception is thrown that could be passed to a function taking an ExceptionType as an argument, the catch clause is invoked with e bound to value thrown CMSC 433, Fall 2001 - Adam Porter 72 CMSC 433, Fall 2001 - Adam Porter 73 9

Exception example struct IndexOutOfBounds{; char String::getChar(int i) { if (i < 0 i >= length) throw IndexOutOfBounds(); ; try { c = s.getchar(42); catch (IndexOutOfBounds err) { cout << Index out of bounds ; Exception hierarchy You can create a hierarchy of exceptions Exception IOException BoundsException Using derived classes catch a reference to the base class (parameter Base&) if a derived class is thrown, you catch it catch value of the base class (parameter Base) if derived class is thrown, copy constructor for the base class is invoked, using the obj thrown as arg CMSC 433, Fall 2001 - Adam Porter 74 CMSC 433, Fall 2001 - Adam Porter 75 More exceptions Exception declarations If you aren t going to do anything with the exception you caught omit variable name in catch You can throw other values ints, doubles don t do this You can declare the set of exceptions a function might throw not checked at compile time Checked at run-time if you declare the exceptions you throw and you throw something not on that list function unexpected() is called which by default calls terminate() which by default calls abort() CMSC 433, Fall 2001 - Adam Porter 76 CMSC 433, Fall 2001 - Adam Porter 77 Declaring exceptions Declare you might throw X or Y int f(int i) throw (X,Y) { ; Declare you don t throw any exceptions int g(int i) throw () { ; Declare that you don t know what exceptions you throw int h(int i) { ; Using destructors for final actions Sometimes, you want to ensure you take some action when leaving a scope no matter how you leave the scope exception, return, fall off the bottom Destructors get called no matter how you leave the scope use them CMSC 433, Fall 2001 - Adam Porter 78 CMSC 433, Fall 2001 - Adam Porter 79 10

Example - File closer class FileCloser { const FILE *f; public: FileCloser(FILE *f) { this.f = f; ; virtual ~FileCloser() { f.close(); CMSC 433, Fall 2001 - Adam Porter 80 Example - Monitor lock class MonitorLock { Monitor & m; public: MonitorLock(Monitor & mon) : m(mon) { m.lock(); virtual ~MonitorLock() { m.unlock(); CMSC 433, Fall 2001 - Adam Porter 81 11