COMP 2355 Introduction to Systems Programming

Similar documents
Evolution of Programming Languages

Where do we go from here?

CMSC 4023 Chapter 11

CS201- Introduction to Programming Current Quizzes

CPSC 427: Object-Oriented Programming

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

A brief introduction to C++

CA341 - Comparative Programming Languages

Short Notes of CS201

CPSC 427: Object-Oriented Programming

CS201 - Introduction to Programming Glossary By

G52CPP C++ Programming Lecture 13

COMP 2355 Introduction to Systems Programming

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

Lecture 2, September 4

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

Midterm Review. PIC 10B Spring 2018

Common Misunderstandings from Exam 1 Material

COMP6771 Advanced C++ Programming

CS3157: Advanced Programming. Outline

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

September 10,

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Object Oriented Software Design II

Chapter 11. Abstract Data Types and Encapsulation Concepts

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

2 2

Constants, References

Interview Questions of C++

Homework 4. Any questions?

Linked List using a Sentinel

Advanced Systems Programming

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

CSE 303: Concepts and Tools for Software Development

ECE Fall 20l2, Second Exam

CE221 Programming in C++ Part 1 Introduction

21. Dynamic Datatypes and Memory Management

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!

Homework 6. Reading. Problems. Handout 7 CS242: Autumn November

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Object-Oriented Programming, Iouliia Skliarova

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

Fast Introduction to Object Oriented Programming and C++

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

377 Student Guide to C++

Lecture 15a Persistent Memory & Shared Pointers

a data type is Types

CSC1322 Object-Oriented Programming Concepts

Object Oriented Programming. Solved MCQs - Part 2

Pointers, Dynamic Data, and Reference Types

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

Comp151. Construction & Initialization

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

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

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)

21. Dynamic Datatypes and Memory Management

Pointers. Developed By Ms. K.M.Sanghavi

04-19 Discussion Notes

Assumptions. History

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

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

STRUCTURING OF PROGRAM

G52CPP C++ Programming Lecture 16

Overview of C++: Part 1

G52CPP C++ Programming Lecture 20

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

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

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

Written by John Bell for CS 342, Spring 2018

Object-Oriented Programming

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

377 Student Guide to C++

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

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

CS 376b Computer Vision

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

C11: Garbage Collection and Constructors

An Introduction to C++

C++_ MARKS 40 MIN

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

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

2 ADT Programming User-defined abstract data types

Introduction to C++ Systems Programming

Object-Oriented Programming for Scientific Computing

05-01 Discussion Notes

Introduction Of Classes ( OOPS )

CMSC 341 Lecture 2 Dynamic Memory and Pointers

内存管理. Memory management

FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING

Call The Project Dynamic-Memory

Fundamentals of Programming. Lecture 19 Hamed Rasifard

primitive arrays v. vectors (1)

G52CPP C++ Programming Lecture 9

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

C++ and OO. l C++ classes and OO. l More Examples. l HW2. C++ for C Programmers by Ira Pohl

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

Transcription:

COMP 2355 Introduction to Systems Programming Christian Grothoff christian@grothoff.org http://grothoff.org/christian/ 1

Today Class syntax, Constructors, Destructors Static methods Inheritance, Abstract methods Access modifiers Namespaces Smart Pointers 2

Dynamic Memory Allocation in C++ new instead of malloc delete instead of free delete[] for arrays! 3

int * p = new int; *p = 42; int * q = p; delete q; Allocating Objects 4

int * p = new int[5]; p[4] = 42; int * q = &p[1]; delete[] p; Allocating Arrays 5

C++ structs struct stack { element * first; void init(); bool isempty(); void push(element * value); element * pop(); element * top(); void free(); ; 6

Implementing the Functions void stack::push(element * value) { value->next = first; first = value; element * stack::top() { return first; void stack::init() { first = NULL; 7

Using the struct struct stack s; s.init(); s.push(e); cout << *(s.first); // allowed! 8

structs vs. classes All members are public (anyone can freely access) No inheritance or subtyping Syntactically virtually identical to C++ classes 9

C++ classess class stack { public: stack(); // constructor! bool isempty(); void push(element * value); element * pop(); element * top(); ~stack(); // destructor! private: element * first; ; 10

Using the class stack s; cout << "Stack is " << (s.isempty()?"":"not ") << "empty" << endl; s.push(e); cout << *(s.first); // NOT allowed! 11

Implementing the Methods // constructor stack::stack() { first = NULL; bool stack::isempty() { return NULL == first; 12

Implementing the Destructor // destructor stack::~stack() { element * save; while (! isempty()) { save = first->next; delete first; first = save; 13

About Destructors A class can only have one destructor Destructors take no arguments and return void Destructors must not throw exceptions Destructors should never be called directly Destructors should always be virtual 14

Who calls Destructors? delete will call the destructor for the object delete[] will call the destructor for each element in the array (and, possibly, the array itself) For stack-allocated objects, the destructor is called when the scope ends 15

Constructors and Destructors void f() { stack s2; stack * h = new stack; delete h; throw 4; int main(int argc, char ** argv) { stack s; try { f() catch (...) { return 0; 16

About Constructors A class can have many constructors (overloading) Constructors can be called with new Constructors must return void Constructors may throw exceptions If no constructor is specified, a default constructor that does nothing is generated (not even zeroing as in Java!) 17

Constructors with Arguments stack::stack(int max) { first = NULL; this->max = max; stack * h = new stack(42); stack s(42); stack s2 = 42; // works too (coersion constructor) 18

The Copy Constructor Copy constructors are implicitly used for assignments: stack s(42); stack t = s; // here! Copy constructors for a class of type T must have the form T::T(const T&) You can also use them explicitly using constructs like: T s(42); T t(s); // here! 19

Constructors and Arrays stack * sv = new stack[10]; The above allocates an array for 10 stack objects Each object is initialized using the default constructor (no arguments!) sv must be freed using delete[] 20

Static Methods Use the modifier static No this available in the body No access to fields of the class They work just like in Java! Note: you can also have static fields. 21

Inheritance class stack : public vector { public: stack(int size) : vector(size) { ; element * pop() { return removeat(size()-1); element * top() { return get(size()-1); void push(element * e) { add(e, size()); 22

Virtual Functions In Java, all methods are by default virtual In C++, you must declare methods as virtual for the same effect; otherwise the static type of the receiver determines what function is called! 23

Example class P { public: void m() { cout << 1 << endl; class S : public P { public: void m() { cout << 2 << endl; P * p = new S; p->m(); // 1 S * s = new S; s->m(); // 2 24

Example class P { public: virtual void m() { cout << 1 << endl; class S : public P { public: virtual void m() { cout << 2 << endl; P * p = new S; p->m(); // 2 S * s = new S; s->m(); // 2 25

Abstract Functions Abstract functions in C++ work just like in Java, except for the syntax: class AbstractBaseClass { public: virtual void absfun(int args) = 0; The = 0 indicates that the function is abstract. 26

Multiple Inheritance In Java, a class can implement multiple interfaces In C++, a class can inherit from multiple classes! class Child : public Father, public Mother { //... 27

Multiple Inheritance The inheritance DAG must not have any cycles However, diamonds are allowed: class Human { string name; class Father : public Human { class Mother : public Human { class Child : public Father, public Mother { //... Here, Child would have the field name twice! 28

Virtual Inheritance The keyword virtual, when used when extending a class, will ensure that inherited fields are not replicated: class Human { string name; class Father : public virtual Human { class Mother : public virtual Human { class Child : public Father, public Mother { //... Here, Child would have the field name only once! 29

protected Members that are declared protected are only visible to subtypes This is different from Java where protected members are also visible in the same package! 30

Namespaces C++ namespaces are like Java packages Instead of writing package, you write: namespace myspace { class Foo {... ; Instead of writing import, you write using namespace Instead of writing java.util.list, you write std::list 31

Friends friend allows particular functions to access (all) private members: class complex { private: double re, im; public: complex(double, double); friend double geo::dist(complex, complex); 32

Smart Pointers Smart pointers are constructions that try to address common issues with normal pointers: Dangling pointers (use-after-free) Memory leaks (missing free) Failure to initialize (random pointers) Aliasing (multiple pointers to same object) 33

Exceptions and Memory Leaks int f() { throw 52; int g() { SomeClass * s = new SomeClass; f(); delete s; // not called! int main() { try { g(); catch (int i) { 34

A Bad Solution... int f() { throw 52; int g() { SomeClass * s = new SomeClass; try { f(); catch(...) { delete s; throw; delete s; int main() { try { g(); catch (int i) { 35

The auto ptr solution int f() { throw 52; int g() { auto_ptr<someclass> s = new SomeClass; f(); int main() { try { g(); catch (int i) { 36

How does it work? auto ptr<someclass> instance s acts in most respects just like SomeClass* Except, the destructor of auto ptr calls delete on the underlying instance delete automatically called whenever s leaves the scope, including during an exception! 37

Other properties of auto ptr When an alias is created, auto ptr sets the old pointer to null Only one reference to the object at any time Obvious which auto ptr will free No unexpected change of object through other alias 38

Questions? 39