38
Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes C++ Classes Enhanced Structures C struct only permitted to have data members Functions to operate on such data appeared elsewhere Association with data depended upon programmer C++ struct may have function members as well Common package combines data and functions Combination of data and functions treated as single object

Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C struct only permitted to have data members Functions to

Embed Size (px)

Citation preview

Page 1: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 1 -

C++ ClassesC++ Classes

Enhanced Structures

C struct only permitted to have data members Functions to operate on such data appeared elsewhere Association with data depended upon programmer

C++ struct may have function members as well Common package combines data and functions Combination of data and functions treated as single

object

Page 2: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 2 -

C++ ClassesC++ Classes

Classes

Word class arose in Simula 67 used to describe new data type

Means in an object oriented language for

Information hiding

Abstraction

Encapsulation

Page 3: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 3 -

C++ ClassesC++ Classes

Classes

A C++ class

Collects entities with common characteristics into objects

Is a data type

Collection of data elements

Data Members

Set of operations

Function Members

Page 4: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 4 -

C++ ClassesC++ Classes

Classesclass Name

{

body;

};

class Vehicle

{

};

Vehicle myVehicle;

Vehicle * rentalVehicle = &my Vehicle;

Page 5: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 5 -

C++ ClassesC++ Classes

ClassesClass Name

Identifies the class

Upper case by convention

Helps to distinguish from variable or constant

Instance NameWritten in lower case

Class BodyEnclosed in curly braces

Contains….

Data Members

Function Members

Terminated with a ;

Page 6: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 6 -

C++ ClassesC++ Classes

Class Access

public

Access

Visible to general public

Any function or class within the system

Has access

All data and function members that follow

Available to everyone to use or modify

Page 7: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 7 -

C++ ClassesC++ Classes

Class Access

protected

Access

Visible to

Class members

Classes named as friends

Members of derived classes

All data and function members that follow…...

Appear private to outsiders

Appear public to members and members of derived classes

Page 8: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 8 -

C++ ClassesC++ Classes

Class Access

private

Access

More restrictive

Visible to

Class members

Classes named as friends

All data and function members that follow

Hidden from everyone

No one can change from outside the struct or class

Page 9: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 9 -

C++ ClassesC++ Classes

Class Access

Access Restriction Increasing

Class

privateprotectedpublic Message

Message

Object

Page 10: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 10 -

C++ ClassesC++ Classes

Classes

Public Interface

Establish well defined persistent public interface

Using access specifiers

Hide implementation of the object

Implementation

Private and protected members can change

Functionality remains constant

Page 11: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 11 -

C++ ClassesC++ Classes

ClassesDeclaration

class Name

{

public:

..…

protected:

..…

private:

..…

};

private by default

Page 12: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 12 -

C++ ClassesC++ Classes

Classes

Class

BeersInstance

myBeer

Data membersEach new instance of a class gets distinct copies of all data members

Function MembersShared among all instances

Page 13: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 13 -

C++ ClassesC++ Classes

ClassesDeclaration

class Name

{

public:function membersdata members

protected:function membersdata members

private:function membersdata members

};

Page 14: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 14 -

C++ ClassesC++ Classes

Class Initialization - Constructors and Destructors

Constructor Member function with the same name as the class Responsible for initializing class data members Can be overloaded to have zero or more parameters

syntax

className (arg0, arg1, ... argn) { body }

args are optional

Page 15: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 15 -

C++ ClassesC++ Classes

Class Initialization - Constructors

The constructor is invoked….

Whenever class object declared

At that time full type checking of the definition is applied

Assumes level of accessibility of section in which declaredPublicProtectedPrivate

Invocation of new invokes the constructor for class instance

If new fails the constructor not invoked

What about malloc?

Page 16: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 16 -

C++ ClassesC++ Classes

Class Constructors

class SimpleClass{public:

SimpleClass () : myValue(10); SimpleClass (int aValue) : myValue(aValue);

private:int myValue;

};

int main (void}{

SimpleClass myClass(15), yourClass;return 0;

}

class SimpleClass{public:

SimpleClass () : myValue(10); SimpleClass (int aValue) : myValue(aValue);

private:int myValue;

};

int main (void}{

SimpleClass myClass(15), yourClass;return 0;

}

Page 17: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 17 -

C++ ClassesC++ Classes

Class Initialization - Constructors and DestructorsDestructor

Is member function

Has the same name as the class

Is preceded by the ~ symbol

Provides means to de-initialize data members

Has no parameters

Cannot specify

Return type or value

Cannot be overloaded

Page 18: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 18 -

C++ ClassesC++ Classes

Class Initialization - Constructors and Destructors

Destructor

Storage to be de-initialized must have been allocated by new

Can be called explicitly to clean up without deallocating

syntax

~className ( ) { body }

no args

Page 19: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 19 -

C++ ClassesC++ Classes

Class Initialization - Constructors and DestructorsDestructor

Destructor invoked

Whenever delete operator applied to an object.

Called by delete before freeing storage.

Class object exits scope.

Just before program terminates.

What about free?

Page 20: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 20 -

C++ ClassesC++ Classes

Private Constructors...

class Date{friend class Holiday;public:….private:

Date (int aDay=15, int aMonth=8, int aYear=1971);int day, month, year;

};

class Holiday{public:

Date *createHoliday(int day, int month) {

myHoliday = new Date (day, month); return myHoliday;

}

private:Date *myHoliday;

};

class Date{friend class Holiday;public:….private:

Date (int aDay=15, int aMonth=8, int aYear=1971);int day, month, year;

};

class Holiday{public:

Date *createHoliday(int day, int month) {

myHoliday = new Date (day, month); return myHoliday;

}

private:Date *myHoliday;

};

Page 21: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 21 -

C++ ClassesC++ Classes

Class Initialization - Constructors and Destructor

Important to remember…...

Constructor

Initializes storage

Does not allocate

Destructor

Deinitializes storage

Does not delete

Sequence

new constructor destructor delete

Page 22: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 22 -

C++ ClassesC++ Classes

Class Member Functions

Manager

Implementer

Helper

Access

Page 23: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 23 -

C++ ClassesC++ Classes

Class Member Functions

Manager Functions Manage the class objects

Initialization / deinitialization

Assignment

Memory management

Type conversion Examples of Manager Functions

Constructor / destructor Invoked

Explicitly by the compiler

Page 24: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 24 -

C++ ClassesC++ Classes

Class Member Functions

Implementer Functions

Define and provide

Capabilities associated with class

Invoked

Explicitly by the programmer

Provide

Significant portion of the public interface to the class

Page 25: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 25 -

C++ ClassesC++ Classes

Class Member Functions

class Stack

{

public:

Stack() { head = NULL; }

void push (Entry aValue) { add the value and set new head}

Entry pop() { set new head and return value}

private:

Entry* head;

};

Page 26: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 26 -

C++ ClassesC++ Classes

Class Member Functions

Helper Functions

Carry out auxiliary tasks

Not invoked (typically)

Explicitly by programmer

Typically dependent upon the underlying data structure

Generally declared private

Page 27: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 27 -

C++ ClassesC++ Classes

Class Member Functions

Access Functions

Provide access to otherwise protected or private data

Invoked

Explicitly by the programmer

Provide

Remaining component of the public interface

Page 28: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 28 -

C++ ClassesC++ Classes

Classes - Overloading Class Member Functions

Class Member Functions can use

Same name

In same scope

If signature is unique

• Same rules as non-member functions applied to disambiguate

• Must provide implementation for each overloaded function

Page 29: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 29 -

C++ ClassesC++ Classes

Classes - Name Scope

In C++

Have 3 kinds of scope….

File

Local

Class

Page 30: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 30 -

C++ ClassesC++ Classes

Classes - Name Scope

File

Portion of the program not contained within the definition of

Function

Class

Outer most scope of the program

Encloses

Local Scope

Class Class

Page 31: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 31 -

C++ ClassesC++ Classes

Classes - Name ScopeLocal

Portion of the program (in general)

Contained within the definition of a function

Each function

Represents distinct local scope

Within a function

Each block maintains an associated local scope

Those variables are local to the enclosing scope

Local scopes can be nested

Page 32: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 32 -

C++ ClassesC++ Classes

Classes - Name Scope

Class

Each class represents a distinct scope

Member functions

Treated as within the scope of their class

Page 33: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 33 -

C++ ClassesC++ Classes

Classes - UnionsUnion syntax is based upon that for structures and classessyntax

union Name {

type1 var1;type2 var2;

...typen varn;

};Name - union variable name

serves as the type specifiertypei - any of the typesvari - variable of typei

Page 34: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 34 -

C++ ClassesC++ Classes

Classes - UnionsIn comparison with a class…..

Union Can Only hold one type of variable at a timeDeclare

ConstructorDestructor

Union CannotDeclare static data membersDeclare as a member instance of a class that defines a

ConstructorDestructor

Page 35: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 35 -

C++ ClassesC++ Classes

Classes - Unions

union Mine

{

int intValue;

float floatValue;

char charValue;

};

Mine aMine;

char

int

float

Page 36: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 36 -

C++ ClassesC++ Classes

Classes - UnionsMember Access…..

Access union member exactly like struct or classunionName.memberunionPtr -> member

ExampleMine me;me.intVal = 10; // me will hold an intme.charVal = ‘a’; // me will hold a char

Members can bepublic - like a struct is the defaultprotectedprivate

Page 37: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 37 -

C++ ClassesC++ Classes

Classes - Unions

Initialization

Union can be initialized only with value of type of first member

Anonymous Union

Has no name tag

Not followed by object definition

Cannot have private or protected members

Cannot have function members

If defined at file scope … Must be declared static

Page 38: Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to

Copyright 2008 Oxford Consulting, Ltd 1 October 2008

- 38 -

C++ ClassesC++ Classes

Summary

Now have defined 3 variations on the basic class data structure

<classKey> <className> [:baseList] {< member list>};

classKey class, struct, union

className name of the class

baseClass list of classes the class is derived from

member list data and function members