Harmony Clean Flat Responsive WordPress Blog Theme

Assignment 11

Review Questions:
1. Discuss the advantages of C# properties, relative to writing accessor methods in C++ or Java.
  • The advantage of C# Properties relative to writing accessor are:
Read-only access can be provided, by having a getter method but no corresponding setter method.
Constraints can be included in setters. For example, if the data value should be restricted to a particular range, the setter can enforce that.
The actual implementation of the data member can be changed without affecting the clients if getters and setters are the only access.
2. Explain the dangers of C’s approach to encapsulation.
  • There are two problems with this approach. First, the documentation of the dependence of the client program on the library (and its header file) is lost. Second, the author of the library could change the header file and the implementation file, but the client could attempt to use the new implementation file (not realizing it had changed) but with the old header file, which the user had copied into his or her client program.
3. Why didn’t C++ eliminate the problems discussed in Problem 7?
  • Because in C++ these kinds of problems can be handled by allowing nonmember functions to be “friends” of a class. Friend functions have access to the private entities of the class where they are declared to be friends.
4. 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.
5. In what ways are the method calls in C++ more or less readable than those of Objective-C?
  • Objective-C uses the directives, @private and @public, to specify the access levels of the instance variables in a class definition. These are used as the reserved words public and private are used in C++. The difference is that the default in Objective-C is protected, whereas it is private in C++.
Problem Sets:
  1. Explain why naming encapsulations are important for developing large programs.
  • is to provide a way to organize programs into logical units for compilation. This allows parts of programs to be recompiled after isolated changes. There is another kind of encapsulation that is necessary for constructing large programs.
prevalent in any object that is created in combination
used to hide the values or state of a structured data object inside a class, preventing unauthorized parties direct access to them.
Each library can create its own naming encapsulation to prevent its names from conflicting with the names defined in other libraries or in client code. Each logical part of a software system can create a naming encapsulation with the same purpose.
Several different collections of code can be placed in the same namespace, even though they are stored in different places.
  1. Describe the three ways a client can reference a name from a namespace in C++.
  • Client code can gain access to the names in the namespace of the header file of a library in three different ways. One way is to qualify the names from the library with the name of the namespace. For example, a reference to the variable topSub could appear as follows:
myStackSpace::topSub
This is exactly the way the implementation code could reference it if the implementation file was not in the same namespace.
The other two approaches use the using directive. This directive can be used to qualify individual names from a namespace, as with
using myStackSpace::topSub;
which makes topSub visible, but not any other names from the myStackSpace namespace.
The using directive can also be used to qualify all of the names from a namespace, as in the following:
using namespace myStackSpace;
Code that includes this directive can directly access the names defined in the
namespace, as in
p = topSub;
  1. The namespace of the C# standard library, System, is not implicitly available to C# programs. Do you think this is a good idea? Defend your answer.
  • I think it is not, because it reduces its efficiency.
  1. What are the advantages and disadvantages of the ability to change objects in Ruby?
  • Classes in Ruby are dynamic in the sense that members can be added at any time. This is done by simply including additional class definitions that specify the new members. Moreover, even predefined classes of the language, such as String, can be extended. Methods can also be removed from a class. This is done by providing another class definition in which the method to be removed is sent to the method remove_method as a parameter. The dynamic classes of Ruby are another example of a language designer trading readability (and as a conse- quence, reliability for flexibility. Allowing dynamic changes to classes clearly adds flexibility to the language, while harming readability. 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.
5. Compare Java’s packages with Ruby’s modules.
  • In java encapsulation construct called package .Packages can contain more than one type definition, and the types in a package are partial friends of one another.In ruby a encapsulation construct called module. Modules typically define collections of methods and constants. So, modules are convenient for encapsulating libraries of related methods and constants, whose names are in a separate namespace so there are no name conflicts with other names in a program that uses the module. Modules are unlike classes in that they cannot be instantiated or subclassed and do not define variables.