1. Describe the three ways a client can reference a name from a namespace in C++
ID: 3549167 • Letter: 1
Question
1. Describe the three ways a client can reference a name from a namespace in C++.
2. Suppose someone designed a stack abstract data type in which the function top returned an access path (or pointer) rather than returning a copy of the top element. This is not a true data abstraction. Why? Give an example that illustrates the problem.
3. What are the advantages and disadvantages of the ability to change objects in Ruby?
4. What are the disadvantages of designing an abstract data type to be a pointer?
5. Explain the dangers of C
Explanation / Answer
1.Only one entity can exist with a particular name in a particular scope. This is seldom a problem for local names, since blocks tend to be relatively short, and names have particular purposes within them, such as naming a counter variable, an argument, etc...
But non-local names bring more possibilities for name collision, especially considering that libraries may declare many functions, types, and variables, neither of them local in nature, and some of them very generic.
Namespaces allow to group named entities that otherwise would have global scope into narrower scopes, giving them namespace scope. This allows organizing the elements of programs into different logical scopes referred to by names.
The syntax to declare a namespaces is:
Where identifier is any valid identifier and named_entities is the set of variables, types and functions that are included within the namespace. For example:
In this case, the variables a and b are normal variables declared within a namespace called myNamespace.
These variables can be accessed from within their namespace normally, with their identifier (either a or b), but if accessed from outside the myNamespace namespace they have to be properly qualified with the scope operator ::. For example, to access the previous variables from outside myNamespace they should be qualified like:
Namespaces are particularly useful to avoid name collisions. For example:
In this case, there are two functions with the same name: value. One is defined within the namespace foo, and the other one in bar. No redefinition errors happen thanks to namespaces. Notice also how pi is accessed in an unqualified manner from within namespace bar (just as pi), while it is again accessed in main, but here it needs to be qualified as second::pi.
Namespaces can be split: Two segments of a code can be declared in the same namespace:
This declares three variables: a and c are in namespace foo, while b is in namespace bar. Namespaces can even extend across different translation units (i.e., across different files of source code).
2.This design is not data abstraction. Since the function, top, returned an access path (or
pointer) and by this the main program is able to change the top element of the stack since it can
refer to the element address. So, nothing can prevent the main program from then changing the top
element of the stack, for example:
Stack myStack = new Stack (10);
int x = 5;
ptr = *int;
ptr = myStack.top;
*ptr = 20;
Ruby is touted as having several important advantages over Java and C. The first major advantage is ease of use. The syntax is economical and concise, meaning that Ruby can accomplish more with less verbiage than in Java or C. For example it uses limited punctuation and simple naming conventions. The second major advantage is something known as 'Dynamic Typing'. Java, by comparison uses 'Static Typing', which means that the data type of each variable has to be precisely specified before the program runs. This makes it much less flexible than Ruby.
The Ruby language has additional features which indicate that in some areas it is more advanced than Java or C. Its strength lies in something known as 'Meta-Programming'. This is the ability to write computer programs that write or manipulate other programs. These abilities mean that Ruby could have important applications in the field of artificial intelligence. Reflection, or the ability of a program to reason about itself, is important for artificial intelligence research and Ruby does this very well.
For instance Ruby can change class definitions dynamically (whilst a program is running). Classes are the abstract definition of objects. In Java these definitions have to be fixed before the program is run and cannot be changed. As another example, data (input into programs) can be interpreted as code (the programming language itself).
Another aspect of Ruby which may make it very attractive for those working in artificial intelligence is that it has a 'LISP-like' syntax and some features which are similar to LISP. LISP is the second oldest programming language. Because of its mathematical notation it became the favored language for AI research. LISP was one of Matsumoto
namespace identifier { named_entities } Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.