Praktikum: Entwicklung interaktiver eingebetteter Systeme

Similar documents
eingebetteter Systeme

Introducing C++ to Java Programmers

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

Object Oriented Design

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

Exercise 6.2 A generic container class

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

Lab 2: ADT Design & Implementation

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Chapter 10 Introduction to Classes

Function Overloading

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

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

Introduction to Programming session 24

CS 376b Computer Vision

ADTs in C++ In C++, member functions can be defined as part of a struct

COEN244: Class & function templates

Week 8: Operator overloading

Lecture 18 Tao Wang 1

Fast Introduction to Object Oriented Programming and C++

Memory management COSC346

Introduction to Classes

CSc 328, Spring 2004 Final Examination May 12, 2004

Functions and Recursion

Pointers. Developed By Ms. K.M.Sanghavi

EL2310 Scientific Programming

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

Lecture 5. Function Pointers

PIC 10A. Lecture 17: Classes III, overloading

CS93SI Handout 04 Spring 2006 Apr Review Answers

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

CE221 Programming in C++ Part 1 Introduction

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

Inheritance, and Polymorphism.

CS32 - Week 1. Umut Oztok. June 24, Umut Oztok CS32 - Week 1

Polymorphism Part 1 1

CSCI 123 Introduction to Programming Concepts in C++

Intermediate Programming, Spring 2017*

Classes: A Deeper Look

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Professor Terje Haukaas University of British Columbia, Vancouver C++ Programming

Instantiation of Template class

Next week s homework. Classes: Member functions. Member functions: Methods. Objects : Reminder. Objects : Reminder 3/6/2017

CSCE 110 PROGRAMMING FUNDAMENTALS

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

C++ Programming. Classes, Constructors, Operator overloading (Continued) M1 Math Michail Lampis

CS201 Latest Solved MCQs

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

Advanced Systems Programming

C and C++ 7. Exceptions Templates. Alan Mycroft

Chapter 1: Object-Oriented Programming Using C++

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

Introduction to C++ Introduction to C++ 1

CMSC 202 Final May 19, Name: UserID: (Circle your section) Section: 101 Tuesday 11: Thursday 11:30

Programming, numerics and optimization

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

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

Operator overloading

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

CS

Midterm Exam 5 April 20, 2015

Homework 4. Any questions?

6.S096 Lecture 4 Style and Structure

Programming C++ Lecture 2. Howest, Fall 2014 Instructor: Dr. Jennifer B. Sartor

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

CS304 Object Oriented Programming Final Term

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

CS250 Final Review Questions

A Tour of the C++ Programming Language

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

Lab 1: First Steps in C++ - Eclipse

Data Abstraction. Hwansoo Han

Assignment 1: grid. Due November 20, 11:59 PM Introduction

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

EECE.3220: Data Structures Spring 2017

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

INHERITANCE: CONSTRUCTORS,

C++ (classes) Hwansoo Han

Chapter 20 - C++ Virtual Functions and Polymorphism

A Tour of the C++ Programming Language

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Chapter 17: Linked Lists

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Chapter 2. Procedural Programming

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

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

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

Introduction Of Classes ( OOPS )

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

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

UNIVERSITY OF SWAZILAND

CSE 303: Concepts and Tools for Software Development

This is a CLOSED-BOOK-CLOSED-NOTES exam consisting of five (5) questions. Write your answer in the answer booklet provided. 1. OO concepts (5 points)

GEA 2017, Week 4. February 21, 2017

Transcription:

Praktikum: Entwicklung interaktiver eingebetteter Systeme C++-Labs (falk@cs.fau.de) 1

Agenda Writing a Vector Class Constructor, References, Overloading Templates, Virtual Functions Standard Template Library (ADVANCED) Smart Pointer (ADVANCED) 2

Writing a Vector Class Writing a simple vector class a vector is an one-dimensional array of objects start with a simple object integer values - type int to make future changes easier use a typedef - t_vector provide methods to - create a vector of given size - read/write to/from that vector (implemented later) - destroy a vector without memory leakage 1 10 5 42 3 4 7 a vector for 7 element of type int4 item index: 0 1 2 3 4 5 6 3

Vector Class Header #ifndef _INCLUDED_VECTOR_HPP #define _INCLUDED_VECTOR_HPP avoid multiple inclusion #include <iostream> typedef int t_vector; // class declaration class vector { public: vector(int size = 16); ~vector(); protected: t_vector *_buf; int _size; }; // Note the semicolon #endif // _INCLUDED_VECTOR_HPP data type to be stored in vector constructor and destructor member variables to store the vector elements and the size of the vector 4

Vector Class Implementation // use header from previous slide #include "vector.hpp" vector::vector(int size) { // constructor _size = size; _buf = new t_vector[_size]; } for(int idx = 0;idx < _size;++idx) { _buf[idx] = -1; } std::cout << "vector of size: " << _size << " created [ "; for(int idx = 0; idx < _size;++idx) { std::cout << _buf[idx] << " "; } std::cout << "]" << std::endl; vector::~vector() { // destructor delete[] _buf; std::cout << "vector of size: " << _size << " deleted << std::endl; } allocate storage for vector elements data type to be stored in vector initialize vector elements to known value free the storage allocated by the vector elements 5

Vector Class Compile and Run Open the Eclipse workspace workspace01 /workspace01 6

Vector Class Compile and Run Then open the 01_cpp project in there 01_cpp_01_lab_intro 01_cpp_02_lab_ctor_ovl 01_cpp_03_lab_templ_virt 01_cpp_04_lab_full_asoc_cache 01_cpp_05_lab_smart_pointer 7

Lab 01_cpp_01_lab_intro 1/1 Modify main.cpp to instantiate vectors of size 2,5 and 10 explicitly call the destructor of one vector Compile and Run the program using the eclipse Build menu Run menu 8

Agenda Writing a Vector Class Constructor, References, Overloading Templates, Virtual Functions Standard Template Library (ADVANCED) Smart Pointer (ADVANCED) 9

Lab 01_cpp_02_lab_ctor_ovl For the vector class a constructor with an optional parameter for the initial value is needed (default = 0) a function with two arguments that reads values from the vector is needed - Argument 1: a reference to the value to be read - Argument 2: the index of the value to be read - The function has to implement a range check for the index argument two operators have to be implemented - vector &operator =(const vector &rhs); - vector &operator +=(const vector &rhs); (implements pointwise addition; check if both vectors are of equal length) 10

Lab 01_cpp_02_lab_ctor_ovl 1/1 Constructor, References and Overloading in vector.h - extend the function prototype of the constructor to take two arguments (vector size and initial value) - give the function prototype for the new read() function that takes two arguments (value and index) in vector.cpp - implement the element initialization in the constructor - implement the new read() method - implement the operator=() - implement the operator+=() Compile and Run the program using the eclipse Build menu Run menu 11

Agenda Writing a Vector Class Constructor, References, Overloading Templates, Virtual Functions Standard Template Library (ADVANCED) Smart Pointer (ADVANCED) 12

Lab 01_cpp_03_lab_templ_virt Making the vector class a template class modify the vector class to be a template class that can store an arbitrary data type Create a class hierarchy for graphical objects pure virtual base class graph_obj - declares a method area() to return the area concrete implementations derived from graph_obj (e.g. a rectangle and a circle) have to implement that method Store graphical objects within the vector class Use the new template version of the vector class to store graphical objects (e.g. rectangles and circles) 13

Lab 01_cpp_03_lab_templ_virt 1/3 Class Templates, Virtual Methods and Classes in vector.h - modify the code to make vector a template class vector<t> - Hint: In our original code we used t_vector as a typedef for the data type to store in the vector! - Hint: Have a look at the constructor, as it has already been transferred to a template style! - Hint: Remember that the complete class implementation of a template class has to reside in the header file! Compile and Run the program using the eclipse DebugVector Build menu entry 1 DebugAll 2 DebugGraph 3 DebugVector DebugVector Run menu entry 1 01_cpp_03_lab_templ_virt DebugAll 2 01_cpp_03_lab_templ_virt DebugGraph 3 01_cpp_03_lab_templ_virt DebugVector Run As Run Configurations Organize Favourites 14

Lab 01_cpp_03_lab_templ_virt 2/3 Class Templates, Virtual Methods and Classes in graph_obj.h - implement a class circ (for circle) that inherits from the virtual base class graph_obj - the constructor should take the radius as an optional argument (default = 0.0) - implement the method area() - Hint: Don't forget to implement a destructor as well! Compile and Run the program using the eclipse DebugGraph Build menu entry 1 DebugAll 2 DebugGraph 3 DebugVector DebugGraph Run menu entry 1 01_cpp_03_lab_templ_virt DebugAll 2 01_cpp_03_lab_templ_virt DebugGraph 3 01_cpp_03_lab_templ_virt DebugVector Run As Run Configurations Organize Favourites 15

Lab 01_cpp_03_lab_templ_virt 3/3 Class Templates, Virtual Methods and Classes in main.cpp - instantiate a vector of rect with 2 elements, the elements should have width=1, height=2 - instantiate a vector of circ with 3 elements, the elements should have radius=2 Compile and Run the program using the eclipse DebugAll Build menu entry 1 DebugAll 2 DebugGraph 3 DebugVector DebugAll Run menu entry 1 01_cpp_03_lab_templ_virt DebugAll 2 01_cpp_03_lab_templ_virt DebugGraph 3 01_cpp_03_lab_templ_virt DebugVector Run As Run Configurations Organize Favourites 16

Agenda Writing a Vector Class Constructor, References, Overloading Templates, Virtual Functions Standard Template Library (ADVANCED) Smart Pointer (ADVANCED) 17

Lab 04_lab_full_asoc_cache Problem Associative hardware caches have fixed sizes and given replace strategies The C++ STL provides associative container classes, but these do not have a fixed size and no replace strategy Idea Implement a fully associative cache as a template class full_asoc_cache<>, that uses the map<> container class from the STL The data types for the key and for the entry are given as template parameters The size of the cache (the number of cache-lines) is given as constructor parameter To simplify the implementation, inserting a new entry into a full cache replaces a random cache line 18

Lab 04_lab_full_asoc_cache 1/1 Standard Template Library in full_asoc_cache.h implement following methods - bool get(const TAG_T&, ENTRY_T&); Hint: Use the method find() from the class map<> - void insert(const TAG_T&, const ENTRY_T&); Hint: Use the operator[] from the class map<> - void erase(const TAG_T&); - void clear(); Compile and Run the program using the eclipse Build menu Run menu Output: re:10.1 im:0 re:12.1 im:0.2 re:14.1 im:0.4 Re:15.1 im:0.5 19

Agenda Writing a Vector Class Constructor, References, Overloading Templates, Virtual Functions Standard Template Library (ADVANCED) Smart Pointer (ADVANCED) 20

Lab 01_cpp_05_smart_pointer Problem Unlike Java, C++ provides no built-in garbage collector that deletes unreferenced objects, thus eliminating memory leaks Smart pointers that manage reference counts for every allocated object are able to know when the last reference to an object is gone and thus delete the object Idea Implement a template class smart_ptr<> that represents a pointer to a given object type T The Copy Constructors and the Assignment Operators have to manage the reference counts The Destructor and the Assignment Operators may delete the referenced object A smart_ptr<> can be created from a pointer to an object of type T A common reference count value is allocated if the pointer is not 0 (what is the default value) 21

Lab 01_cpp_05_smart_pointer Smart Pointer in smart_ptr.h - implement a constructor to create a smart_ptr<t> from a pointer T* - implement the copy constructors and the assignment operator with reference counting - implement the destructor and avoid memory leaking - implement the missing operators to create a complete smart pointer Compile and Run the program using the eclipse Build menu Run menu Output: *ptr3 = black-colored car with speed 12.0416 *ptr4 = silver-colored jet with speed 100.125 *ptr5 = 42 22