1)Suppose someone designed a stack abstract data type in which the func- tion to
ID: 3738056 • Letter: 1
Question
1)Suppose someone designed a stack abstract data type in which the func- tion 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?
2)Describe a situation where a C# struct is preferable to a C# class?
3Explain the two problems with abstract data types that are ameliorated by inheritance?
4)What exactly does it mean for a subclass to have an is-a relationship with its parent class?
Explanation / Answer
1)
Ans) 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;
3)
ANS;-
Two problems of abstract data types that are ameliorated by inheritance are:
1. The first problem is Reuse of abstract data types is much easier because the modifications of existing types need not be done on the legacy code, and
2. The second problem is with abstract data types all types are independent and at the same level, disallowing any logically hierarchical type dependencies.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.