• Nie Znaleziono Wyników

Programming and Data Structures Programming and Data Structures

N/A
N/A
Protected

Academic year: 2021

Share "Programming and Data Structures Programming and Data Structures"

Copied!
14
0
0

Pełen tekst

(1)

Programming and Data Structures Programming and Data Structures

Lecture 1 Lecture 1

Dr Piotr Cybula <piotr.cybula@wmii.uni.lodz.pl>

Dr Piotr Cybula <piotr.cybula@wmii.uni.lodz.pl>

(2)

Literature Literature

D. M. Reed, J. Zelle: Data Structures and Algorithms Using Python and C++.

Stephen Prata: C++ Primer Plus.

Bruce Eckel: Thinking in C++. Second edition. Vol. 1.

(http://mindview.net/Books/TICPP/ThinkingInCPP2e.html)

Stanley Lippman, Josée Lajoie, Barbara Moo: C++ Primer. Forth edition.

Bjarne Stroustrup: Language C++.

Michael Ben-Ari: Understanding programming languages.

http://cppreference.com http://www.cplusplus.com

(3)

Programming paradigms Programming paradigms

declarative programming (tell the computer what do you want)

imperative programming (tell the computer how to do it)

procedural programming (bottom-up design):

global data structures with free access (risky)

many loose functions building a program

object programming (top-down design):

protected & separated data structures (black-box solution)

functions operating on small parts of data (encapsulation)

generic programming (static polymorphism)

object-oriented programming (object programming + dynamic polymorphism)

modular programming, event programming, functional programming, ...

(4)

Data structures in programming Data structures in programming

Internal data structures (phisical representation):

basic, embedded (numbers, characters, addresses)

compound:

homogenic (arrays, strings)

heterogenic (structures/tuples/objects, lists, trees) Abstract data structures (logical representation):

sequences

stacks, queues

sorted lists, priority queses, heaps

sets, dictionaries (maps), trees, graphs

(5)

Structural type Structural type

Structural approach (without the encapsulation):

struct Obj //data structure type { int a;

float b; //fields

}; //semicolon required

void set(Obj &o, int _a, float _b) //global function { o.a = _a;

o.b = _b;

}

int getA(const Obj &o) //global function { return o.a;

}

Obj x; //structural variable (structure)

set(x, 1, 2.5); //function operating on structure cout << getA(x); //function operating on structure

(6)

Object type Object type

Object approach (the encapsulation):

struct Obj //abstract data type (the class) { int a;

float b; //class attributes

void set(int _a, float _b) //method (modifier, setter) { a = _a;

b = _b;

}

int getA() const //method (selector, getter) { return a;

}};

Obj x; //the object (an instance of the class) x.set(1, 2.5); //messege sent to the object cout << x.getA(); //messege sent to the object

(7)

Interface and implementation Interface and implementation

File obj.h (abstract type interface):

#ifndef MY_OBJ_H //single inclusion guard

#define MY_OBJ_H

struct Obj //abstract data type (the class) { int a, b;

void set(int _a, int _b); //method prototype };#endif

File obj.cpp (abstract type implementation):

#include ”obj.h”

void Obj::set(int _a, int _b) //method body (:: scope operator) { a = _a; b = _b;

}File main.cpp:

#include ”obj.h”

int main()

{ Obj x; //the object (an instance of the class) x.set(1, 2); //messege sent to the object

}

(8)

Object construction Object construction

the constructor - the special method for proper initialisation of the fields

called automatically on every object creation

the name of the constructor is the same as the name of the type (class)

many constructors with arbitrary number of arguments, no return value!

struct Obj

{ int a, b; //since C++11 one can initialise fields inside //e.g. int a = 0;

Obj(int _a = 0, int _b = 0) //constructor { a = _a;

b = _b;

}};

Obj x, y(1), z(1, 2), v = 3; //the implicit constructor calls Obj t[5], *s = new Obj, *p = new Obj[3]; //also here

(9)

Default constructor Default constructor

Default constructor:

may be called without arguments (any arguments with default values)

automatically created if (and only if) there are no explicit constructors, but without any initialisation of the fields (risky when there are any dynamically allocated fields in the class)

struct Obj

{ int a, b; //fields

Obj() //default constructor { a = 0;

b = 0;

} Obj() = default;//no-initialise default constructor (C++11) };Obj x, t[5]; //default constructor calls

Obj *s = new Obj, *p = new Obj[3]; //also here

(10)

Default constructor Default constructor

Default constructor:

if any constructors are defined, the default constructor is not created automatically

important – always define the default constructor (to simplify future inhertitance from the class)

struct Obj

{ int a, b; //fields

Obj(int _a, int _b = 0) //1- or 2-parameter constructor { a = 0;

b = 0;

}};

Obj y(1), z(1, 2); //proper creation

Obj x; //compile-time error without the default constructor

(11)

Copy constructor Copy constructor

Copy constructor:

takes as the only argument a reference to an existing object of the same type

automatically created if (and only if) there is no explicit copy constructor, but copying the fields by simple bit-copy (risky when there are any pointer fields)

important – always define the copy constructor for a class with pointer fields

struct Obj

{ int a, b; //fields

… //default constructor here

Obj(const Obj &o) //copy constructor { a = o.a;

b = o.b;

}};

Obj x;

Obj y(x), z = x; //the implicit copy constructor calls

(12)

Object destruction Object destruction

the destructor - the special method for proper cleanup

called automatically when the object goes out of scope and on explicit deletion

the name of the destructor is the same as the name of the type (class) but with leading tilde (~)

only one destructor without arguments, no return value!

struct Obj

{ int a, b; //fields

… //default constructor here

~Obj() //destructor with any cleaning operations { ... }

~Obj() = default; //do-nothing destructor (C++11) };{

Obj x, *p = new Obj; //constructor calls

delete p; //the explicit destructor call (object *p) } //the implicit destructor call (object x)

(13)

Object destruction Object destruction

The destructor:

automatically created if (and only if) there is no explicit destructor, but without any cleanup operations (risky when there are any dynamically allocated fields in the class)

important – always define the destructor for a class with the constructor dynamically allocating its fields

destructors for objects are called by the compiler in the reverse order of the object creation order

{ Obj x, *p = new Obj, z;

{ Obj y;

} //y destructed

delete p; //*p destructed } //z and then x destructed

(14)

Default operations Default operations

Compiler predefined operations (unless explicitly defined):

default constructor (paremeter-less, no initialisation)

copy-constructor (strict bit copy of all the object attributes)

destructor (does nothing)

assignment operator (strict bit copy of all the object attributes)

struct Obj { int a, b;

};

{ Obj x; //default constructor, random values for attributes Obj y = x; //copy-constructor

x = y; //assignment operator } //destructor for y and x

Cytaty

Powiązane dokumenty

(1) set the pred pointer on the node preceding the deleted node, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the

(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head) and pass the auxiliary pointers towards the end of the list

(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking

(1) invoke two auxiliary pointers set on the adjacent nodes pred and succ (assume that the pointers next in the preceding nodes pointed to by succ are already inverted, succ is

● input/output streams: iostream, fstream, sstream, iomanip. ● string

● sequence container – double-ended queue, dynamic array using non- contiguous storage, a list of arrays. ● fast addition/removal at the beginning and at the end of

● sort function sorts in the ascending order the contents of a container (between the starting and the ending iterator). ● container items must

Number rectangle with unequal sides is a counter example, because it has all right angles, so the first statement is true, but it is not a square, so the second statement is false..