Composition I. composition is a way to combine or compose multiple classes together to create new class

Similar documents
The.Call Interface I. the R side of the.call interface is almost the same as the.c interface

Memory Management I. two kinds of memory: stack and heap

Advanced Systems Programming

Intermediate Programming, Spring 2017*

C:\Temp\Templates. Download This PDF From The Web Site

Object-Oriented Programming, Iouliia Skliarova

2 2

Multidimensional Arrays I

Review: C++ Basic Concepts. Dr. Yingwu Zhu

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

C++ 8. Constructors and Destructors

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

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

Constructors & Destructors

CS201- Introduction to Programming Current Quizzes

POLYMORPHISM Polymorphism: the type Polymorphism taking many shapes type of object

A brief introduction to C++

PIC 10A Objects/Classes

Object-Oriented Programming. Lecture 4 Dr Piotr Cybula

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

Common Misunderstandings from Exam 1 Material

Midterm Review. PIC 10B Spring 2018

The University of Nottingham

Lab 1: First Steps in C++ - Eclipse

Dynamic memory in class Ch 9, 11.4, 13.1 & Appendix F

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

Object Oriented Software Design II

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

COMP 2355 Introduction to Systems Programming

Object Oriented Software Design II

Linked List using a Sentinel

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

W3101: Programming Languages C++ Ramana Isukapalli

Logistics. Templates. Plan for today. Logistics. A quick intro to Templates. A quick intro to Templates. Project. Questions? Introduction to Templates

Constructors.

Array Elements as Function Parameters

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

Come and join us at WebLyceum

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

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

BEng (Hons) Telecommunications. Examinations for 2016 / Semester 2

G52CPP C++ Programming Lecture 13

Classes and Objects in C++

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

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

C++_ MARKS 40 MIN

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

Object-Oriented Programming, Iouliia Skliarova

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)

ComS 228 Exam 1. September 27, 2004

CSCE 110 PROGRAMMING FUNDAMENTALS

pointers & references

IS0020 Program Design and Software Tools Midterm, Fall, 2004

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001

Constants, References

Lecture 15a Persistent Memory & Shared Pointers

Midterm 2. 7] Explain in your own words the concept of a handle class and how it s implemented in C++: What s wrong with this answer?

University of Maryland Baltimore County. CMSC 202 Computer Science II. Fall Mid-Term Exam. Sections

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

University of Toronto

ECE Fall 20l2, Second Exam

AIMS Embedded Systems Programming MT 2017

内存管理. Memory management

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

Intermediate Programming & Design (C++) Classes in C++

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

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

Introduction to C++ Templates and Exceptions. C++ Function Templates C++ Class Templates Exception and Exception Handler

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

UEE1303(1070) S12: Object-Oriented Programming Advanced Topics of Class

Written by John Bell for CS 342, Spring 2018

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Polymorphism Part 1 1

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

6.096 Introduction to C++ January (IAP) 2009

Financial computing with C++

CS250 Final Review Questions

Comp151. Construction & Initialization

Initializing and Finalizing Objects

CS 376b Computer Vision

Dynamic Data Structures

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

What does it mean by information hiding? What are the advantages of it? {5 Marks}

Grades. Notes (by question) Score Num Students Approx Grade A 90s 7 A 80s 20 B 70s 9 C 60s 9 C

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

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

Unified Modeling Language a case study

Come and join us at WebLyceum

CS24 Week 3 Lecture 1

Constructor - example

OOP Fundamental Concepts

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

Lecture 10: Introduction to Inheritance

CS201 Some Important Definitions

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

CS 162, Lecture 25: Exam II Review. 30 May 2018

CS 61B, Spring 1996 Midterm #1 Professor M. Clancy

Transcription:

Composition I composition is a way to combine or compose multiple classes together to create new class composition has-a relationship a car has-a gear-box a graduate student has-a course list if XX has-a YY, ZZ,... then we could define a class with XX being the composite class YY, ZZ,... being the component classes August 24, 2005 c 2005 - Gopi Goswami (goswami@stat.harvard.edu) Page 1

Composition II suppose a generic MCMC Sampler class looks like: class Sampler private: MonteCarloSpecs mcs; Draw current_draw; Draw proposal_draw; public: }; Sampler (MonteCarloSpecs mcs, Draw draw); void do_sampling (void); void report_summary (void); so here a Sampler object has a MonteCarloSpecs object and two Draw obejcts August 24, 2005 c 2005 - Gopi Goswami (goswami@stat.harvard.edu) Page 2

Composition III construction of a composed class (Sampler) from the component classes (MonteCarloSpecs and Draw): Sampler::Sampler (MonteCarloSpecs &mcs, Draw &draw) : mcs(mcs), current_draw(draw), proposal_draw(draw) // empty body } that was easy, but wait, is current_draw(draw) a familiar construct in a initializer list? NO, its a little deep, its called the use of a copy-constructor August 24, 2005 c 2005 - Gopi Goswami (goswami@stat.harvard.edu) Page 3

Composition IV for any class the compiler gives you a copy-constructor e.g. compiler would generate code for Draw (Draw const &draw); this constructs a new object of class Draw, say, draw_new by copying the members of the argument object of class draw Draw const &draw if all the data-members of the class Draw are non-pointers, this automatic copying is fine, but what happens if you have pointer member(s), say, double *var_comp_vals then after member copying draw_new.var_comp_vals and draw.var_comp_vals will point to the same memory location, wouldn t they? so when the destructor Draw( ) is called once for draw and once for draw_new, guess what, you will get a nice error message saying trying to free an memory location twice!, why? August 24, 2005 c 2005 - Gopi Goswami (goswami@stat.harvard.edu) Page 4

Composition V so don t rely on the compiler, write your own copy-constructor if your class has at least one pointer data member: class Draw private: int const var_comp_dim; double *var_comp_vals; double log_density; int check_var_comp_dim (int var_comp_dim); void set_log_density (double val); public: }; Draw (int var_comp_dim, double const *var_comp_vals); Draw (Draw const &draw); Draw( ); int const get_var_comp_dim (void) const; double get_log_density (void) const; double const * const get_var_comp_vals (void) const; double compute_log_density (void); note the (usual) constructor and the copy constructor have the same name (but they take different arguments), this is called function overloading August 24, 2005 c 2005 - Gopi Goswami (goswami@stat.harvard.edu) Page 5

Composition VI compare the implementations of the two types of constructors: Draw::Draw (int var_comp_dim, double const *var_comp_vals) : var_comp_dim(check_var_comp_dim(var_comp_dim)) assert(var_comp_vals); cout << "Creating a Draw object" << endl; this->var_comp_vals = new double[var_comp_dim]; for (int ii = 0; ii < var_comp_dim; ++ii) this->var_comp_vals[ii] = var_comp_vals[ii]; } Draw::Draw (Draw const &draw) : var_comp_dim(draw.var_comp_dim), log_density(draw.log_density) cout << "Creating a Draw object" << endl; var_comp_vals = new double[var_comp_dim]; for (int ii = 0; ii < var_comp_dim; ++ii) var_comp_vals[ii] = draw.var_comp_vals[ii]; } no error checking necessary in the copy-constructor, why? August 24, 2005 c 2005 - Gopi Goswami (goswami@stat.harvard.edu) Page 6

Aside I one could have declared a constructor for Sampler like: Sampler (int n_iters, float time_in_secs, float prop_burn_in, string const name_of_algo, int &debug_level, int var_comp_dim, double const *var_comp_vals); note this takes all the arguments necessary for passing to the constructors of its component classes: MonteCarloSpecs and Draw its a cumbersome and error-prone approach, so instead use copy-constrcutors for the component classes for constructing a complicated composed class August 24, 2005 c 2005 - Gopi Goswami (goswami@stat.harvard.edu) Page 7

Composition Use I the use of composed classes is same as of a regular class (only possible difference is in their construction where the copy-constructors of the component classes are being used) int main (int argc, char **argv) int debug_level = 1; MonteCarloSpecs mcs(10, 10, 0.1, "test", debug_level); double vals[] = 1.0, 100.0 }; Draw draw(2, vals); Sampler sampler(mcs, draw); } sampler.do_sampling( ); sampler.report_summary( ); return 0; August 24, 2005 c 2005 - Gopi Goswami (goswami@stat.harvard.edu) Page 8

Code Files prog7.h prog7.c prog7makefile August 24, 2005 c 2005 - Gopi Goswami (goswami@stat.harvard.edu) Page 9