Docsity

Introducing the Class-Jave Programming-Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Introducing the Class-Jave Programming-Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Introducingthe

Class

Class

Fundamentals

-^ object.

A^ class

includes

both^ data

and^ the

code^ that

will

operate

on^ that

data.

-^ Thus,

a^ class^

links^ data

with^ code.

C++^ uses

a^ class

specification

to^ construct

objects.

-^ Objects

are^ instances

of^ a^ class.

-^ Therefore,

a^ class

is^ essentially

a^ set^ of

plans^ that

specify^

how^ to^

build^ an

object.

-^ It^ is^ important

to^ be^ clear

on^ one

issue:^ A

class^ is

a

logical^ abstraction. • It^ is^ not

until^ an

object^

of^ that^

class^ has

been^ created

that^ a^ physical

representation

of^ that

class^ exists

in

memory.

-^ Within

a^ class,

data^ is

contained

in^ variables

and

code^ is

contained

in^ functions.

-^ Collectively,

the^ functions

and^ variables

that

constitute

a^ class

are^ called

members

of^ the

class.

-^ Thus,

a^ variable

declared

within

a^ class

is^ called

a

member

variable,

and^ a^

function

declared

within

a^ class

is^ called

a^ member

function.

-^ Sometimes

the^ term

instance

variable

is^ used

in

place^ of

member

variable.

-^ A^ class

is^ created

by^ using

the^ keyword

class.^ A

class

declaration

is^ syntactically

similar

to^ a^ structure.

//^ This^ creates

the^ class

queue.

class^ queue

{ int q[100];int sloc,^

rloc; public:void^ init();void^ qput(int i);int qget();};

The^ member

variables

of

queue

are^ q,^

sloc,^ and rloc. The^ member

functions are^ init(

),^ qput(

),^ and qget(^ ).

-^ This

is^ one

way^ encapsulation

is^ achieved—

you^ can

tightly

control

access

to^ certain

items

of^ data

by^ keeping

them^

private.

-^ Although

there

are^ none

in^ this

example,

you

can^ also

define

private

functions,

which

can

be^ called

only^ by

other

members

of^ the

class.

-^ To^ make

parts^

of^ a^ class

public

(i.e.,^ accessible

to^ other

parts^

of^ your

program),

you^ must

declare

them^

after^ the

public

keyword.

-^ All^ variables

or^ functions

defined

after^

the

public

specifier are

accessible

by^ all

other

functions

in^ your

program.

-^ Typically,

your^ program

will^ access

the

private

members

of^ a^ class

through

its^ public

functions.

-^ Once

you^ have

defined

a^ class,

you^ can

create

an^ object

of^ that

type^ by

using^

the^ class

name.

-^ A^ class

name

becomes

a^ new

type^ specifier.

queue

Q1,^ Q2;

-^ When

an^ object

of^ a^ class

is^ created,

it^ will

have^ its

own^ copy

of^ the

member

variables

that^ comprise

the^ class.

-^ This

means

that^ Q

and^ Q

will^ each

have

their^ own,

separate

copies

of^ q,^ sloc,

and^ rloc.

-^ Thus,

the^ data

associated

with^ Q

is^ distinct

and^ separate

from^

the^ data

associated

with

Q2.

-^ To^ implement

a^ function

that^ is^ a^

member^

of^ a^ class,

you^ must

tell

the^ compiler

to^ which

class^ the

function

belongs^

by^ qualifying

the

function^

name^ with

the^ class

name.

-^ For^ example,

here^ is^ one

way^ to^ code

the^ qput(

)^ function:

void^ queue::qput(int i){ if(sloc==100)

{ cout <<^ "Queue

is^ full.\n"; return;} sloc++;q[sloc]^ =^

i; }

-^ The^

::^ is^ called

the^ scope

resolution

operator.

-^ Essentially,

it^ tells

the^ compiler

that^ this

version

of^ qput(

)^ belongs

to^ the

queue

class.

-^ Or,^ put

differently,

::^ states

that^ this

qput(^

)^ is^ in

queue’s

scope.

-^ Several

different

classes

can^ use

the^ same

function

names.

-^ The^

compiler

knows

which

function

belongs

to

which^

class^ because

of^ the

scope^

resolution

operator

and^ the

class^ name.