Docsity

C++ Encapsulation, Heap, Exercises of C programming

Partial preview of the text

Download C++ Encapsulation, Heap and more Exercises C programming in PDF only on Docsity!

CSE333, Spring 2018

L12: C++ Heap

C++ Encapsulation, Heap^ CSE 333 Spring 2018 Instructor:

Justin Hsia

Teaching Assistants: Danny Allen

Dennis Shao

Eddie Huang

Kevin Bi

Jack Xu

Matthew Neldam

Michael Poulain

Renshu Gu

Robby Marver

Waylon Huang

Wei Lin

CSE333, Spring 2018

L12: C++ Heap

Administrivia^ ^ Exercise 10 released today, due Monday^ ^

Write a substantive class in C++!  Refer to

Complex.h

/Complex.cc

^ Homework 2 due next Thursday (4/26)^ ^

File system crawler, indexer, and search engine

CSE333, Spring 2018

L12: C++ Heap

Access Control^ ^ Access modifiers for members:^ ^

public

: accessible to

all^ parts of the program

^ private

: accessible to the member functions of the class

-^ Private to

class

, not object instances

^ protected

: accessible to the member functions of the class and any

derived

classes

^ Reminders:^ ^

Access modifiers apply to

all^ members that follow until

another access modifier is reached  If no access modifier specified,

struct

members default to

public

and^

class

members default to

private

CSE333, Spring 2018

L12: C++ Heap

Nonmember Functions^ ^ “Nonmember functions” are just normal functions thathappen to use our class^ ^

Called like a regular function instead of as a member of a classobject instance^ •^ This gets a little weird when we talk about operators…  These do

not^

have access to the class’ private members

^ Useful nonmember functions often included as part ofinterface^ ^

Declaration goes in header file, but

outside

of class definition

CSE333, Spring 2018

L12: C++ Heap

Namespaces^ ^ Each namespace is a separate scope^ ^

Useful for avoiding symbol collisions! ^ Namespace definition:^ ^

namespace

name

{

//^ declarations

go^

here

}  Creates a new namespace name if it did not exist, otherwise adds to the existing namespace

(! )

-^ This means that namespaces can discontiguous  Definitions can appear outside of the namespace definition namespace

name

{

//^ declarations

go^

here

}

CSE333, Spring 2018

L12: C++ Heap

Classes vs. Namespaces^ ^ They look very similar, but classes are

not

namespaces:

^ There are no instances/objects of a namespace; a namespaceis just a group of logically-related members ^ To access a member of a namespace, you must use the fullyqualified name (

i.e.^ nsp_name::member

)

-^ Unless you are

using

that namespace

-^ You only used the fully qualified name of a class member whenyou are defining it outside of the scope of the class definition

CSE333, Spring 2018

L12: C++ Heap

Lecture Outline^ ^ Class Encapsulation^ ^ Using the Heap^ ^

new^

/^ delete

/^ delete[]

CSE333, Spring 2018

L12: C++ Heap

C++

nullptr ^ C and C++ have long used

NULL

as a pointer value

that references nothing  C++11 introduced a new literal for this:

nullptr

^ New reserved word ^ Interchangeable with

NULL

for all practical purposes, but it

has type

T*^ for any/every

T, and is not an integer value

-^ Avoids funny edge cases (see C++ references for details) •^ Still can convert to/from integer

0 for tests, assignment, etc.

^ Advice: prefer

nullptr

in C++11 code

-^ Though NULL will also be around for a long, long time

CSE333, Spring 2018

L12: C++ Heap

new

/delete

Example

#include

"Point.h" using

namespace

std;

...^

//^ definitions

of^ AllocateInt()

and^

AllocatePoint()

int^ main

()^ {

Point*

x^ =^

AllocatePoint

(1,^ 2);

int*^

y^ =^ AllocateInt

cout^

<<^ "x's

x_^ coord:

"^ <<

x-> get_x

()^ <<

endl;

cout^

<<^ "y:

"^ <<

y^ <<

",^ *y:

"^ <<

*y^ <<

endl;

delete

x; delete

y; return

int*^

AllocateInt

(int^

x)^ {

int*^

heapy_int

=^ new

int;

*heapy_int

=^ x; return

heapy_int; }

Point*

AllocatePoint

(int^

x,^ int

y)^ {

Point*

heapy_pt

=^ new

Point

(x,y);

return

heapy_pt; }

heappoint.cc

CSE333, Spring 2018

L12: C++ Heap

Dynamically Allocated Arrays^ ^ To dynamically allocate an array:^ ^

Default initialize: ^ To dynamically deallocate an array:^ ^

Use delete[] name;  It is an

incorrect

to use “

delete

name;

” on an array

-^ The compiler probably won’t catch this, though (

! ) because it

can’t tell if it was allocated with

new type[size];

or^ new type; • Results in undefined behavior

type*

name

=^ new

type[size];

delete[]

name;

CSE333, Spring 2018

L12: C++ Heap

Arrays Example (class objects)

#include

"Point.h" using

namespace

std;

int^ main

()^ {

... Point

stack_point(1,

Point*

heap_point

=^ new

Point(1,

Point*

error_point_arr

=^ new

Point[10];

Point*

error2_point_arr

=^ new

Point[10](1,

... delete

heap_point; ...return

arrays.cc

CSE333, Spring 2018

L12: C++ Heap

malloc

vs.

new

malloc()

new

What is it?

a function

an operator or keyword

How often used (in C)?

often

never

How often used (in C++)?

rarely

often

Allocated memory for

anything

arrays, structs, objects,

primitives

Returns

a^ void* ( should be cast

appropriate pointer type( doesn’t need a cast

When out of memory

returns

NULL

throws an exception

Deallocating

free()

delete

or^ delete[]

CSE333, Spring 2018

L12: C++ Heap

Heap Member Example^ ^ Let’s build a class to simulate some of the functionalityof the C++ string^ ^

Internal representation: c-string to hold characters ^ What might we want to implement in the class?

CSE333, Spring 2018

L12: C++ Heap

Str Class Walkthrough #include

using

namespace

std;

class

Str^

public:Str();

//^ default

ctor

Str(const

char*

s);^

//^ c-string

ctor

Str(const

Str&

s);^

//^ copy

ctor

~Str();

//^ dtor

int^ length

()^ const;

//^

return

length

of^ string

char*

c_str

()^ const;

//^ return

a^ copy

of^ st_

void^

append

(const

Str&

s);

Str&^

operator=(const

Str&

s);^

//^ string

assignment

friend

std::ostream&

operator<<(std::ostream&

out,

const

Str&

s);

private:char*

st_;

//^

c-string

on^ heap

(terminated

by^ '\0')

};^ //

class

Str

Str.h