Concepts of Programming Languages

(Sean Pound) #1
11.4 Language Examples 495

}


-(int) empty {
return topSub == -1);
}


int main (int argc, char argv[]) {
int temp;
NSAutoreleasePool
pool = [[NSAutoreleasePool alloc]
init];
Stack *myStack = [[Stack alloc]initWith];
[myStack push: 5];
[myStack push: 3];
temp = [myStack top];
NSLog(@"Top element is:%i", temp);
[myStack pop];
temp = [myStack top];
NSLog(@"Top element is:%i", temp);
temp = [myStack top];
[myStack pop];
[myStack release];
[pool drain];
return 0;
}
@end


The output of this program is as follows:


Top element is: 3
Top element is: 5
Error in top--stack is empty
Error in pop--stack is empty


Screen output from an Objective-C program is created with a call to a
method with the odd-looking name, NSLog, which takes a literal string as its
parameter. Literal strings are created with an at sign (@) followed by a quoted
string. If an output string includes the values of variables, the names of the
variables are included as parameters in the call to NSLog. The positions in the
literal string for the values are marked with format codes, for example %i for
an integer and %f for a floating-point value in scientific notation, as is similar
to Cā€™s printf function.


11.4.3.4 Evaluation


The support in Objective-C for abstract data types is adequate. Some find
it odd that it uses syntactic forms from two very different languages, Small-
talk (for its method calls) and C (for nearly everything else). Also, its use of

Free download pdf