1/82
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
abstraction
the view or representation of an entity that includes only the most significant attributes; allows one to collect instances of entities into groups in which their common attributes need not be considered
- simplifies the programming process
What are the two fundamental kinds of abstraction?
process abstraction and data abstraction
process abstraction
among the oldest in programming language design; all subprograms are these because they provide a way for a program to specify a process, without providing details of how it performs its task (at least in the calling program)
What is an essential part of data abstraction?
Its operations, which are defined as process abstractions.
abstract data type
data structure, in the form of a record, but which includes subprograms that manipulate its data; enclosure that includes only the data representation of one specific data type and the subprograms that provide the operations for that type
An instance of an abstract data type is called a/an
object
T/F: all built-in types are abstract data types
T
T/F: information hiding is a key concept in data abstraction
T; provides portability
What characteristics do user-defined abstract data types contain?
1. a type definition that allows program units to declare variables of the type but hides the representation of objects of the type
2. a set of operations for manipulating objects of the type
benefits of information hiding
- increased reliability
- reduces the range of code and number of variables of which a programmer must be aware when writing or reading a part of the program
- makes name conflicts less likely, because the scopes of variables is smaller
- increased flexibility
clients
program units that use a specific abstract data type
What are the reasons accessors are better than making data public?
1. read-only access can be provided by having a getter method but no corresponding setter method
2. constraints can be included in setters; for example, if the data value should be restricted to a particular range, the setter can enforce that
3. the actual implementation of the data member can be changed without affecting the clients if getters and setters are the only access
What operations are required by many abstract data types?
- iterators
- accessors
- constructors
- destructors (often used to reclaim heap storage that may be used by parts of abstract data type objects in languages that do not do implicit storage reclamation)
What are some examples of languages that directly support abstract data types?
C++, Java, C#, and Ruby
design issues of ADTs
- whether abstract data types can be parameterized
- what access controls are provided and how such controls are specified
- language designer must decide whether the specification of the type is physically separate from its implementation (or whether that is a developer choice)
data defined in a C++ class
data members
the functions (methods) defined in a class
member functions
implicitly inlined
when a member function has its header and body in the class definition, rather than separating them
T/F: placing member function definitions outside the class definition separates specification from implementation, a common goal of modern programming
T
protected members in C++
makes a member visible to subclasses, but not to clients
T/F: destructors are implicitly called in C++
T (ex. of destructor with class name Cat: ~Cat( ))
Java ADTs
- all objects are allocated from the heap and accessed through reference variables
- methods must be defined completely in a class
- a method body must appear with its corresponding method header (both declared and defined in a single syntactic unit)
- access modifiers are attached to each method or variable definition rather than having them in groups
advantage of Java ADTs over C++ ADTs
It uses implicit garbage collection of all objects.
- it allows the programmer to ignore manual deallocation
Java print statement syntax
System.out.println(" ");
C# ADTs
- combination of both C++ and Java, with new constructs
- like Java, all class instances are heap dynamic
- default constructors are predefined
- allows destructors to be defined, but it uses garbage collection for most of its heap objects, so rarely used
- uses same access modifier setup as Java
- implicitly uses getters and setters (inherited from Delphi)
C# structs
- lightweight classes
- like classes, but do not support inheritance
- value types, as opposed to reference types
- allocated on run-time stack instead of the heap
- all C# value types, including its primitive types, are these
Ruby ADTs
- class methods are distinguished from instance methods by having the class name appended to the beginning of their names with a period separator
- constructors are named "initialize" and cannot be overloaded, so only one per class
- dynamic, members can be added at any time
- methods can be removed from a class with "remove_method"
T/F: Adding flexibility for dynamic classes in Ruby harms readability.
T; to determine the behavior of a class at a particular point in a program, one must find all of its definitions in the program and consider all of them
Ruby access control
method access control is dynamic, so access violations are detected only during execution
- default method access is public, but can also be protected or private
- can specify access control before function or at end of class without parameters
(ex. def method7
end
private: method7
)
Ruby attributes
instance data that is accessible through accessor methods
Ruby setters syntax
def sum=(new_sum)
@sum = new_sum
end
getters:
def sum
@sum
end
How can getters and setters in Ruby be implicitly generated?
include calls to attr_reader (getter) and attr_writer (setter)
ex. attr_reader :sum, :total
attr_writer :sum
T/F: Arrays in Ruby are not arrays of references to objects.
F; are arrays of references to objects
What is the difference between how static typed and dynamic typed languages store type elements?
Static typed languages require specific code changes (templates in C++) to accept any type as class parameters, while dynamic typed (like Ruby) do not require any changes since dynamic.
What were the problems with original collection types in Java?
1. every time an object was removed from the collection, it had to be cast to the appropriate type
2. no error checking when elements were added to the collection
3. the collection types could not store primitive types
- instead, uses new when instantiating classes to specify type, and uses "extends" ClassName when dependent on a class
C# parameterized ADTs (defining generic type)
- first used collection classes like Java, but ran into same issues
- now uses generic classes (predefined ones are Array, List, Stack, Queue, and Dictionary)
- eliminates problems of allowing mixed types in collections and requiring casts when objects are removed from the collections
- user can define generic classes; can be defined for its elements to be indexed
What is one capability that Java provides that C# does not provide with parameterized ADTs?
wildcard classes
encapsulation
organized programs into collections of logically related code and data, each of which can be compiled without recompilation of the rest of the program
- often placed in libraries and made available for reuse in programs other than those for which they were written
examples of encapsulation
- C++ and C using header and implementation files
- C# using the .NET programming languages utility: the assembly (built by .NET compilers, where a .NET application consists of one or more assemblies)
assembly (in terms of .NET usage)
a file that appears to application programs to be a single dynamic link library or an executable
- internal access modifier in C# is visible to all classes in the assembly in which it appears
naming encapsulations
- C++ and C# namespace keyword
- Java package keyword, import keyword from package (no friend functions/classes)
- Ruby module keyword, require 'fileName'
Which language was the first to offer complete support for object-oriented programming?
Smalltalk
What three key language features must OOL provide?
- ADTs (encapsulation)
- inheritance
- dynamic binding of method calls to methods (polymorphism)
What are classes called that are defined through inheritance?
child class/derived class/subclass, where main class is called parent/base/superclass class
In inheritance, a new method is said to _________ the inherited method.
override; which is then called an overridden method
multiple inheritance
processes where classes have more than one parent class
abstract method
where a protocol is defined in a base class, but never called; if a base class contains at least one of these, the class is called an abstract class; usually cannot be instantiated, only its subclasses with full implementation can be instantiated; purely virtual function
principle of substitution
if a language allows programs in which a variable of a class can be subed for a variable of one of its ancestor classes in any situation, without causing type errors and without changing the behavior of the program, the language supports this principle
What is used as an alternative to multiple inheritance to avoid issues of complexity and efficiency?
interfaces, where methods are declared but not defined
object slicing
when excess space not able to fit on the stack is truncated
Ruby syntax for defining a child class
class MyChildClass < BaseClass
T/F: Ruby is compiled.
F; Ruby is interpreted
class instance record (CIR)
structure for instance variables of a C++ class; Object-level structure storing instance data and vtable pointer.
virtual method table (vtable)
Class-level structure storing function pointers for dynamic dispatch.
virtual method
a method that is allowed to be overridden
reflection
provides the possibility of late binding of calls to methods that are outside the inheritance hierarchy of the calling code; allows run-time access to dynamically modify behavior
metadata
types and structure of a program
introspection
process of a program examining its metadata
ways program can modify behavior dynamically
- could use metadata
- could change metadata directly
- could intercede in the execution of the program
reflection in different languages
- Java (uses Class objects and .getClass() methods
- C# uses Type, .GetType, MethodInfo .GetMethod
problems with using reflection
- performance suffers
- exposes private fields and methods
- operations may not work when code is run under a security manager, also making it non-portable
irb
interactive Ruby
str.sub vs str.gsub
sub only replaces first occurrence, gsub replaces all
str.sub(/^../,"House")
replaces first (^) two letters (..) of string with House
str.sub(/..$/,"House")
replaces last two letters of string with House
str.scan(/./)
scans for every single character; (.) indicates every character besides newline, even spaces
str.scan(/\w\w/) { |x| puts x }
puts consecutive word characters beside each other, each on a separate line (excluding spaces since not word characters)
str.scan(/\W/) { |x| puts x }
matches any non-word character
s.scan(/\d/) { |x| puts x}
puts all digits in a string on a separate line
s.scan(/\d\d/) { |x| puts x}
puts consecutive digits on a separate line (will omit digits that aren't consecutive)
s.scan(/\D/) { |x| puts x}
includes everything other than a digit on a separate line
s.scan(/\d{2}/) { |x| puts x}
prints exactly 2 consecutive digits
s.scan(/\d{2,3}/) { |x| puts x}
2 or 3 consecutive digits
s.scan(/d+/)
puts all d occurrences in an array; if none, just puts "d" in an array
s.scan(/\d*/) { |x| puts x }
scans for 0 or more digits, including space characters
s.scan(/\s/) { |x| puts x }
whitespace
s.scan(/[abcABC]/) { |x| puts x }
scans for all included characters
puts "Average is #{"%.1f"%average}"
print statement when including rounding
CSV.open('students2.csv','w') do |csv|
10 students.each do |stdnt|
11 csv << stdnt
12 end
13 end
writing to a CSV file
a = Square.new(3)
puts a.perimeter
use Class.new(parameters) when creating a class object
def self.count
@@numSquares
end
a = Square.new
Square.count
referring to class itself, not particular instance
system() vs exec()
system does operation then continues in code, exec does operation and then terminates program
#!/usr/local/bin/ruby
2
3 f = fork # same usage as Perl fork
4
5 if f.nil? # if 0
6 puts "Child"
7 else
8 puts "Parent"
9 end
IPC with pipes in Ruby