Assignment Chap 5,6,7,8,9,10,11,12

Selamat darang di blog saya ini. Blog ini ditulis atas permintaan dosen terkeren saya Tri Djoko Wahjono, yang mengajar mata kuliah Konsep Bahasa Pemrograman di Binus University di kampus yang keren dan eksklusif ini Alam Sutera (banyak kupu-kupu berterbangan, banyak burung gereja minta makan, #nofilter) – Jakarta.

Chap 6:

Review Questions

  1. What are the advantages of user-defined enumeration types?

*User-defined enumeration types are most useful when a data type can take one of a small, discrete set of values, each of which have some meaning that is not a number. A favorite example in textbooks is a type for the suit of a playing card. There are four options: SPADE, CLUB, HEART, DIAMOND, and so we would make an enumeration type with these four entries. The main advantages are efficiency in storage (compared to e.g. storing these values as strings) and more readable code.

  1. In what ways are the user-defined enumeration types of C# more reliable than those of C++?

*Since C# enumeration types are not coerced into integer types, as the C++ does so, it’s more reliable.

  1. What are the design issues for arrays?

*-What types are legal for subscripts?

-Are subscripting expressions in elemnet refrence range checked?

-When are subscript ranges bound?

-When does allocation take place?

-What is the maximum number of subscripts?

-Can array objects be initialized?

-Are any kind of slices supported?

  1. Define static, fixed stack-dynamic, stack-dynamic, fixed heap-dynamic, and heap-dynamic arrays. What are the advantages of each?

*Static array is an array which the subscript ranges are statically bound and storage allocation is static (before run-time). The advantage is efficiency (no dynamic allocation).

Fixed stack-dynamic array is an array which the subscript ranges are statically bound, but the allocation is done at declaration time. The advantage is space efficiency

Stack-dynamic array is an array which the subscript ranges are dynamically bound and the storage allocation is dynamic (done at run-time). The advantage is flexibility (the size of an array need not be known until the array is to be used).

Fixed heap-dynamic array is similar to fixed stack-dynamic, storage binding is dynamic but fixed after allocation (i.e., binding is done when requested and storage is allocated from heap, not stack). The advantage of fixed heap-dynamic array is flexibility (the array’s size always fits the problem).

Heap-dynamic array is an array which the binding of subscript ranges and storage allocation is dynamic and can change any number of times. The advantage is flexibility (arrays can grow or shrink during program execution).

  1. What happens when a nonexistent element of an array is referenced in Perl?

*If an r-value is required, undef is returned. If an l-value is required, the array is extended, then the newly created but undefined element is returned. No error is reported.

Problem Set

  1. Explain all of the differences between Ada’s subtypes and derived types.

*Ada’s subtype is compatible with its base type, so you can mix operands of the base type with operands of the base type. While Ada’s derived type is a completely separate type that has the same characteristics as its base type. We can’t mix operands of a derived type with operands of the base type.

  1. What significant justification is there for the -> operator in C and C++?

*The only justification for -> operator in C and C++ is writability. It is slightly easier to write p->q than (*p).q.

  1. What are all of the differences between the enumeration types of C++ and those of Java?

*In C++, an enumeration is just a set of named, integral constants. Also, C++ will implicitly convert enum values to their integral equivalent. In Java, an enumeration is more like a named instance of a class. You have the ability to customize the members available on the enumeration. Java will explicitly convert enum values to their integral equivalent.

  1. The unions in C and C++ are separate from the records of those languages, rather than combined as they are in Ada. What are the advantages and disadvantages to these two choices?

*

Union separated from records Union combined with records
Advantages -Reduce developer overhead (less checking)

-Goes with the C++ design (C++ is not a weak strong typing language)

-Support type checking

-Goes with the Ada design (Ada is strong typing language)

Disadvantages -No type checking of references

-May end with wrong or unexpected results

-Increase developer overhead

-Using more functions to resolve type compatibility

  1. Multidimensional arrays can be stored in row major order, as in C++, or in column major order, as in Fortran. Develop the access functions for both of these arrangements for three-dimensional arrays.

*Let the subscript ranges of the three dimensions be named min(1), min(2), min(3), max(1), max(2), and max(3). Let the sizes of the subscript ranges be size(1), size(2), and size(3). Assume the element size is 1.

Row Major Order:

location(a[i,j,k]) = (address of a[min(1),min(2),min(3)]) + ((i-min(1))*size(3) + (j-min(2)))*size(2) + (k-min(3))

Column Major Order:

location(a[i,j,k]) = (address of a[min(1),min(2),min(3)]) + ((k-min(3))*size(1) + (j-min(2)))*size(2) + (i-min(1))

Chapter  7:

Review Questions

  1. What associativity rules are used by APL?

*In APL, all operators have the same level of precedence. Thus, the order of evaluation of operators in APL expressions is determined entirely by the associativity rule, which is right to left for all operators.

  1. What is the difference between the way operators are implemented in C++ and Ruby?

*The difference is in Ruby, all the arithmetic, relational, and assignment operator, as well as array indexing, shifts, and bitwise logic operators, are implemented as methods. While in C, they aren’t implemented as methods.

  1. Define functional side effect.

*A functional side effect is a side effect of a function which occurs when the function changes either one of its parameters or a global variable.

  1. What is a coercion?

*A coercion is a process by which a compiler automatically converts a value of one type into a value of another type when that second type is required by the surrounding context.

  1. What is a conditional expression?

*A conditional expression is an expression that returns value A or value B depending on whether a Boolean value is true or false. A conditional expression lets you write a single assignment statement that has the same effect as the following:

if condition:

x = true_value

else:

x = false_value

Problem Set

  1. Should C’s single-operand assignment forms (for example, ++count) be included in other languages (that do not already have them)? Why or why not?

*Yes C should, because those assigning operations make operation simpler and easy to know if that operation is assigning operation. And it will ease the increment or even decrement while we use in looping.

  1. Describe a situation in which the add operator in a programming language would not be commutative.

*If the add operator for a language is also used to concatenate strings, it’s quite apparent that it would not be commutative.

For example:

“abc” + “def” = “abcdef”

“def” + “abc” = “defabc”

These two strings are obviously not equal, so the addition operator is not commutative.

  1. Describe a situation in which the add operator in a programming language would not be associative.

*It is not associative when it includes the other operator with higher precedence like the multiplication and division.

  1. Assume the following rules of associativity and precedence for expressions:

Precedence

Highest           *, /, not

+, –, &, mod

– (unary)

=, /=, <, <=, >=, >

And

Lowest             or, xor

Associativity      Left to right

Show the order of evaluation of the following expressions by parenthesizing all subexpressions and placing a superscript on the right parenthesis to indicate order. For example, for the expression

a + b * c + d

the order of evaluation would be represented as

((a + (b * c)1)2 + d)3

  1. a * b – 1 + c                                ((( a * b )1 – 1)2 + c )3
  2. a * (b – 1) / c mod d                   ((( a * ( b – 1 )1 )2 / c )3 mod d )4
  3. (a – b) / c & (d * e / a – 3)          (((a – b)1 / c)2 & ((d * e)3 / a)4 – 3)5)6
  4. -a or c = d and e                        (( -a )1 or ( ( c = d )2 and e )3 )4
  5. a > b xor c or d <= 17               (((a > b)1 xor c)3 or (d <= 17)2 )4
  6. -a + b                                       (–( a + b )1 )2
  1. Show the order of evaluation of the expressions of Problem 9, assuming that there are no precedence rules and all operators associate right to left.

*

  1. ( a * ( b – ( 1 + c )1 )2 )3
  2. ( a * ( ( b – 1 )2 / ( c mod d )1 )3 )4
  3. ( ( a – b )5 / ( c & ( d * ( e / ( a – 3 )1 )2 )3 )4 )6
  4. ( – ( a or ( c = ( d and e )1 )2 )3 )4
  5. ( a > ( xor ( c or ( d <= 17 )1 )2 )3 )4

f. ( – ( a + b )1 )2

Chapter 8:

Review Questions

  1. What is unusual about Python’s design of compound statements?

*Python uses indentation to specify compound statements. For example,

if x > y :

x = y

print “case 1″

equally indent statements are grouped as one compound statement.

  1. Under what circumstances must an F# selector have an else clause?

*If the expression returns a value, it must have an else clause.

  1. What are the common solutions to the nesting problem for two-way selectors?

*The common solution is to force an alternative semantics, by using compound statements.

  1. What are the design issues for multiple-selection statements?

*-What is the form and type of the control statement?

-How are the selectable segments specified?

-Is execution flow through the structure restricted to include just a single selectable segment?

-How are case values specified?

-What is done about unrepresented expression values?

  1. Between what two language characteristics is a trade-off made when deciding whether more than one selectable segment is executed in one execution of a multiple selection statement?

*In Ada, the choice lists of the case statement must be exhaustive, so that there can be no unrepresented values in the control expression. In C++, unrepresented values can be caught at run time with the default selector. If there is no default, an unrepresented value causes the whole statement to be skipped.

Problem Set

  1. Analyze the potential readability problems with using closure reserved words for control statements that are the reverse of the corresponding initial reserved words, such as the case-esac reserved words of ALGOL 68. For example, consider common typing errors such as transposing characters.

*The potential readability problem is the typing errors. It’s very possible to occur if we don’t type the code carefully.

  1. Use the Science Citation Index to find an article that refers to Knuth (1974). Read the article and Knuth’s paper and write a paper that summarizes both sides of the goto issue.

*An alternative viewpoint is presented in Donald Knuth’s Structured Programming with go to Statements, which analyzes many common programming tasks and finds that in some of them GOTO is the optimal language construct to use.[7] In their quasi-standard book on the C programming language, Dennis Ritchie and Brian Kernighan warn that goto is “infinitely abusable”, but also suggest that it could be used for end-of-function error handlers and for multi-level breaks from loops.

  1. In his paper on the goto issue, Knuth (1974) suggests a loop control statement that allows multiple exits. Read the paper and write an operational semantics description of the statement.

*Operational semantics are a category of formal programming language semantics in which certain desired properties of a program, such as correctness, safety or security, are verified by constructing proofs from logical statements about its execution and procedures, rather than by attaching mathematical meanings to its terms (denotational semantics).

  1. What are the arguments both for and against the exclusive use of Boolean expressions in the control statements in Java (as opposed to also allowing arithmetic expressions, as in C++)?

*The primary argument for using Boolean expressions exclusively as control expressions is the reliability that results from disallowing a wide range of types for this use. In C, for example, an expression of any type can appear as a control expression, so typing errors that result in references to variables of incorrect types are not detected by the compiler as errors. No , it would not be a good idea. Although this custom precedence sounds like increasing flexibility, requiring parentheses to show a custom precedence would impact in readability and writability of a program.

  1. In Ada, the choice lists of the case statement must be exhaustive, so that there can be no unrepresented values in the control expression. In C++, unrepresented values can be caught at run time with the default selector. If there is no default, an unrepresented value causes the whole statement to be skipped. What are the pros and cons of these two designs (Ada and C++)?

*Ada was designed for military grade software development. The idea is that whenever you modify code in such a way that a new case emerges (for example adding a new value for an enumeration type), you are forced to manually revisit (and therefore re-validate) all the case statements that analyze it. Having a “default” is risky: you may forget that there is a case somewhere where the new case should not have been handled by the default.

Chapter 9:

6.What is a Ruby array formal parameter?

Ruby supports a complicated but highly flexible actual parameter configuration. The initial parameters are expressions, whose value objects are passed to the corresponding formal parameters. The initial parameters can be following by a list of key => value pairs, which are placed in an anonymous hash and a reference to that hash is passed to the next formal parameter. These are used as a substitute for keyword parameters, which Ruby does not support.
7.What is a parameter profile? What is a subprogram protocol?
The parameter profile of a subprogram contains the number, order, and types of its formal parameters. The protocol of a subprogram is its parameter profile plus, if it is a function, its return type. In languages in which subprograms have types, those types are defined by the subprogram’s protocol.
8.What are formal parameters? What are actual parameters?
The parameters in the subprogram header are called formal parameters. They are sometimes thought of as dummy variables because they are not variables in the usual sense: In most cases, they are bound to storage only when the subprogram is called, and that binding is often through some other program variables. While actual parameters is a parameter that bound to the formal parameters of the subprogram. Subprogram call statements must include the name of the subprogram and a list of parameters to be bound to the formal parameters of the subprogram.
9.What are the advantages and disadvantages of keyword parameters?
The advantage of keyword parameters is that they can appear in any order in the actual parameter list. The disadvantage to keyword parameters is that the user of the subprogram must know the names of formal parameters.
10.What are the differences between a function and a procedure?

There are two distinct categories of subprograms—procedures and functions— both of which can be viewed as approaches to extending the language. All sub- programs are collections of statements that define parameterized computations. Functions return values and procedures do not. Procedures can produce results in the calling program unit by two methods: (1) If there are variables that are not formal parameters but are still visible in both the procedure and the calling program unit, the procedure can change them; and (2) if the procedure has formal parameters that allow the transfer of data to the caller, those parameters can be changed. However Functions are called by appearances of their names in expressions, along with the required actual parameters. And functions define new user-defined operators.

Problem Sets :

6.Present one argument against providing both static and dynamic local variables in subprograms.
In subprograms local variables can be static or dynamic; If local variable treated statically: This allows for compile-time allocation/ deallocation and ensures proper type checking but does not allow for recursion. And if local variables treated dynamically: This allows for recursion at the cost of run-time allocation/ deallocation and initialization because these are stored on a stack, referencing is indirect based on stack position and possibly timeconsuming

7. Consider the following program written in C syntax:

void fun (int first, int second) {

first += first;

second += second;

}

void main() {

int list[2] = {1, 3};

fun(list[0], list[1]);

}

For each of the following parameter-passing methods, what are the values of the list array after execution?
Passed by value
Passed by reference
Passed by value-result
Passed by value : list[2] = { 3 , 5 }
Passed by reference : list[2] = { 6 , 10 }
Passed by value-result : list[2] = { 6 , 10 }

8.Argue against the C design of providing only function subprograms.
If a language provides only functions, then either programmers must live with the restriction of returning only a single result from any subprogram, or functions must allow side effects, which is generally considered bad. Since having subprograms that can only modify a single value is too restrictive, C’s choice is not good.
9.From a textbook on Fortran, learn the syntax and semantics of statement functions. Justify their existence in Fortran.
The Fortran 1966 standard provided a reference syntax and semantics, but vendors continued to provide incompatible extensions. These standards have improved portability.
10.Study the methods of user-defined operator overloading in C++ and Ada, and write a report comparing the two using our criteria for evaluating languages.
One of the nice features of C++ is that you can give special meanings to operators, when they are used with user-defined classes. This is called operator overloading. You can implement C++ operator overloads by providing special member-functions on your classes that follow a particular naming convention. For example, to overload the + operator for your class, you would provide a member-function named operator+ on your class. Meanwhile for Ada, since much of the power of the language comes from its extensibility, and since proper use of that extensibility requires that we make as little distinction as possible between predefined and user-defined types, it is natural that Ada also permits new operations to be defined, by declaring new overloading of the operator symbols.
Chapter 10:

6. What is the difference between an activation record and an activation record instance?

*An activation record is the format, or layout, of the moncode part of a subprogram. An activation record instance is a concrete example of an activation record, a collection of data in the form of an activation record.7. Why are the return address, dynamic link, and parameters placed in the bottom of the activation record?

*It’s because the entry must appear first.8. What kind of machines often use registers to pass parameters?

*RISC Machines often use registers to pass parameters.9. What are the two steps in locating a nonlocal variable in a static-scoped language with stack-dynamic local variables and nested subprograms?

*First step, find correct activation record (the harder part) and then the second step is determine the offset within that activation record (easy part).10. Define static chain, static_depth, nesting_depth, and chain_offset.

*Static chain is chain of static links connecting an activation record to all of it’s static ancestors (it’s enclosing subprograms).
Static depth is depth of the nesting for each enclosing static scope.
Nesting depth is the difference between the static depth of the reference and that of the scope where it was declared.
Chain offset is same as nesting depth.

Problem Set :

6. Although local variables in Java methods are dynamically allocated at the beginning of each activation, under what circumstances could the value of a local variable in a particular activation retain the value of the previous activation?
*Each activation allocates variables in exactly the same order. Variables are not initialized to any value unless the program contains an initialization statement for the variable – they simply have whatever value is stored in the location they are allocated. If a procedure finishes executing, returns, and is immediately reinvoked, a variable would be assigned the same stack location it had on the previous invocation, and would have the last value from that previous invocation.7. It is stated in this chapter that when nonlocal variables are accessed in a dynamic-scoped language using the dynamic chain, variable names must be stored in the activation records with the values. If this were actually done, every nonlocal access would require a sequence of costly string comparisons on names. Design an alternative to these string comparisons that would be faster.

*Using approach that uses an auxiliary data structure called a display. Or, to write variable names as integers. These integers act like an array. So when the activation happens, the comparisons will be faster.8. Pascal allows gotos with nonlocal targets. How could such statements be handled if static chains were used for nonlocal variable access? Hint: Consider the way the correct activation record instance of the static parent of a newly enacted procedure is found (see Section 10.4.2).

*Based on the hint statement, the target of every goto in a program could be represented as an address and a nesting depth, where the nesting depth is the difference between the nesting level of the procedure that contains the goto and that of the procedure containing the target. Then, when a goto is executed, the static chain is followed by the number of links indicated in the nesting depth of the goto target. The stack top pointer is reset to the top of the activation record at the end of the chain.9. The static-chain method could be expanded slightly by using two static links in each activation record instance where the second points to the static grandparent activation record instance. How would this approach affect the time required for subprogram linkage and nonlocal references?

*Including two static links would reduce the access time to nonlocals that are defined in scopes two steps away to be equal to that for nonlocals that are one step away. Overall, because most nonlocal references are relatively close, this could significantly increase the execution efficiency of many programs.10. Design a skeletal program and a calling sequence that results in an activation record instance in which the static and dynamic links point to different activation-recorded instances in the run-time stack.

*\\
\emph{Answer}:\\
procedure Main\_2 is\\
\verb+ + X : Integer;\\
\verb+ +procedure Bigsub is\\
\verb+ +\verb+ + A, B, C : Integer;\\
\verb+ +\verb+ + procedure Sub1 is\\
\verb+ +\verb+ +\verb+ + A, D : Integer;\\
\verb+ +\verb+ +\verb+ + begin — of Sub1\\
\verb+ +\verb+ +\verb+ + A := B + C; $\longleftarrow$ 1\\
\verb+ +\verb+ +\verb+ + …\\
\verb+ + end; — of Sub1\\
\verb+ + procedure Sub2(X : Integer) is\\
\verb+ +\verb+ + B, E : Integer;\\
\verb+ +\verb+ + procedure Sub3 is\\
\verb+ +\verb+ +\verb+ + C, E : Integer;\\
\verb+ +\verb+ +\verb+ + begin — of Sub3\\
\verb+ +\verb+ +\verb+ + …\\
\verb+ +\verb+ +\verb+ + Sub1;\\
\verb+ +\verb+ +\verb+ + …\\
\verb+ +\verb+ +\verb+ + E := B + A; $\longleftarrow$ 2\\
\verb+ +\verb+ + end; — of Sub3\\
\verb+ +\verb+ + begin — of Sub2\\
\verb+ +\verb+ + …\\
\verb+ +\verb+ + Sub3;\\
\verb+ +\verb+ + …\\
\verb+ +\verb+ + A := D + E; $\longleftarrow$ 3\\
\verb+ + end; — of Sub2\\
\verb+ + begin — of Bigsub\\
\verb+ +\verb+ + …\\
\verb+ +\verb+ + Sub2(7);\\
\verb+ +\verb+ + …\\
\verb+ + end; — of Bigsub\\
begin — of Main\_2\\
\verb+ + …\\
\verb+ + Bigsub;\\
\verb+ + …\\
end; — of Main\_2\\
\\
The sequence of procedure calls is:\\
Main\_2 calls Bigsub\\
Bigsub calls Sub2\\
Sub2 calls Sub3\\
Sub3 calls Sub1\\
\\
The activation records with static and dynamic links is as follows:\\
\begin{figure}
\centering
\includegraphics[scale=0.5]{ari}
\end{figure}At position 1 in procedure Sub1, the reference is to the local variable,
A, not to the nonlocal variable A from Bigsub. This reference to A has the
chain\_offset/local\_offset pair (0, 3). The reference to B is to the nonlocal B
from Bigsub. It can be represented by the pair (1, 4). The local\_offset is 4,
because a 3 offset would be the first local variable (Bigsub has no parameters). Notice that if the dynamic link were used to do a simple search for
an activation record instance with a declaration for the variable B, it would
find the variable B declared in Sub2, which would be incorrect. If the (1, 4)
pair were used with the dynamic chain, the variable E from Sub3 would be
used. The static link, however, points to the activation record for Bigsub,
which has the correct version of B . The variable B in Sub2 is not in the
referencing environment at this point and is (correctly) not accessible. The
reference to C at point 1 is to the C defined in Bigsub, which is represented
by the pair (1, 5).\\
\\

\noindent

Chapter 11:

Review Questions :

6. Explain how information hiding is provided in an Ada package.

There are two approaches to hiding the representation from clients in the package specification. The first one is to include two sections in the package specification, the second one is in which entities are visible to clients and one that hides its contents.

7. To what is the private part of an Ada package specification visible?

The private part of an Ada package specification is visible to the compiler but not to the client programs.8. What is the difference between private and limited private types in Ada?

Private types have built-in operations for assignment and comparison. Limited private types don’t have built-in operations.9. What is in an Ada package specification? What about a body package?

Package specification, is an Ada package which provides the interface of the encapsulation (and perhaps more). Body package, is an Ada package which provides the implementation of most, if not all, of the entities named in the associated package specification.10. What is the use of the Ada with clause?

The use of Ada with clause is to make the names defined in external packages visible.Problem Set:

6. Discuss the advantages of C# properties, relative to writing accessor methods in C++ or Java.
One of the advantage of C# properties relative writing accessor methods in C++ or Java is it can control access to the fields.

7. Explain the dangers of C’s approach to encapsulation.

The main problem is that the biggest part of encapsulation is done via hidding, rather than protection. This hidding is achieved through definition hidding: a header file is preprocessed (which is a synonym for copy-pasted) into the implementation file. Anyone with this header file will be able to access any method or public variable of a the client related to the header, left appart any “static” method / variable. Static is actually the only rue level of protection here, as it’s the only one that another unit (file) would not be able to access even if it’s aware of it’s existence. This whole “protection-is-name-hiding” approach leads to a load of problems: you can access a symbol using the wrong datatype and your compiler will happily do so. To protect critical parts, you should rely on text being hidden from the compiler while it’s processing certain units (via #ifdef / #define).8. Why didn’t C++ eliminate the problems discussed in Problem 7?

It didn’t eliminate the problem because it evolved from C. Hence, it kept a lot of backward compatibility, and the same way of doing basic things. While some problems where solved (like the protected access, which is in-between normal and static in C), some stay, as the symbol access using wrong datatype (inherent to the linker, which doesn’t do type-checking).9. What are the advantages and disadvantages of the Objective-C approach to syntactically distinguishing class methods from instance methods?

Instance methods use an instance of a class, whereas a class method can be used with just the class name. A class is like the blueprint of a house: You only have one blueprint and (usually) you can’t do that much with the blueprint alone. An instance (or an object) is the actual house that you build based on the blueprint: You can build lots of houses from the same blueprint. You can then paint the walls a different color in each of the houses, just as you can independently change the properties of each instance of a class without affecting the other instances.10. In what ways are the method calls in C++ more or less readable than those of Objective-C?

In Objective C, all method calls are essentially virtual. This makes it a bit easier on the programmer, but it comes at a possible performance decrease. So sometimes methods call in C++ can be more or less readable than those of Objective-C.
Chapter 12:

Review Questions :

6. Describe a situation where dynamic binding is a great advantage over its absence.

There is a base class, A, that defines a method draw that draws some figure associated with the base class. A second class, B, is defined as a subclass of A. Objects of this new class also need a draw method that is like that provided by A but a bit different. With overriding, we can directly modify B’s draw function. But without it, we either make a specific function in A for B and inherit it.7. What is a virtual method?

A virtual method is a declared class method that allows overriding by a method with the same derived class signature. Virtual methods are tools used to implement the polymorphism feature of an object-oriented language, such as C#. When a virtual object instance method is invoked, the method to be called is determined based on the object’s runtime type, which is usually that of the most derived class.8. What is an abstract method? What is an abstract class?

An abstract method is a method which all descendant classes should have. An abstract class is a class which has abstract method.9. Describe briefly the eight design issues used in this chapter for object-oriented languages.

-What non-objects are in the language?
-Are Subclasses Subtypes? If so, derived objects can be legally used wherever a parent object could be used.
-Type Checking and Polymorphism
-Single and Multiple Inheritance. Inherit from 1 (or more than 1) parent.
-Object Allocation and Deallocation. Are objects allocated from heap or stack.
-Dynamic and Static Binding. When are messages bound to methods, before or during run-time?
-Nested Classes. Can a class be nested inside another class?
-Initialization of Objects. Are objs init’d when created? Implicit or explicit?10. What is a nesting class?

A nesting class is a class defined inside another class.Problem Set:

6. Compare the multiple inheritance of C++ with that provided by interfaces in Java.

C++ inheritance is implementation inheritance. That is, a class inheriting from two of more superclasses actually inherits the code from those classes. Java’s interface mechanism is an interface inheritance. That is, a class implementing two or more interfaces simply inherits (and must provide its own implementations for) the methods of the interface.7. What is one programming situation where multiple inheritance has a significant advantage over interfaces?

When two or more parent classes are derived from one grandparent class and has one child (diamond problem).8. Explain the two problems with abstract data types that are ameliorated by inheritance.

The problems solved are reusability of code and “extensibility”. Reusability because one won’t have to copy/paste his code from one data type to another, allowing for a greater readability. Extensibility because a method can accept a certain class as an argument, and get a child class of this one. This will allow the user to have a wider set of functionality, but the method will still be able to know that the entities it relies on are present.

9. Describe the categories of changes that a subclass can make to its parent class.

Subclasses can add things (variables, methods). Subclass in C++ can effectively remove a method using “private” inheritance. Inherited methods can be overridden.

10. Explain one disadvantage of inheritance.

Language & implementation complexity. The shared inheritance problem of multiple inheritance. Subclass is dependent upon its base class (which might change over time).

Posted in Uncategorized | Leave a comment

Assignment Chap 5,6,7,8,9,10,11,12

Selamat darang di blog saya ini. Blog ini ditulis atas permintaan dosen terkeren saya Tri Djoko Wahjono, yang mengajar mata kuliah Konsep Bahasa Pemrograman di Binus University di kampus yang keren dan eksklusif ini Alam Sutera (banyak kupu-kupu berterbangan, banyak burung gereja minta makan, #nofilter) – Jakarta.

Chap 6:

Review Questions

  1. What are the advantages of user-defined enumeration types?

*User-defined enumeration types are most useful when a data type can take one of a small, discrete set of values, each of which have some meaning that is not a number. A favorite example in textbooks is a type for the suit of a playing card. There are four options: SPADE, CLUB, HEART, DIAMOND, and so we would make an enumeration type with these four entries. The main advantages are efficiency in storage (compared to e.g. storing these values as strings) and more readable code.

  1. In what ways are the user-defined enumeration types of C# more reliable than those of C++?

*Since C# enumeration types are not coerced into integer types, as the C++ does so, it’s more reliable.

  1. What are the design issues for arrays?

*-What types are legal for subscripts?

-Are subscripting expressions in elemnet refrence range checked?

-When are subscript ranges bound?

-When does allocation take place?

-What is the maximum number of subscripts?

-Can array objects be initialized?

-Are any kind of slices supported?

  1. Define static, fixed stack-dynamic, stack-dynamic, fixed heap-dynamic, and heap-dynamic arrays. What are the advantages of each?

*Static array is an array which the subscript ranges are statically bound and storage allocation is static (before run-time). The advantage is efficiency (no dynamic allocation).

Fixed stack-dynamic array is an array which the subscript ranges are statically bound, but the allocation is done at declaration time. The advantage is space efficiency

Stack-dynamic array is an array which the subscript ranges are dynamically bound and the storage allocation is dynamic (done at run-time). The advantage is flexibility (the size of an array need not be known until the array is to be used).

Fixed heap-dynamic array is similar to fixed stack-dynamic, storage binding is dynamic but fixed after allocation (i.e., binding is done when requested and storage is allocated from heap, not stack). The advantage of fixed heap-dynamic array is flexibility (the array’s size always fits the problem).

Heap-dynamic array is an array which the binding of subscript ranges and storage allocation is dynamic and can change any number of times. The advantage is flexibility (arrays can grow or shrink during program execution).

  1. What happens when a nonexistent element of an array is referenced in Perl?

*If an r-value is required, undef is returned. If an l-value is required, the array is extended, then the newly created but undefined element is returned. No error is reported.

Problem Set

  1. Explain all of the differences between Ada’s subtypes and derived types.

*Ada’s subtype is compatible with its base type, so you can mix operands of the base type with operands of the base type. While Ada’s derived type is a completely separate type that has the same characteristics as its base type. We can’t mix operands of a derived type with operands of the base type.

  1. What significant justification is there for the -> operator in C and C++?

*The only justification for -> operator in C and C++ is writability. It is slightly easier to write p->q than (*p).q.

  1. What are all of the differences between the enumeration types of C++ and those of Java?

*In C++, an enumeration is just a set of named, integral constants. Also, C++ will implicitly convert enum values to their integral equivalent. In Java, an enumeration is more like a named instance of a class. You have the ability to customize the members available on the enumeration. Java will explicitly convert enum values to their integral equivalent.

  1. The unions in C and C++ are separate from the records of those languages, rather than combined as they are in Ada. What are the advantages and disadvantages to these two choices?

*

Union separated from records Union combined with records
Advantages -Reduce developer overhead (less checking)

-Goes with the C++ design (C++ is not a weak strong typing language)

-Support type checking

-Goes with the Ada design (Ada is strong typing language)

Disadvantages -No type checking of references

-May end with wrong or unexpected results

-Increase developer overhead

-Using more functions to resolve type compatibility

  1. Multidimensional arrays can be stored in row major order, as in C++, or in column major order, as in Fortran. Develop the access functions for both of these arrangements for three-dimensional arrays.

*Let the subscript ranges of the three dimensions be named min(1), min(2), min(3), max(1), max(2), and max(3). Let the sizes of the subscript ranges be size(1), size(2), and size(3). Assume the element size is 1.

Row Major Order:

location(a[i,j,k]) = (address of a[min(1),min(2),min(3)]) + ((i-min(1))*size(3) + (j-min(2)))*size(2) + (k-min(3))

Column Major Order:

location(a[i,j,k]) = (address of a[min(1),min(2),min(3)]) + ((k-min(3))*size(1) + (j-min(2)))*size(2) + (i-min(1))

Chapter  7:

Review Questions

  1. What associativity rules are used by APL?

*In APL, all operators have the same level of precedence. Thus, the order of evaluation of operators in APL expressions is determined entirely by the associativity rule, which is right to left for all operators.

  1. What is the difference between the way operators are implemented in C++ and Ruby?

*The difference is in Ruby, all the arithmetic, relational, and assignment operator, as well as array indexing, shifts, and bitwise logic operators, are implemented as methods. While in C, they aren’t implemented as methods.

  1. Define functional side effect.

*A functional side effect is a side effect of a function which occurs when the function changes either one of its parameters or a global variable.

  1. What is a coercion?

*A coercion is a process by which a compiler automatically converts a value of one type into a value of another type when that second type is required by the surrounding context.

  1. What is a conditional expression?

*A conditional expression is an expression that returns value A or value B depending on whether a Boolean value is true or false. A conditional expression lets you write a single assignment statement that has the same effect as the following:

if condition:

x = true_value

else:

x = false_value

Problem Set

  1. Should C’s single-operand assignment forms (for example, ++count) be included in other languages (that do not already have them)? Why or why not?

*Yes C should, because those assigning operations make operation simpler and easy to know if that operation is assigning operation. And it will ease the increment or even decrement while we use in looping.

  1. Describe a situation in which the add operator in a programming language would not be commutative.

*If the add operator for a language is also used to concatenate strings, it’s quite apparent that it would not be commutative.

For example:

“abc” + “def” = “abcdef”

“def” + “abc” = “defabc”

These two strings are obviously not equal, so the addition operator is not commutative.

  1. Describe a situation in which the add operator in a programming language would not be associative.

*It is not associative when it includes the other operator with higher precedence like the multiplication and division.

  1. Assume the following rules of associativity and precedence for expressions:

Precedence

Highest           *, /, not

+, –, &, mod

– (unary)

=, /=, <, <=, >=, >

And

Lowest             or, xor

Associativity      Left to right

Show the order of evaluation of the following expressions by parenthesizing all subexpressions and placing a superscript on the right parenthesis to indicate order. For example, for the expression

a + b * c + d

the order of evaluation would be represented as

((a + (b * c)1)2 + d)3

  1. a * b – 1 + c                                ((( a * b )1 – 1)2 + c )3
  2. a * (b – 1) / c mod d                   ((( a * ( b – 1 )1 )2 / c )3 mod d )4
  3. (a – b) / c & (d * e / a – 3)          (((a – b)1 / c)2 & ((d * e)3 / a)4 – 3)5)6
  4. -a or c = d and e                        (( -a )1 or ( ( c = d )2 and e )3 )4
  5. a > b xor c or d <= 17               (((a > b)1 xor c)3 or (d <= 17)2 )4
  6. -a + b                                       (–( a + b )1 )2
  1. Show the order of evaluation of the expressions of Problem 9, assuming that there are no precedence rules and all operators associate right to left.

*

  1. ( a * ( b – ( 1 + c )1 )2 )3
  2. ( a * ( ( b – 1 )2 / ( c mod d )1 )3 )4
  3. ( ( a – b )5 / ( c & ( d * ( e / ( a – 3 )1 )2 )3 )4 )6
  4. ( – ( a or ( c = ( d and e )1 )2 )3 )4
  5. ( a > ( xor ( c or ( d <= 17 )1 )2 )3 )4

f. ( – ( a + b )1 )2

Chapter 8:

Review Questions

  1. What is unusual about Python’s design of compound statements?

*Python uses indentation to specify compound statements. For example,

if x > y :

x = y

print “case 1″

equally indent statements are grouped as one compound statement.

  1. Under what circumstances must an F# selector have an else clause?

*If the expression returns a value, it must have an else clause.

  1. What are the common solutions to the nesting problem for two-way selectors?

*The common solution is to force an alternative semantics, by using compound statements.

  1. What are the design issues for multiple-selection statements?

*-What is the form and type of the control statement?

-How are the selectable segments specified?

-Is execution flow through the structure restricted to include just a single selectable segment?

-How are case values specified?

-What is done about unrepresented expression values?

  1. Between what two language characteristics is a trade-off made when deciding whether more than one selectable segment is executed in one execution of a multiple selection statement?

*In Ada, the choice lists of the case statement must be exhaustive, so that there can be no unrepresented values in the control expression. In C++, unrepresented values can be caught at run time with the default selector. If there is no default, an unrepresented value causes the whole statement to be skipped.

Problem Set

  1. Analyze the potential readability problems with using closure reserved words for control statements that are the reverse of the corresponding initial reserved words, such as the case-esac reserved words of ALGOL 68. For example, consider common typing errors such as transposing characters.

*The potential readability problem is the typing errors. It’s very possible to occur if we don’t type the code carefully.

  1. Use the Science Citation Index to find an article that refers to Knuth (1974). Read the article and Knuth’s paper and write a paper that summarizes both sides of the goto issue.

*An alternative viewpoint is presented in Donald Knuth’s Structured Programming with go to Statements, which analyzes many common programming tasks and finds that in some of them GOTO is the optimal language construct to use.[7] In their quasi-standard book on the C programming language, Dennis Ritchie and Brian Kernighan warn that goto is “infinitely abusable”, but also suggest that it could be used for end-of-function error handlers and for multi-level breaks from loops.

  1. In his paper on the goto issue, Knuth (1974) suggests a loop control statement that allows multiple exits. Read the paper and write an operational semantics description of the statement.

*Operational semantics are a category of formal programming language semantics in which certain desired properties of a program, such as correctness, safety or security, are verified by constructing proofs from logical statements about its execution and procedures, rather than by attaching mathematical meanings to its terms (denotational semantics).

  1. What are the arguments both for and against the exclusive use of Boolean expressions in the control statements in Java (as opposed to also allowing arithmetic expressions, as in C++)?

*The primary argument for using Boolean expressions exclusively as control expressions is the reliability that results from disallowing a wide range of types for this use. In C, for example, an expression of any type can appear as a control expression, so typing errors that result in references to variables of incorrect types are not detected by the compiler as errors. No , it would not be a good idea. Although this custom precedence sounds like increasing flexibility, requiring parentheses to show a custom precedence would impact in readability and writability of a program.

  1. In Ada, the choice lists of the case statement must be exhaustive, so that there can be no unrepresented values in the control expression. In C++, unrepresented values can be caught at run time with the default selector. If there is no default, an unrepresented value causes the whole statement to be skipped. What are the pros and cons of these two designs (Ada and C++)?

*Ada was designed for military grade software development. The idea is that whenever you modify code in such a way that a new case emerges (for example adding a new value for an enumeration type), you are forced to manually revisit (and therefore re-validate) all the case statements that analyze it. Having a “default” is risky: you may forget that there is a case somewhere where the new case should not have been handled by the default.

Chapter 9:

6.What is a Ruby array formal parameter?

Ruby supports a complicated but highly flexible actual parameter configuration. The initial parameters are expressions, whose value objects are passed to the corresponding formal parameters. The initial parameters can be following by a list of key => value pairs, which are placed in an anonymous hash and a reference to that hash is passed to the next formal parameter. These are used as a substitute for keyword parameters, which Ruby does not support.
7.What is a parameter profile? What is a subprogram protocol?
The parameter profile of a subprogram contains the number, order, and types of its formal parameters. The protocol of a subprogram is its parameter profile plus, if it is a function, its return type. In languages in which subprograms have types, those types are defined by the subprogram’s protocol.
8.What are formal parameters? What are actual parameters?
The parameters in the subprogram header are called formal parameters. They are sometimes thought of as dummy variables because they are not variables in the usual sense: In most cases, they are bound to storage only when the subprogram is called, and that binding is often through some other program variables. While actual parameters is a parameter that bound to the formal parameters of the subprogram. Subprogram call statements must include the name of the subprogram and a list of parameters to be bound to the formal parameters of the subprogram.
9.What are the advantages and disadvantages of keyword parameters?
The advantage of keyword parameters is that they can appear in any order in the actual parameter list. The disadvantage to keyword parameters is that the user of the subprogram must know the names of formal parameters.
10.What are the differences between a function and a procedure?

There are two distinct categories of subprograms—procedures and functions— both of which can be viewed as approaches to extending the language. All sub- programs are collections of statements that define parameterized computations. Functions return values and procedures do not. Procedures can produce results in the calling program unit by two methods: (1) If there are variables that are not formal parameters but are still visible in both the procedure and the calling program unit, the procedure can change them; and (2) if the procedure has formal parameters that allow the transfer of data to the caller, those parameters can be changed. However Functions are called by appearances of their names in expressions, along with the required actual parameters. And functions define new user-defined operators.

Problem Sets :

6.Present one argument against providing both static and dynamic local variables in subprograms.
In subprograms local variables can be static or dynamic; If local variable treated statically: This allows for compile-time allocation/ deallocation and ensures proper type checking but does not allow for recursion. And if local variables treated dynamically: This allows for recursion at the cost of run-time allocation/ deallocation and initialization because these are stored on a stack, referencing is indirect based on stack position and possibly timeconsuming

7. Consider the following program written in C syntax:

void fun (int first, int second) {

first += first;

second += second;

}

void main() {

int list[2] = {1, 3};

fun(list[0], list[1]);

}

For each of the following parameter-passing methods, what are the values of the list array after execution?
Passed by value
Passed by reference
Passed by value-result
Passed by value : list[2] = { 3 , 5 }
Passed by reference : list[2] = { 6 , 10 }
Passed by value-result : list[2] = { 6 , 10 }

8.Argue against the C design of providing only function subprograms.
If a language provides only functions, then either programmers must live with the restriction of returning only a single result from any subprogram, or functions must allow side effects, which is generally considered bad. Since having subprograms that can only modify a single value is too restrictive, C’s choice is not good.
9.From a textbook on Fortran, learn the syntax and semantics of statement functions. Justify their existence in Fortran.
The Fortran 1966 standard provided a reference syntax and semantics, but vendors continued to provide incompatible extensions. These standards have improved portability.
10.Study the methods of user-defined operator overloading in C++ and Ada, and write a report comparing the two using our criteria for evaluating languages.
One of the nice features of C++ is that you can give special meanings to operators, when they are used with user-defined classes. This is called operator overloading. You can implement C++ operator overloads by providing special member-functions on your classes that follow a particular naming convention. For example, to overload the + operator for your class, you would provide a member-function named operator+ on your class. Meanwhile for Ada, since much of the power of the language comes from its extensibility, and since proper use of that extensibility requires that we make as little distinction as possible between predefined and user-defined types, it is natural that Ada also permits new operations to be defined, by declaring new overloading of the operator symbols.
Chapter 10:

6. What is the difference between an activation record and an activation record instance?

*An activation record is the format, or layout, of the moncode part of a subprogram. An activation record instance is a concrete example of an activation record, a collection of data in the form of an activation record.7. Why are the return address, dynamic link, and parameters placed in the bottom of the activation record?

*It’s because the entry must appear first.8. What kind of machines often use registers to pass parameters?

*RISC Machines often use registers to pass parameters.9. What are the two steps in locating a nonlocal variable in a static-scoped language with stack-dynamic local variables and nested subprograms?

*First step, find correct activation record (the harder part) and then the second step is determine the offset within that activation record (easy part).10. Define static chain, static_depth, nesting_depth, and chain_offset.

*Static chain is chain of static links connecting an activation record to all of it’s static ancestors (it’s enclosing subprograms).
Static depth is depth of the nesting for each enclosing static scope.
Nesting depth is the difference between the static depth of the reference and that of the scope where it was declared.
Chain offset is same as nesting depth.

Problem Set :

6. Although local variables in Java methods are dynamically allocated at the beginning of each activation, under what circumstances could the value of a local variable in a particular activation retain the value of the previous activation?
*Each activation allocates variables in exactly the same order. Variables are not initialized to any value unless the program contains an initialization statement for the variable – they simply have whatever value is stored in the location they are allocated. If a procedure finishes executing, returns, and is immediately reinvoked, a variable would be assigned the same stack location it had on the previous invocation, and would have the last value from that previous invocation.7. It is stated in this chapter that when nonlocal variables are accessed in a dynamic-scoped language using the dynamic chain, variable names must be stored in the activation records with the values. If this were actually done, every nonlocal access would require a sequence of costly string comparisons on names. Design an alternative to these string comparisons that would be faster.

*Using approach that uses an auxiliary data structure called a display. Or, to write variable names as integers. These integers act like an array. So when the activation happens, the comparisons will be faster.8. Pascal allows gotos with nonlocal targets. How could such statements be handled if static chains were used for nonlocal variable access? Hint: Consider the way the correct activation record instance of the static parent of a newly enacted procedure is found (see Section 10.4.2).

*Based on the hint statement, the target of every goto in a program could be represented as an address and a nesting depth, where the nesting depth is the difference between the nesting level of the procedure that contains the goto and that of the procedure containing the target. Then, when a goto is executed, the static chain is followed by the number of links indicated in the nesting depth of the goto target. The stack top pointer is reset to the top of the activation record at the end of the chain.9. The static-chain method could be expanded slightly by using two static links in each activation record instance where the second points to the static grandparent activation record instance. How would this approach affect the time required for subprogram linkage and nonlocal references?

*Including two static links would reduce the access time to nonlocals that are defined in scopes two steps away to be equal to that for nonlocals that are one step away. Overall, because most nonlocal references are relatively close, this could significantly increase the execution efficiency of many programs.10. Design a skeletal program and a calling sequence that results in an activation record instance in which the static and dynamic links point to different activation-recorded instances in the run-time stack.

*\\
\emph{Answer}:\\
procedure Main\_2 is\\
\verb+ + X : Integer;\\
\verb+ +procedure Bigsub is\\
\verb+ +\verb+ + A, B, C : Integer;\\
\verb+ +\verb+ + procedure Sub1 is\\
\verb+ +\verb+ +\verb+ + A, D : Integer;\\
\verb+ +\verb+ +\verb+ + begin — of Sub1\\
\verb+ +\verb+ +\verb+ + A := B + C; $\longleftarrow$ 1\\
\verb+ +\verb+ +\verb+ + …\\
\verb+ + end; — of Sub1\\
\verb+ + procedure Sub2(X : Integer) is\\
\verb+ +\verb+ + B, E : Integer;\\
\verb+ +\verb+ + procedure Sub3 is\\
\verb+ +\verb+ +\verb+ + C, E : Integer;\\
\verb+ +\verb+ +\verb+ + begin — of Sub3\\
\verb+ +\verb+ +\verb+ + …\\
\verb+ +\verb+ +\verb+ + Sub1;\\
\verb+ +\verb+ +\verb+ + …\\
\verb+ +\verb+ +\verb+ + E := B + A; $\longleftarrow$ 2\\
\verb+ +\verb+ + end; — of Sub3\\
\verb+ +\verb+ + begin — of Sub2\\
\verb+ +\verb+ + …\\
\verb+ +\verb+ + Sub3;\\
\verb+ +\verb+ + …\\
\verb+ +\verb+ + A := D + E; $\longleftarrow$ 3\\
\verb+ + end; — of Sub2\\
\verb+ + begin — of Bigsub\\
\verb+ +\verb+ + …\\
\verb+ +\verb+ + Sub2(7);\\
\verb+ +\verb+ + …\\
\verb+ + end; — of Bigsub\\
begin — of Main\_2\\
\verb+ + …\\
\verb+ + Bigsub;\\
\verb+ + …\\
end; — of Main\_2\\
\\
The sequence of procedure calls is:\\
Main\_2 calls Bigsub\\
Bigsub calls Sub2\\
Sub2 calls Sub3\\
Sub3 calls Sub1\\
\\
The activation records with static and dynamic links is as follows:\\
\begin{figure}
\centering
\includegraphics[scale=0.5]{ari}
\end{figure}At position 1 in procedure Sub1, the reference is to the local variable,
A, not to the nonlocal variable A from Bigsub. This reference to A has the
chain\_offset/local\_offset pair (0, 3). The reference to B is to the nonlocal B
from Bigsub. It can be represented by the pair (1, 4). The local\_offset is 4,
because a 3 offset would be the first local variable (Bigsub has no parameters). Notice that if the dynamic link were used to do a simple search for
an activation record instance with a declaration for the variable B, it would
find the variable B declared in Sub2, which would be incorrect. If the (1, 4)
pair were used with the dynamic chain, the variable E from Sub3 would be
used. The static link, however, points to the activation record for Bigsub,
which has the correct version of B . The variable B in Sub2 is not in the
referencing environment at this point and is (correctly) not accessible. The
reference to C at point 1 is to the C defined in Bigsub, which is represented
by the pair (1, 5).\\
\\

\noindent

Chapter 11:

Review Questions :

6. Explain how information hiding is provided in an Ada package.

There are two approaches to hiding the representation from clients in the package specification. The first one is to include two sections in the package specification, the second one is in which entities are visible to clients and one that hides its contents.

7. To what is the private part of an Ada package specification visible?

The private part of an Ada package specification is visible to the compiler but not to the client programs.8. What is the difference between private and limited private types in Ada?

Private types have built-in operations for assignment and comparison. Limited private types don’t have built-in operations.9. What is in an Ada package specification? What about a body package?

Package specification, is an Ada package which provides the interface of the encapsulation (and perhaps more). Body package, is an Ada package which provides the implementation of most, if not all, of the entities named in the associated package specification.10. What is the use of the Ada with clause?

The use of Ada with clause is to make the names defined in external packages visible.Problem Set:

6. Discuss the advantages of C# properties, relative to writing accessor methods in C++ or Java.
One of the advantage of C# properties relative writing accessor methods in C++ or Java is it can control access to the fields.

7. Explain the dangers of C’s approach to encapsulation.

The main problem is that the biggest part of encapsulation is done via hidding, rather than protection. This hidding is achieved through definition hidding: a header file is preprocessed (which is a synonym for copy-pasted) into the implementation file. Anyone with this header file will be able to access any method or public variable of a the client related to the header, left appart any “static” method / variable. Static is actually the only rue level of protection here, as it’s the only one that another unit (file) would not be able to access even if it’s aware of it’s existence. This whole “protection-is-name-hiding” approach leads to a load of problems: you can access a symbol using the wrong datatype and your compiler will happily do so. To protect critical parts, you should rely on text being hidden from the compiler while it’s processing certain units (via #ifdef / #define).8. Why didn’t C++ eliminate the problems discussed in Problem 7?

It didn’t eliminate the problem because it evolved from C. Hence, it kept a lot of backward compatibility, and the same way of doing basic things. While some problems where solved (like the protected access, which is in-between normal and static in C), some stay, as the symbol access using wrong datatype (inherent to the linker, which doesn’t do type-checking).9. What are the advantages and disadvantages of the Objective-C approach to syntactically distinguishing class methods from instance methods?

Instance methods use an instance of a class, whereas a class method can be used with just the class name. A class is like the blueprint of a house: You only have one blueprint and (usually) you can’t do that much with the blueprint alone. An instance (or an object) is the actual house that you build based on the blueprint: You can build lots of houses from the same blueprint. You can then paint the walls a different color in each of the houses, just as you can independently change the properties of each instance of a class without affecting the other instances.10. In what ways are the method calls in C++ more or less readable than those of Objective-C?

In Objective C, all method calls are essentially virtual. This makes it a bit easier on the programmer, but it comes at a possible performance decrease. So sometimes methods call in C++ can be more or less readable than those of Objective-C.
Chapter 12:

Review Questions :

6. Describe a situation where dynamic binding is a great advantage over its absence.

There is a base class, A, that defines a method draw that draws some figure associated with the base class. A second class, B, is defined as a subclass of A. Objects of this new class also need a draw method that is like that provided by A but a bit different. With overriding, we can directly modify B’s draw function. But without it, we either make a specific function in A for B and inherit it.7. What is a virtual method?

A virtual method is a declared class method that allows overriding by a method with the same derived class signature. Virtual methods are tools used to implement the polymorphism feature of an object-oriented language, such as C#. When a virtual object instance method is invoked, the method to be called is determined based on the object’s runtime type, which is usually that of the most derived class.8. What is an abstract method? What is an abstract class?

An abstract method is a method which all descendant classes should have. An abstract class is a class which has abstract method.9. Describe briefly the eight design issues used in this chapter for object-oriented languages.

-What non-objects are in the language?
-Are Subclasses Subtypes? If so, derived objects can be legally used wherever a parent object could be used.
-Type Checking and Polymorphism
-Single and Multiple Inheritance. Inherit from 1 (or more than 1) parent.
-Object Allocation and Deallocation. Are objects allocated from heap or stack.
-Dynamic and Static Binding. When are messages bound to methods, before or during run-time?
-Nested Classes. Can a class be nested inside another class?
-Initialization of Objects. Are objs init’d when created? Implicit or explicit?10. What is a nesting class?

A nesting class is a class defined inside another class.Problem Set:

6. Compare the multiple inheritance of C++ with that provided by interfaces in Java.

C++ inheritance is implementation inheritance. That is, a class inheriting from two of more superclasses actually inherits the code from those classes. Java’s interface mechanism is an interface inheritance. That is, a class implementing two or more interfaces simply inherits (and must provide its own implementations for) the methods of the interface.7. What is one programming situation where multiple inheritance has a significant advantage over interfaces?

When two or more parent classes are derived from one grandparent class and has one child (diamond problem).8. Explain the two problems with abstract data types that are ameliorated by inheritance.

The problems solved are reusability of code and “extensibility”. Reusability because one won’t have to copy/paste his code from one data type to another, allowing for a greater readability. Extensibility because a method can accept a certain class as an argument, and get a child class of this one. This will allow the user to have a wider set of functionality, but the method will still be able to know that the entities it relies on are present.

9. Describe the categories of changes that a subclass can make to its parent class.

Subclasses can add things (variables, methods). Subclass in C++ can effectively remove a method using “private” inheritance. Inherited methods can be overridden.

10. Explain one disadvantage of inheritance.

Language & implementation complexity. The shared inheritance problem of multiple inheritance. Subclass is dependent upon its base class (which might change over time).

Posted in Uncategorized | Leave a comment

Hello world!

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

Posted in Uncategorized | 1 Comment