Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C struct...
-
Upload
aubrie-golden -
Category
Documents
-
view
212 -
download
0
Transcript of Copyright 2008 Oxford Consulting, Ltd 1 October 2008 - 1 - C++ Classes Enhanced Structures C struct...
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
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
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
Copyright 2008 Oxford Consulting, Ltd 1 October 2008
- 4 -
C++ ClassesC++ Classes
Classesclass Name
{
body;
};
class Vehicle
{
…
};
Vehicle myVehicle;
Vehicle * rentalVehicle = &my Vehicle;
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 ;
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
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
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
Copyright 2008 Oxford Consulting, Ltd 1 October 2008
- 9 -
C++ ClassesC++ Classes
Class Access
Access Restriction Increasing
Class
privateprotectedpublic Message
Message
Object
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
Copyright 2008 Oxford Consulting, Ltd 1 October 2008
- 11 -
C++ ClassesC++ Classes
ClassesDeclaration
class Name
{
public:
..…
protected:
..…
private:
..…
};
private by default
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
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
};
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
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?
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;
}
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
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
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?
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;
};
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
Copyright 2008 Oxford Consulting, Ltd 1 October 2008
- 22 -
C++ ClassesC++ Classes
Class Member Functions
Manager
Implementer
Helper
Access
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
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
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;
};
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
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
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
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
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
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
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
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
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
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
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
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
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