Informatik I (D-ITET) Übungsstunde 12,

Similar documents
Informatik I (D-ITET) Übungsstunde 11, Hossein Shafagh

CSC 330 Object Oriented Programming. Operator Overloading Friend Functions & Forms

19. Classes. Encapsulation: public / private. Member Functions: Declaration. Member Functions: Call. class rational { int n; int d; // INV: d!

17. Classes. Encapsulation: public / private. Member Functions: Declaration. Member Functions: Call. class rational { int n; int d; // INV: d!

Informatik I (D-ITET) Übungsstunde 6, Hossein Shafagh

Informatik I (D-ITET) Übungsstunde 5, Hossein Shafagh

What is a Structure? related data items. Examples: n Used for handling a group of logically

Informatik I (D-ITET) Übungsstunde 9, Hossein Shafagh

Programming & Data Structure

16. Structs and Classes I

21. Dynamic Datatypes and Memory Management

CS11 Intro C++ Spring 2018 Lecture 3

Structures. 21 July 2009 Programming and Data Structure 1

21. Dynamic Datatypes and Memory Management

17. Classes. Overloading Functions. Operator Overloading. Function Overloading. operatorop

INFORMATIK 1 am D-ITET

PIC10B/1 Winter 2014 Exam I Study Guide

Operator Overloading in C++ Systems Programming

Chapter 18 - C++ Operator Overloading

The University of Nottingham

COMP 2355 Introduction to Systems Programming

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Classes. Christian Schumacher, Info1 D-MAVT 2013

Introduction to C++ Systems Programming

Abstraction and Encapsulation. Benefits of Abstraction & Encapsulation. Concrete Types. Using Typedefs to streamline classes.

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

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

Introduction to Programming

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Object-Oriented Programming

An Introduction to C++

8. Floating-point Numbers II

arrays review arrays and memory arrays: character array example cis15 advanced programming techniques, using c++ summer 2008 lecture # V.

Chapter7 Expression and Assignment Statement. Introduction

Introduction to the C programming language

Short Notes of CS201

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

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Chapter 12 - Templates

CS201 - Introduction to Programming Glossary By

Introduction to the C programming language

CPSC 427: Object-Oriented Programming

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

Example Final Questions Instructions

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)

User Defined Data: Product Constructor

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

CS 33. Introduction to C. Part 4. CS33 Intro to Computer Systems IV 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

CPSC 427: Object-Oriented Programming

pointers & references

Operator Overloading

04-19 Discussion Notes

Object-Oriented Programming

Object Oriented Programming COP3330 / CGS5409

Data Structures (INE2011)

CS24 Week 4 Lecture 2

Fast Introduction to Object Oriented Programming and C++

eingebetteter Systeme

EXP54-CPP. Do not access an object outside of its lifetime

Object-Oriented Programming for Scientific Computing

AIMS Embedded Systems Programming MT 2017

G52CPP C++ Programming Lecture 13

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

Chapter 15 - C++ As A "Better C"

Outline. 1 About the course

CSC 330 Object-Oriented Programming. Exception Handling CSC 330

CS242 COMPUTER PROGRAMMING

Evolution of Programming Languages

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

Common Misunderstandings from Exam 1 Material

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

Chapter-8 DATA TYPES. Introduction. Variable:

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

11. Reference Types. Swap! Reference Types: Definition. Reference Types

PIC 10A Objects/Classes

Introduction. W8.2 Operator Overloading

CS304 Object Oriented Programming Final Term

CSE030 Fall 2012 Final Exam Friday, December 14, PM

CS201 Some Important Definitions

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

Object oriented programming

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Chapter 18: Stacks And Queues

CSC1322 Object-Oriented Programming Concepts

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

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

17. Classes. Encapsulation, Classes, Member Functions, Constructors

W8.2 Operator Overloading

Recharge (int, int, int); //constructor declared void disply();

1.00 Lecture 5. Objects

COMP6771 Advanced C++ Programming

Programming Languages and Techniques (CIS120)

We look back... #include <iostream> #include <vector>

Programming in C and C++

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Derived and abstract data types. TDT4205 Lecture 15

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Transcription:

Informatik I (D-ITET) Übungsstunde 12, 11.12.2017 Hossein Shafagh shafagh@inf.ethz.ch Self-Assessment now!

Task 11.1 Finite rings a) struct int_7 { ; int value; //INV: 0<= value <=6 b) // POST : return value is the sum of a and b int_7 operator+ (int_7 a, int_7 b) { int help; help = a.value + b.value; int_7 r; r.value = help%7; return r;

Task 11.1 Finite rings c) // POST : return value is the difference of a and b int_7 operator- (int_7 a, int_7 b) { int_7 r; if (a.value >= b.value) { int help; r.value = a.value - b.value; return r; help = a.value - b.value; r.value = 7 + help; return r;

Task 11.2a: Complex numbers // represents complex numbers in cartesian form struct Complex { ; double real; double imag; // real part // imaginary part

Task 11.2a: Complex numbers // complex number output std::ostream& operator << (std::ostream& out, const Complex number) { return out << '[' << number.real << ',' << number.imag << ']'; // complex number input std::istream& operator >> (std::istream& in, Complex& number) { unsigned char f1, f2, f3; return in >> f1 >> c.r >> f2 >> c.i >> f3;

Task 11.2a: Complex numbers // complex number addition Complex operator + (const Complex a, const Complex b){ Complex result; result.real = a.real + b.real; result.imag = a.imag + b.imag; return result; // complex number subtraction Complex operator - (const Complex a, const Complex b){ Complex result; result.real = a.real - b.real; result.imag = a.imag - b.imag; return result; // complex number multiplication Complex operator * (const Complex a, const Complex b){ Complex result; result.real = a.real * b.real - a.imag * b.imag; result.imag = a.imag * b.real + a.real * b.imag; return result;

Task 11.2a: Complex numbers // complex number division Complex operator / (const Complex a, const Complex b) { Complex result; const float divisor = b.real * b.real + b.imag * b.imag; result.real = (a.real * b.real + a.imag * b.imag) / divisor; result.imag = (a.imag * b.real - a.real * b.imag) / divisor; return result; Complex operator - (const Complex c) { Complex result; result.real = - c.real; result.imag = - c.imag; return result; // complex number equality bool operator == (const Complex a, const Complex b){ return a.real == b.real && a.imag == b.imag; // complex number inequality bool operator!= (const Complex a, const Complex b){ return!(a == b);

1 A class for vectors class Vector { private: // already private, keyword only used to emphasize ; double x; double y;

1 A class for vectors (constructor) class Vector { private: // already private, keyword only used to emphasize double x; double y; ; public: // now switch to public the class initialization list Vector () : x(0), y(0) { Vector (const double _x, const double _y) : x(_x), y(_y) { Vector v1; Vector v2(3,4);

1 A class for vectors (getter & setter) class Vector { private: // already private, keyword only used to emphasize double x; double y; public: // now switch to public Vector () : x(0), y(0) { Vector (const double _x, const double _y) : x(_x), y(_y) { ; modify the object via this double get_x() const {return x; double get_y() const {return y; Vector a(3,4); double x = a.get_x(); double y = a.get_y();

1 A class for vectors (getter & setter) class Vector { private: // already private, keyword only used to emphasize double x; double y; public: // now switch to public Vector () : x(0), y(0) { Vector (const double _x, const double _y) : x(_x), y(_y) { double get_x() const {return x; double get_y() const {return y; ; void set_x(const double _x) {x = _x; void set_y(const double _y) {y = _y;

1 A class for vectors (Arithmetic operators) class Vector { ; // y += z, the first argument passed implicitly, in this case y Vector& operator+= (const Vector& b); Vector v1; Vector v2(3,4); v2 += v1; Vector& Vector::operator+= (const Vector& b) { x += b.get_x(); y += b.get_y(); return *this;

1 A class for vectors (Arithmetic operators) class Vector { ; // implicit conversion using a constructor with only x component // add to class: Vector (const double _x) : x(_x), y(0) { Vector v1; Vector v2(3,4); v2 += v1; Vector v4 = v1 + 3; // implicit conversion Vector operator+ (const Vector& a, const Vector& b) { Vector res = a; res += b; return res;

1 A class for vectors (output) #include <iostream> std::ostream& operator<< (std::ostream& out, const Vector v) { return out << v.get_x() <<, << v.get_y(); Vector v1; Vector v2(3,4); v2 += v1; Vector v4 = v1 + 3; // implicit conversion const Vector& v5 = v1; std::cout << v1 << "\n" << v5 << "\n";

2 Dynamically allocated memory int* dyn_int = new int (3); // constructed with value 3 int n =...; int* dyn_int_range = new int[n]; delete dyn_int; // deconstruct dynamic variable dyn_int = 0; delete[] dyn_int_range; // deconstruct dynamic range dyn_int_range = 0;

2 Dynamically allocated memory //... int i; std::cin >> i; int* mem = new int[i]; // length of input for (int j = 0; j < i; ++j) std::cin >> *(mem + j); for (int j = i-1; j >= 0; --j) std::cout << *(mem + j) << " "; delete[] mem; mem = 0; //...

3 Dynamic Data Types stack s1; s1.push(1); s1.push(3); s1.push(2); stack s2(s1);

3 Dynamic Data Types stack::stack(const stack& s) : top_node (0) { copy(s.top_node, top_node); void stack::copy(const linked_list_node* from, linked_list_node*& to) { assert (to == 0); if (from!= 0) { to = new linked_list_node(from->key); copy(from->next, to->next);

3 Dynamic Data Types stack s2; s2.push(4); s2.push(9); s2 = s1; // s1 as before

3 Dynamic Data Types stack s2; s2.push(4); s2.push(9); s2 = s1; // s1 as before // overwrite operator= // re-use copy function as before and afterwards free memory void stack::clear(linked_list_node* from) { if (from!= 0) { clear (from->next); delete from;

3 Dynamic Data Types stack s2; s2.push(4); s2.push(9); s2 = s1; // s1 as before s1 = s1; // self-assignment fails! stack& stack::operator= (const stack& s) { if (top_node!= s.top_node) { // test for self-assignment clear(top_node); top_node = 0; // fix dangling pointer copy(s.top_node, top_node); return *this;

Übungsblatt 12 Problem 12.1. Understanding structs and classes Problem 12.2. Averager Special restriction: Must use provided main function Problem 12.3. Queue (dynamic data types) Special restriction: Must use provided template (no simplifcaton allowed) Must implement a dynamic data type, reuse of existing dynamic container (e.g., vector) is not allowed. Similar to stack as dynamic structure enqueue, dequeue, is_empty output operator operator<< [1 4.. 10] copy constructor, assignment operator, destructor (rule of three)