Programsystemkonstruktion med C++: Övning 2. Karl Palmskog september 2010

Similar documents
C++ For Science and Engineering Lecture 27

September 19,

Introduction to C++ Part II. Søren Debois. Department of Theoretical Computer Science IT University of Copenhagen. September 12th, 2005

Short Notes of CS201

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

CS201 - Introduction to Programming Glossary By

Advanced C++ Topics. Alexander Warg, 2017

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

Object-Oriented Programming

Instantiation of Template class

Object Oriented Programming COP3330 / CGS5409

Programming Abstractions

Chapter 11. Abstract Data Types and Encapsulation Concepts

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

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

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

Inheritance, and Polymorphism.

Cpt S 122 Data Structures. Templates

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

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

OBJECT ORIENTED PROGRAMMING USING C++

COMP 2355 Introduction to Systems Programming

COEN244: Class & function templates

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Midterm Review. PIC 10B Spring 2018

September 10,

Ch. 11: References & the Copy-Constructor. - continued -

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

VIRTUAL FUNCTIONS Chapter 10

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism

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

CS

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

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

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Chapter 12 - Templates

C++ Templates. David Camp

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

Abstract Data Types and Encapsulation Concepts

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

CPSC 427: Object-Oriented Programming

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

CS304 Object Oriented Programming Final Term

StackVsHeap SPL/2010 SPL/20

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

Understand Execution of a Program

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

Advanced Systems Programming

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

C++ for System Developers with Design Pattern

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

C++ (classes) Hwansoo Han

Polymorphism. Programming in C++ A problem of reuse. Swapping arguments. Session 4 - Genericity, Containers. Code that works for many types.

Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts

CSE 303: Concepts and Tools for Software Development

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

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

CS250 Final Review Questions

CMSC 4023 Chapter 11

SDV4001 Object Oriented Programming with C++ and UI Midterm 2013

Overview of C++: Part 1

CSE 12 Week Eight, Lecture One

Inheritance. Transitivity

CSI33 Data Structures

AN OVERVIEW OF C++ 1

CSC 210, Exam Two Section February 1999

CSci 1113 Final. Name: Student ID:

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

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

CS250 Final Review Questions

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

SFU CMPT Topic: Class Templates

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Programming in C and C++

Topics. bool and string types input/output library functions comments memory allocation templates classes

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Object Oriented Programming. Solved MCQs - Part 2

C++ TEMPLATES. Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

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

CS 2150 Exam 1, Spring 2018 Page 1 of 6 UVa userid:

Introduction to Classes

CS 376b Computer Vision

! Operators such as =, +, <, can be defined to. ! The function names are operator followed by the. ! Otherwise they are like normal member functions:

Object Oriented Paradigm

Example : class Student { int rollno; float marks; public: student( ) //Constructor { rollno=0; marks=0.0; } //other public members };

CPSC 427: Object-Oriented Programming

Fast Introduction to Object Oriented Programming and C++

Written by John Bell for CS 342, Spring 2018

AIMS Embedded Systems Programming MT 2017

THE BIG FOUR FRIEND FUNCTIONS

Midterm Exam 5 April 20, 2015

Exercise 6.2 A generic container class

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

Transcription:

Programsystemkonstruktion med C++: Övning 2 Karl Palmskog palmskog@kth.se september 2010

Specalfunktioner in classes Constructor constructors of a class initializes class members initial values can be given in the colon list or the code block default constructor is not used if the constructor is declared default constructor runs members constructor Copy Constructor takes an instance of the class as an argument Copy Constructor copies the members c l a s s V e h i c l e { p r i v a t e : i n t w e i g h t ; p u b l i c : V e h i c l e ( i n t w) : w e i g h t (w) { ;

Specalfunktioner in classes Destructor destructor takes no arguments and does not return anything default destructor is used if not defined runs members destructor Person : : Person ( ) { // f r e e up memory f o r a r r a y o f heap a l l o c a t e d s k i l l s d e l e t e [ ] s k i l l s ;

Specalfunktioner in classes Assignment operators and the Rule of Three the class instance variable is called assignment operator function operator= the default operator= copies the members heuristics: 1 if you need to define the destructor you need to define Copy Constructor operator= 2 if you need to define the Copy Constructor you need to define operator= 3 if you need to define the operator= you need to define the copy constructor destructor, copy and operator= are associated

Specalfunktioner in classes An inheritance hierarchy Animal string name Donkey Horse Mule

Specalfunktioner in classes Special functions and inheritance Objekt Konstruktor Destruktor Animal Animal Animal Donkey Animal Donkey Donkey Animal Horse Animal Horse Horse Animal Mule Animal Mule Donkey Horse Animal Animal Horse Donkey Mule Animal

Virtual Functions Virtual Functions non-virtual functions are selected statically at compile time, depending on the type of pointer or reference virtual functions are determined dynamically during execution based on the object s actual type #i n c l u d e <i o s t r e a m > using namespace s t d ; c l a s s A { p u b l i c : void f o o ( ) { cout << A : : f o o ( ) << e n d l ; v i r t u a l void bar ( ) { cout << A : : bar ( ) << e n d l ; ; c l a s s B : p u b l i c A { p u b l i c : void f o o ( ) { cout << B : : f o o ( ) << e n d l ; void bar ( ) { cout << B : : bar ( ) << e n d l ; ;

Virtual Functions Virtual functions in practice #i n c l u d e ab. h i n t main ( ) { B b ; B bptr = &b ; A aptr = bptr ; bptr >f o o ( ) ; bptr >bar ( ) ; aptr >f o o ( ) ; aptr >bar ( ) ; r e t u r n 0 ; $. / a. out B : : f o o ( ) B : : bar ( ) A : : f o o ( ) B : : bar ( )

Virtual Functions Implementation of virtual functions Mule v-table {... V-table *func1 *func2

Virtual Functions Virtual destructor What is the problem with this code? c l a s s A { p u b l i c : A( ) { s t d : : cout << A( ) << s t d : : e n d l ; ; c l a s s B : p u b l i c A { p u b l i c : B( ) { s t d : : cout << B( ) << s t d : : e n d l ; ; i n t main ( ) { A a = new A ( ) ; A b = new B ( ) ; d e l e t e a ; d e l e t e b ;

Virtual Functions Pure virtual function if a function is pure virtual, the classes are implemented by inheritance correspond to abstract methods in Java c l a s s A { p u b l i c : v i r t u a l i n t f o o ( ) = 0 ;... ; c l a s s B : p u b l i c A { p u b l i c : v i r t u a l i n t f o o ( ) {...... ;

templates (models) used to write type independednt code inheritance is an alternative to shared behaviors functions and classes parameterized on a given type compiler replaces occurrences of type parameters and create parameter free function/class

c l a s s Stack { p r i v a t e : s t a t i c const i n t STK SIZE = 1 0 0 ; i n t m stk [ STK SIZE ] ; i n t m top ; p u b l i c : Stack ( ) : m top ( 0 ) { bool isempty ( ) { r e t u r n ( m top == 0 ) ; bool i s F u l l ( ) { r e t u r n ( m top >= STK SIZE ) ; bool push ( i n t v a l ) { i f ( i s F u l l ( ) ) r e t u r n f a l s e ; m stk [ m top++] = v a l ; r e t u r n true ; bool pop ( i n t &v a l ) { i f ( isempty ( ) ) r e t u r n f a l s e ; v a l = m stk[ m top ] ; r e t u r n true ; ;

template <typename T> c l a s s Stack { p r i v a t e : s t a t i c const i n t STK SIZE = 1 0 0 ; T m stk [ STK SIZE ] ; i n t m top ; p u b l i c : Stack ( ) : m top ( 0 ) { bool isempty ( ) { r e t u r n ( m top == 0 ) ; bool i s F u l l ( ) { r e t u r n ( m top >= STK SIZE ) ; bool push ( const T &v a l ) { i f ( i s F u l l ( ) ) r e t u r n f a l s e ; m stk [ m top++] = v a l ; r e t u r n true ; bool pop (T &v a l ) { i f ( isempty ( ) ) r e t u r n f a l s e ; v a l = m stk[ m top ] ; r e t u r n true ; ;

Use of template class #i n c l u d e <i o s t r e a m > #i n c l u d e s t a c k. h i n t main ( ) { Stack<i n t > i s t k ; // s t a c k o f i n t Stack<double> f s t k ; // s t a c k o f d o u b l e s i s t k. push ( 4 7 1 1 ) ; i s t k. push ( 4 7. 1 1 ) ; f s t k. push ( 4 7. 1 1 ) ; f s t k. push ( 4 7 1 1 ) ; i n t i t o p ; i s t k. pop ( i t o p ) ; s t d : : cout << i t o p << s t d : : e n d l ; r e t u r n 0 ;

A template maxfunction Exam Assignment: write a template function max that returns the larger of two arguments. const & T max ( const T & x, const T & y ) { i f ( x < y ) { r e t u r n y ; e l s e { r e t u r n x ; Följdfrågor: what type parameter functionof the arguments is required? why is the return type of reference? why can not you give arguments of different types?

Meta Programming with templates #i n c l u d e <i o s t r e a m > template<i n t N> s t r u c t f a c t { enum { v a l u e = N f a c t <N 1>:: v a l u e ; ; template<> s t r u c t f a c t <0> { enum { v a l u e = 1 ; ; i n t main ( ) { s t d : : cout << f a c t <5>:: v a l u e << s t d : : e n d l ; r e t u r n 0 ;

Compilation of templatiserade classes and functions template classes/functions has to be defined before use usually occurs because both the declaration and definition of.h-file compiler need not be given the name of the class file support for separate production of the definition/declaration varies between compilers

Templatisering Vs. common super class common super class + code reuse + code reuse + static type control + flexibility + effective implementation - type control at run time - definition before use - overhead - slow compilation - large binary file