🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.objective-c
3 messages
3 total messages Started by W Jamison Sun, 11 Oct 1992 17:02
method inheritance in NeXT ObjC classes
#199
Author: W Jamison
Date: Sun, 11 Oct 1992 17:02
90 lines
1952 bytes
I have been trying to define a class called Stack that is a subclass 
of the NeXT Common Class List.  When I compile and run the program,
I receive the following error message:

	error: Stack does not recognize selector +addObject: IOT trap

Am I wrong in thinking that Stack should recognize the methods
for its superclass List?

I have included the @interface and @implementation files for the
subclass Stack I have written below.  Can anyone tell me what I
have overlooked?  

					Wes Jamison
					University of Pittsburgh
						at Greensburg
// file Stack.h
/* Interface File */

#import <objc/List.h>
#import "Integer.h"

@interface  Stack: List
{
	/* no new instance variables for subclass */
}

/* factory methods for class */

+new;

/* instance methods for class */

-push: (int)input;
-(int) pop;
-display;

@end

/* Implementation File */

#import <stdio.h>

@implementation  Stack:List

/* Details of factory methods  */

+new
{
	self = [super new];
	return self;
}
	
/* Details of instance methods */

-push: (int)input
{
	id myStack;

	[myStack addObject:[[Integer new] set: input]];
	return self;
}

-(int)pop
{
	int output;
	id myStack, myInteger;
	
	myInteger = [myStack removeLastObject];	
	output = [myInteger show];
	[myInteger free];	            
        return output;
}

-display
{
	id myInteger, myStack;
	int i;
	
	
	for (i = 0; i <= [myStack count]; i++)
		{
		myInteger = [myStack objectAt: i];
		printf("%d\n",[myInteger show]);
		}
		printf("\n");	
}	
@end

	
Re: method inheritance in NeXT ObjC classes
#200
Author: Vince Demarco
Date: Sun, 11 Oct 1992 19:37
12 lines
822 bytes
The problem is in the push and pop, you do a [myStack addObject:etc]
it should shave

[self addObject: [[Integer new] set: input];
etc


Also in your code don't use the new methods anymore, use alloc and
init.

Hope this helps
Re: method inheritance in NeXT ObjC classes
#201
Author: Andy Belk
Date: Mon, 12 Oct 1992 10:56
97 lines
2848 bytes
Hi all.
        Just as an aside, this (List->Stack) is a fairly good example of
when subclassing is not necessarily what is really desired.

A Stack usually has push and pop operations, and in this case also a display
method, but one can argue that doing List operations on a stack may cause
problems. For example, one can do things that mess up the LIFO idea of a
stack:

Stack s;
Integer i = 5;
[s init];
[s push: i--];
[s push: i--];
[s push: i--];
[s push: i--];
[s push: i--];
[s removeObjectAt: 5];   // Ok, you can do it, but should you be able to ?
[[s pop] display];

etc.


Here's my version of Stack.h and Stack.m (yes, I did the ObjectiveC example
too, and I don't have Queue or Stack classes either - but I do have access to
a NeXT and its List class ...):

--------------------------- Stack.m -------------------------------

#import <objc/Object.h>
#import <objc/List.h>

@interface Stack : Object
{
  List *sList;
}

+ new;              // Probably should use init and alloc now
- free;
- get;              // returns last object on stack (and removes it)
- put: anObject;    // Store this object on the top of the stack
- (unsigned)size;   // current stack size

@end;

--------------------------- Stack.m -------------------------------

// Use a list with index to make a stack and a queue
// Modified 30th September to use a List rather than inheriting from one -
// Stacks should not look like lists, IMHO. This is an example of delegation
// as compared to inheritance.

#import <stdio.h>
#import "Stack.h"

@implementation Stack: Object

+ new
{
  self = [super new];  // It's VITALLY important to create an object
  sList = [List new];  // before playing with its instance variables !
  [sList init];
  return self;         // MUST return self here or instance will be wrong class
}

// The "-" in front of report means that it's an instance method,
// i.e., how a particular object should respond.

- free
{
  [sList free];         // Note the List object gets freed by me
  return [self free];
}

- put: anObject
{
  [sList addObject: anObject];
  return self;
}

- get
{
  id anObject = [sList lastObject];
  [sList removeLastObject];
  return anObject;
}

- (unsigned)size
{
  return [sList count];
}

@end

------------------------------------------------------------------------
-- 
					    ...   Anyd   ...
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads