Generic Programming with C++

Similar documents
Object Oriented Software Design II

C++ INTERVIEW QUESTIONS

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Object Oriented Software Design II

Java Interview Questions and Answers

The C Programming Language course syllabus associate level

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Chapter 5 Names, Bindings, Type Checking, and Scopes

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Advanced Data Structures

An Incomplete C++ Primer. University of Wyoming MA 5310

How to create/avoid memory leak in Java and.net? Venkat Subramaniam

CS 111 Classes I 1. Software Organization View to this point:

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Class 16: Function Parameters and Polymorphism

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

C++FA 5.1 PRACTICE MID-TERM EXAM

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

CS107L Handout 02 Autumn 2007 October 5, 2007 Copy Constructor and operator=

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

Object-Oriented Programming

Comp151. Definitions & Declarations

Glossary of Object Oriented Terms

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

10CS35: Data Structures Using C

Semantic Analysis: Types and Type Checking

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Software Engineering Techniques

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

C++ Overloading, Constructors, Assignment operator

El Dorado Union High School District Educational Services

Lecture Data Types and Types of a Language

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

7.1 Our Current Model

1.1.3 Syntax The syntax for creating a derived class is very simple. (You will wish everything else about it were so simple though.

C++ Programming Language

C++FA 3.1 OPTIMIZING C++

DATA STRUCTURES USING C

Classes and Pointers: Some Peculiarities (cont d.)

Curriculum Map. Discipline: Computer Science Course: C++

13 Classes & Objects with Constructors/Destructors

Java from a C perspective. Plan

Compile-time type versus run-time type. Consider the parameter to this function:

Introduction to Programming Block Tutorial C/C++

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

No no-argument constructor. No default constructor found

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

1. Polymorphism in C++...2

Java CPD (I) Frans Coenen Department of Computer Science

Inheritance in Programming Languages

16 Collection Classes

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Stacks. Linear data structures

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Cours de C++ Utilisations des conteneurs

C++ Crash Kurs. C++ Object-Oriented Programming

CMSC 202H. ArrayList, Multidimensional Arrays

CEC225 COURSE COMPACT

Introduction to Data Structures

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Data Structures Using C++ 2E. Chapter 5 Linked Lists

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

Software Engineering Concepts: Testing. Pointers & Dynamic Allocation. CS 311 Data Structures and Algorithms Lecture Slides Monday, September 14, 2009

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

Copyright 2001, Bill Trudell. Permission is granted to copy for the PLoP 2001 conference. All other rights reserved.

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Stack Allocation. Run-Time Data Structures. Static Structures

Chapter 3: Restricted Structures Page 1

CS193j, Stanford Handout #4. Java 2

Functions and Parameter Passing

Description of Class Mutation Mutation Operators for Java

Brent A. Perdue. July 15, 2009

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

The Advantages of Dan Grossman CSE303 Spring 2005, Lecture 25

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Java Application Developer Certificate Program Competencies

Moving from CS 61A Scheme to CS 61B Java

vector vec double # in # cl in ude <s ude tdexcept> tdexcept> // std::ou std t_of ::ou _range t_of class class V Vector { ector {

Applied Informatics C++ Coding Style Guide

Sources: On the Web: Slides will be available on:

Fundamentals of Java Programming

Habanero Extreme Scale Software Research Project

The Interface Concept

Code Refactoring and Defects

Pointers and Linked Lists

Basics of C++ and object orientation in OpenFOAM

Chapter 13 - Inheritance

CS107L Handout 04 Autumn 2007 October 19, 2007 Custom STL-Like Containers and Iterators

AP Computer Science Java Subset

Java Interfaces. Recall: A List Interface. Another Java Interface Example. Interface Notes. Why an interface construct? Interfaces & Java Types

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Transcription:

Generic Programming with C++

Generic Programming Generic programming a style of programming supported by C++ Similar to OOP trying to make it simple to reuse code, and abstracting general concepts Different from OOP in OOP, the emphasis is on data aspect, in Generic Programming, the emphasis is the algorithmic aspect Before we learn about generic programming, we'll need some C++ basics...

A typical C++ class (in three parts) The interface file: // in stack.h class Stack { public: // a constant static const int MAX_ELEMENTS = 16; // constructor Stack(); // destructor virtual ~Stack(); // dynamically bound functions virtual void push( int x ); virtual int pop(); // inlined function int getsize() { return size; protected: int size; int elements[max_elements]; ; Java programmers: don't forget the sneaky semi-colon at the end of the class defintion!

The implementation file... // in stack.cc #include "stack.h" Stack::Stack() : size(0) { for ( int i = 0; i < MAX_ELEMENTS; ++i ) { elements[i] = 0; Stack::~Stack() { // nothing to do void Stack::push( int x ) { // stack is full if ( size >= MAX_ELEMENTS - 1 ) return; // otherwise it's safe to push elements[size++] = x; int Stack::pop() { // stack is empty if ( size <= 0 ) return 0; // an exception would be better... int result = elements[0]; size--; for ( int i = 0; i < size; ++i ) { elements[i] = elements[i+1]; return result;

Using the Stack class in a program... // in main.cc #include <iostream> #include "stack.h" int main( int argc, char* argv[] ) { Stack* pstack = new Stack(); pstack->push( 1 ); pstack->push( 2 ); pstack->push( 3 ); int n; while ( n = pstack->pop() ) { cout << n << " " << endl; return 0; // compile with g++ main.cc stack.cc

Memory Management In Java, this task is done mostly in the background, is not the typical programmer's role This is done using an automatic garbage collector, which takes care of deallocating unused objects In C++, memory management is explicitly under the control of the programmer

Different Memory Models in Java only primitive types and references can be stack resident objects (including arrays) are always allocated on the heap in C++ objects can be allocated on the stack or the heap stack resident values are created automatically upon entering/exiting a method heap resident values are created using the new operator

Example // in Java void example() { int i; int a[] = new int[10]; obj ao = new obj(); // in C++ void example() { int i; int a[10]; obj ao; in Java only the primitive integer i is allocated space on the stack, while the array a and the object ao are created as heap resident values in C++ all three values reside on the stack

The stack The stack is an orderly internal data structure managed by a run-time system, which keeps track of: active function calls parameter values return address of caller local variables Objects exist until they go out of scope sometimes called automatic variables never return a pointer to a variable on the stack! Allocating memory on the stack is usually much faster than on the heap, though there is usually less space available.

Objects on the stack The size of stack resident values must be known at compile time no problem for primitive types and pointers array sizes must be known at compile time what about objects?

#include <iostream> The slicing problem class A { public: A(): dataone(2) { virtual void whoami() { cout << "class A" << endl; private: int dataone; ; class B: public A { public: B(): datatwo(4) { virtual void whoami() { cout << "class B" << endl; private: int datatwo; ; int main( int argc, char* argv[] ) { A a; B b; a.whoami(); b.whoami(); a = b; a.whoami(); // not who you think! return 0; when b is assigned to a, the compiler slices off the parts from B, leaving only an A type object.

The Heap (or free store) heap resident values are created with the new or new[] operator, which dynamically allocates memory C programmers: don't mix new and malloc, they are not equivalent! on failure, new will return a null pointer, or throw a bad_alloc exception access to these values is done through pointers (which will often be on the stack) C++ - the pointer declaration is explicitly stated, and has a significance (pointer arithmetic)

The delete operator there is no delete operator in Java, because the garbage collector deallocates unused objects automatically in C++ this task is left for the programmer, who does it using the delete operator on a pointer variable there are actually two delete operators: delete p: deletes the object pointed to by p delete[] p: deletes the array pointed to by p, and each object in the array pointed to by p if you used p = new Class[size] to allocate the objects, you must use delete[] p

Memory problems: using a value before it is initialized unpredictable results! failing to release memory no longer in use memory leak, can result in heap exhaustion! using a value after it has been deleted unpredictable results! invoking delete more than once on the same pointer depends on the heap manager, could corrupt the heap!

Destructors executed when an object is deleted or goes out of scope the name of the destructor is identical to the name of the class, prefixed with ~ e.g. ~Shape() can only be one destructor per class, no parameters or return type destructors for base classes must be virtual

default constructor no parameters Special Constructors needed to create arrays of objects copy constructor one parameter, a const reference to an instance of the same class e.g. Shape( const& Shape ) similar to cloning in Java (same deep vs. shallow copy issues to resolve) need a copy constructor for any class which has pointer or reference members also note: if your class has pointer or reference members, you should implement the assignment operator: e.g. operator=(const Shape& rhs);

Notes about memory management strive for symmetry between new and delete calls if you allocate memory in a constructor, deallocate that memory in the destructor try to hide dynamic memory inside classes if the object ends up on the stack, destructor will automatically ensure proper deallocation on the heap, objects help with by grouping complex collections of heap allocations together. Then you only have to remember to delete one object, rather than many. class HoldMyArray { public: int* const parray; HoldMyArray( int size ) { parray = new int[size]; ~HoldMyArray() { delete[] parray;

virtual functions a dynamically bound member function the virtual keyword is needed only in the base class declaration of the function remember, dynamic binding is needed for polymorphism. Pure virtual functions must be redefined by the derived class, and are resolved dynamically (at run-time) - dynamic binding Non-virtual member functions are resolved statically, i.e. the member function is selected statically (at compiletime) based on the type of the pointer (or reference) to the object.

Pointers Pointer a value that represents the address of an object Operators used: If x is an object, then &x is the address of x (here the & is an address operator, not a definition of reference types) If p is the address of an object, then *p is the object itself (the * is a dereference operator) If p is a pointer to a class (or a structure) p->a denotes we want to access a specific field (a) in this class If p contains the address of x, we say that p is a pointer that points to x Pointers have types int *p; //p is a pointer to int, or *p has type int

Arrays and Pointer Arithmetic C++ - a close association between a pointer and an array An array contains a sequence of one or more objects of the same type, it's size must be known at compile time The name of an array = a pointer to the initial element of the array A pointer is a random access iterator Pointer arithmetic moving along the elements of the array

Cast operations There are several cast operations that provide finegrained control over casting C style casts (using Java syntax) are not type safe! dynamic_cast<t>(v) uses RTTI to perform the cast of v to type T at run-time static_cast<t>(v) converts the value of the expression v to the type T at compile time.

References An alternative way to name an existing object Differences between pointers and references: a reference can never be null a reference cannot be changed (to point to a different object) dereferencing a reference is automatic int i = 9; int &j = i; // j is an alias, a reference for i j++; // i is now 10 i +=3; // i is now 13

Pass by Reference Parameters most common use of references in C++ allows you to avoid copying large objects, which would be very slow... class BigObject { int lotsofmemory[1000000]; // approximately 4MB void badfunc( BigObject o ) {... void frugalfunc( BigObject& o ) {... int main( int argc, char* argv[] ) { BigObject b = new BigObject(); // 4MB from the heap badfunc( b ); // 4MB more on the stack! frugalfunc( b ); // just the original 4MB on the heap

Pass by Reference, example // C++ void passtest( int& i ) { i++; i = 7; int main () { int j = 5; passtest( j ); cout << j << endl; return 0; // Java static void passtest (box i) { i.value++; i = new box(7); public static void main (String [ ] args) { box j = new box(5); passtest(j); System.out.println( J is + j.value); in C++, using pass by value, j would be 5 the same is true also for Java with primitive values in C++, using pass by reference, j is 7 in Java, using ordinary objects, j will be 6 in Java an object parameter passed to a method is really an object reference that is passed by value!