structs (c,c++) - ariel.ac.il...structs 2 contiguously-allocated region of memory refer to members...

56
Structs (C,C++)

Upload: others

Post on 22-May-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Structs (C,C++)

Page 2: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Structs

2

Contiguously-allocated region of memory

Refer to members within structure by

names

Members may be of different types

Example:struct rec

{

int i;

int a[3];

int *p;

};

Memory Layout

i a p

0 4 16

Page 3: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

typedef

3

• Synonyms for variable types – make your

program more readable

typdef unsigned int size_t;

Page 4: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

typedef in C

4

• No need to write “struct Complex” each

time:

typedef struct ComplexType

{

double _real, _imag;

} Complex;

Complex addComplex(Complex, Complex);

Complex subComplex(Complex, Complex);

Complex.h

Page 5: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

C++

5

• No need to write “struct Complex” each time

even if we don’t use a typedef

struct Complex

{

double _real, _imag;

};

Complex addComplex(Complex, Complex);

Complex subComplex(Complex, Complex);

Complex.h

Page 6: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

6

Structs – poor oop: see List code

6

Page 7: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Classes (C++)

Page 8: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

8

Abstract Data Type (ADT)

Attributes:

• 4 bytes.

• Integer numbers.

set of operations

attributes

Operations:

• numerical operators

• logical operations

• bit operations

• I/O operations

int

Data Types define the

way you use storage

(memory) in the

programs you write.

Page 9: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

9

engine

door

window

wheel

attributes

operations

How should we describe a car?

Page 10: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

10

ClassesIn C++ we use classes for defining ADTs.

The syntax:

class ClassName

{

//attributes and operations

};

Objects are instances of classes. That is, objects are to classes what variables are to types.

A class definition does not allocate storage for anyobjects.

Page 11: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

11

Classes

You can write a C++ class such that its objects can be used like primitives both in term of usage (e.g., a+b) and in terms of efficiency

This is different from Java where objects are restricted pointers

Page 12: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Motivating Example

12

Goal:

1. Graphics package

2. Handle drawing of different shapes

3. Maintain list of shapes

Page 13: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Solution #1

13

struct shape {

enum{

RECTANGLE, CIRCLE, TRIANGLE } _type; double _x, _y; double _height, _width;

}; void Draw( shape const* Shape ) {

switch( Shape->type ) {

case RECTANGLE: ...case CIRCLE:

...

Page 14: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Solution #1 - Discussion

14

Pros:

Simple, direct

Cons:

- Lots of unrelated code together

- Need to save all possible data for all types (line needs two points, circle needs one point and a radius)

- Adding new shapes requires changing all procedures that deal with shape

- a lot of "if"/"switch" in runtime (partly true also for inheritance)

Page 15: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Solution #2 – C++ classes

15

Language provides tools for objects

Ideas similar to Java, but many

differences in details.

(We will not show this solution now, since we need yet to

learn these details…)

Page 16: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Simple Class Declaration

File: Counter.hpp

It is also common to use .h for C++ headers

16

#pragma once

class Counter

{

public:

Counter(); // Constructor

void increment(); // A method

int value(); // Another one

private:

int _count;

};

Page 17: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Using the class

File: app.cpp

17

#include "Counter.hpp"

#include <cstdio>

int main()

{

Counter cnt; // Call to constructor!

printf("Initial value= %d\n", cnt.value());

cnt.increment();

printf("New value = %d\n", cnt.value());

}

Page 18: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Class Implementation: Counter.cpp

18

#include "Counter.hpp"

void Counter::increment()

{

_count++;

}

int Counter::value()

{

return _count;

}

Scope operator

Page 19: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Class Implementation: Counter.cpp

19

Constructor - you implement it like a function,

no return type,

There might be “hidden” code inside it (more

later)

Counter::Counter()

{

_count = 0;

}

Page 20: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

How do we compile it?

20

g++ -Wall -Wvla -Werror -g -D_GLIBCXX_DEBUG -std=c++11 –c Counter.cpp –o Counter.o

g++ -Wall -Wvla -Werror -g -D_GLIBCXX_DEBUG -std=c++11 –c app.cpp –o app.o

g++ -Wvla -Werror -g -D_GLIBCXX_DEBUG -std=c++11 -Wall Counter.o app.o –o app

Page 21: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Declaration + implementation

21

#pragma once

class Counter

{

public:

Counter(); // Constructor

// A method with inline (we will learn about

this later) implementation :

void increment(){ _count++; }

private:

int _count;

};

Page 22: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Class Basics: Public/Private

22

Declare which parts of the class are accessible outside the class

class Foo

{

public:

// accessible from outside

private:

// private - not accessible from outside

// (compilation error)

// but visible to user!

};

Page 23: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Class Basics: Public/Private

23

Declare which parts of the class are accessible outside the class

class Foo

{

public:

// accessible from outside

private:

// private - not accessible from outside

// (compilation error)

// but visible to user!

};

Without using “dirty” tricks

Page 24: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Example

24

class MyClass

{

public:

int a();

double _x;

private:

int b();

double _y;

};

int main() {

MyClass foo; // legal:foo._x = 1.0; foo.a(); //compile error: foo._y = 2.0; foo.b();

}

Page 25: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Example

25

class MyClass

{

public:

int a();

double _x;

private:

int b();

double _y;

};

int MyClass::a()

{

// legal

_x = 1.0;

// also legal

_y = 2.0;

b();

}

Page 26: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Example – Point

class Point

{

public:

Point(int x, int y);

int getX();

int getY();

private:

int _x, _y;

};

26

Page 27: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

this

The address of the instance for which the member

method was invoked

27

Page 28: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

this

The address of the instance for which the member

method was invoked

bool Node::isChild(const Node* other)

{

for (const Node* curr=this; curr; curr=curr->next)

{

if (curr==other) return true;

}

return false;

}

class Node {

Node* next; public:

bool isChild(const Node*); // ...

};

28

Page 29: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

this

The address of the instance for which the member

method was invoked

bool Node::isChild(const Node* other)

{

for (const Node* curr=this; curr; curr=curr->next)

{

if (curr==other) return true;

}

return false;

}

class Node {

Node* next; public:

bool isChild(const Node*); // ...

};

29

Type of “this”: Node*

Page 30: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

this

The address of the instance for which the member

method was invoked

bool Node::isChild(const Node* other)

{

for (const Node* curr=this; curr; curr=curr->next)

{

if (curr==other) return true;

}

return false;

}

class Node {

Node* next; public:

bool isChild(const Node*); // ...

};

30

Type of “this”: Node*

But it is rvalue so you cannot write this=…

(same as ‘1’ type is int)

Page 31: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

this

The address of the instance for which the member

method was invoked

bool Node::isChild(const Node* other) const

{

for (const Node* curr=this; curr; curr=curr->next)

{

if (curr==other) return true;

}

return false;

}

class Node {

Node* next; public:

bool isChild(const Node*) const; // ...

};

31

Type of “this”: const Node*

But it is rvalue so you cannot write this=…

(same as ‘1’ type is int)

Page 32: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

structs and classes

32

Where did structs go?

• In C++ class==struct, except that by default

struct members are public and class members

are private (also inheritance diff later):

struct MyStruct{

int x;};

class MyClass{

int x;};

int main() {

MyStruct s; s.x = 1; // okMyClass c; c.x = 1; // error

}

Page 33: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

structs & classes

33

All of these are the same:

struct A{

int x;};

struct A{

public:int x;

};

class A{

public:int x;

};

Page 34: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

structs & classes

34

All of these are the same (and useless):

class A{

int x;};

class A{

private:int x;

};

struct A{

private:int x;

};

Page 35: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

struct / class keyword

35

1. No need to typdef the class declaration to avoid

using "struct MyStruct" or "class MyStruct".

2. But, it is legal. (May be useful if you want to use the

same name for a variable and a class which itself is a

bad idea).

int main() {

//All legal:

struct MyStruct s1; class MyClass c1; MyStruct s2; MyClass c2;

}

Page 36: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Class Basics - member/static

36

class List { public:

static int getMaxSize(); int getSize(); static int max_size=1000; //error! (only outside, below)int size=0; //error! (only in ctor, coming slides)

};

int List::max_size=1000; //ok, in one cpp file

int main() {

List l; l.getSize(); List::getMaxSize(); l.getMaxSize(); //compiles ok, but bad style

}

Page 37: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

this

37

static int List::getMaxSize() //no this!

{

return this->size; // compile error!

return max_size; // ok

}

int List::getSize()

{

return this->size; //ok

}

Page 38: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Class Basics: Constructors

38

Initialize the class object upon construction

class MyClass

{

public:

MyClass();

MyClass( int i );

MyClass( double x, double y );

...

};

MyClass a; // Calls 1

MyClass b(5); // Calls 2

MyClass c( 1.0, 0.0 ); // Calls 3

1

2

3

Page 39: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Constructors – parameterless ctor

39

class MyClass

{

public:

MyClass(); // parameterless ctor.

//...

};

//...

int main()

{

MyClass a; // parameterless ctor called

// ...

Page 40: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Constructors – default parameterless ctor

40

class MyClass

{

public:

// No ctors

//...

};

//...

int main()

{

MyClass a; // default parameterless

// ctor called

// ...

Page 41: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

41

class MyClass

{

public:

MyClass(int x); // no parameterless ctor.

};

int main()

{

MyClass a; // complier error –

// no parameterless ctor.

Constructors – no default parameterless ctor

Page 42: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Destructors

42

1. Ensure propose “cleanup” when the object

is destructed

2. Use for freeing memory, notifying related

objects, etc.

Page 43: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Class Basics: Destructors

43

#include <cstdlib>class MyClass{ public:

MyClass(); ~MyClass(); // destructor

private: char* _mem;

}; MyClass::MyClass() {

_mem=(char*)malloc(1000); } MyClass::~MyClass() {

free(_mem); }

int main()

{

MyClass a;

if( ... )

{

MyClass b;

}

}

Page 44: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

C Interface

44

struct IntList;

typedef struct IntList IntList;

IntList* intListNew();

void intListFree( IntList* List );

void intListPushFront( IntList* List, int x);

void intListPushBack( IntList* List, int x);

int intListPopFront( IntList* List );

int intListPopBack( IntList* List );

int intListIsEmpty( IntList const* List);

typedef void (*funcInt)( int x, void* Data );

void intListMAPCAR( IntList* List,

funcInt Func, void* Data );

Page 45: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

C++ Class

45

In header file:

class IntList

{

public:

IntList();

~IntList();

void pushFront(int x);

void pushBack(int x);

int popFront();

int popBack();

bool isEmpty() const;

private:

struct Node

{

int value;

Node *next;

Node *prev;

};

Node* m_start;

Node* m_end;

};

Page 46: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Classes & Memory allocation

46

Consider this C++ code

main()

{

IntList L;

}

What is the difference?

Compare to C style:

main()

{

IntList* L =

intListNew()

}

Page 47: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Classes & Memory allocation

47

IntList* L =

(IntList*)malloc(sizeof(IntList));

Does not call constructor!

Internal data members are not initialized

free(L);

Does not call destructor!

Internal data members are not freed

Page 48: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

new & delete

48

Special operators:

IntList *L = new IntList;

1. Allocate memory

2. Call constructor

3. Return pointer to the constructed object

delete L;

1. Call destructor

2. Free memory

Page 49: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

new

49

Can be used with any type:

int *i = new int;

char **p = new (char *);

• new is a global operator

• new expression invokes the new operator to

allocate memory, and then calls ctor

• Can be overloaded (or replaced)

• By default, failure throws exception. Can be

changed.

• See <new> header

Page 50: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Global operator new (simplified)

50

void *operator new(size_t size)

{

void *p;

if((p = malloc(size)) == 0)

{

throw std::bad_alloc;

}

return p;

}

Page 51: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

New & Constructors

51

class MyClass

{

public:

MyClass();

MyClass( int i );

MyClass( double x, double y );

};

MyClass* a;

a = new MyClass; // Calls

a = new MyClass(5); // Calls

a = new MyClass( 1.0, 0.0 ); // Calls

1

2

3

1

2

3

Page 52: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

New & arrays

52

To allocate arrays, use

int *a = new int[10]; // array of 10

//ints

size_t n = 4;

IntList *b = new IntList[n];

// array of n IntLists

Objects in allocated array must have an

argument-less constructor!

Page 53: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Delete & arrays

53

Special operation to delete arrays

int *a = new int[10];

int *b = new int[10];

delete [] a; // proper delete command

delete b; // may work, but may

// cause memory leak!

Page 54: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Allocate array of objects w/o def. cons.

54

size_t n = 4; MyClass **arr = new MyClass *[n]; // array of n pointers to MyClass (no

// cons. is invoked)

for (size_t i=0; i<n; ++i) {

arr[i] = new MyClass (i); // each pointer points to a MyClass

// object allocated on the heap, and

// the cons. is invoked. }

Page 55: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

Free an allocated array of pointers to objects

on the heap

55

size_t n = 4; for (size_t i=0; i<n; ++i) {

delete (arr[i]); // invoked the dest. of each MyClass

// object allocated on the heap, and

// free the memory. } delete [] arr; // free the memory allocated for the

// array of pointers. No dest. is invoked

Page 56: Structs (C,C++) - ariel.ac.il...Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec {

56

We will see different (and in many

cases better) alternatives to

directly using new!