PL 12 CH 11
PL 12 CH 11
ISBN 0-321-49362-1
Chapter 11 Topics
1-9
Language Examples: C++ (continued)
• Information Hiding
– Private clause for hidden entities
– Public clause for interface entities
– Protected clause for inheritance (Chapter 12)
• Constructors:
– Functions to initialize the data members of
instances (they do not create the objects)
– May also allocate storage if part of the object
is heap-dynamic
– Can include parameters to provide
parameterization of the objects
– Implicitly called when an instance is created
– Can be explicitly called
– Name is the same as the class name
• Destructors
– Functions to cleanup after an instance is
destroyed; usually just to reclaim heap storage
– Implicitly called when the object’s lifetime ends
– Can be explicitly called
– Name is the class name, preceded by a tilde (~)
1-17
Language Examples: Java
• Similar to C++, except:
– All user-defined types are classes
– All objects are allocated from the heap and
accessed through reference variables
– Individual entities in classes have access
control modifiers (private or public), rather
than clauses
- Implicit garbage collection of all objects
– Java has a second scoping mechanism,
package scope, which can be used in place of
friends
• All entities in all classes in a package that do not
have access control modifiers are visible
throughout the package
Copyright © 2018 Pearson. All rights reserved. 1-18
Language Examples: Package Scope
1-19
An Example in Java
class StackClass {
private int [] *stackRef;
private int [] maxLen, topIndex;
public StackClass() { // a constructor
stackRef = new int [100];
maxLen = 99;
topPtr = -1;
};
public void push (int num) {…};
public void pop () {…};
public int top () {…};
public boolean empty () {…};
}
1-23
Language Examples: C# (continued)
1-24
C# Property Example
public class Weather {
public int DegreeDays { //** DegreeDays is a property
get {return degreeDays;}
set {
if (value < 0 || value > 30)
Console.WriteLine(
"Value is out of range: {0}", value);
else degreeDays = value;}
}
private int degreeDays;
...
}
...
Weather w = new Weather();
int degreeDaysToday, oldDegreeDays;
...
w.DegreeDays = degreeDaysToday;
...
oldDegreeDays = w.DegreeDays;
def push(number)
if @topIndex == @maxLen
puts " Error in push – stack is full"
else
@topIndex = @topIndex + 1
@stackRef[@topIndex] = number
end
end
def pop … end
def top … end
def empty … end
end
1-33
Parameterized Classes in C# 2005
1-43
Language Examples: C# (continued)
1-44
Naming Encapsulations
namespace myStackSpace {
// Stack declarations
}
myStackSpace::topSub
1-49
Naming Encapsulations (continued)
• Java Packages
– Packages can contain more than one class
definition; classes in a package are partial
friends
– Clients of a package can use fully qualified
name or use the import declaration
1-51
Naming Encapsulations (continued)
• Ruby Modules:
- Ruby classes are name encapsulations, but Ruby
also has modules
- Typically encapsulate collections of constants and
methods
- Modules cannot be instantiated or subclassed, and
they cannot define variables
- Methods defined in a module must include the
module’s name
- Access to the contents of a module is requested
with the require method