suisied
suisied

Reputation: 426

Objective-C overriding a method with 2 arguments

Class B.h

@interface ClassB : rndvzViewController {
    int sum, num1, num2;
}

-(void)sumPrint;
-(int)addNum1:(int)_num1 andNum2:(int)_num2;

Class B.m

-(int)addNum1:(int)_num1 andNum2:(int)_num2
{
    num1 = _num1;
    num2 = _num2;

    return sum = num1 + num2;
}

-(void)sumPrint
{
    NSLog(@"%d", sum);
}

class C.h

@interface ClassC : ClassB

@end

Class C.m

-(int)addNum1:(int)_num1 andNum2:(int)_num2
{

    //[self addNum1:num1 andNum2:num2];
    int interest = sum*.05;

    return interest;
}

Calling the methods :

ClassB* classB = [[ClassB alloc] init]; 
ClassC* classC = [[ClassC alloc] init];

[classB addNum1:12 andNum2:12];
classB.sumPrint;
//[classC addNum1:12 andNum2:12];
classC.sumPrint;

Output : 2014-09-09 14:07:48.919 ClassA[38418:60b] 24 2014-09-09 14:07:48.919 ClassA[38418:60b] 0

What am I doing wrong, why is classC.sumPrint returning 0, I want class C to print the sum from the values of class B * interest.

Upvotes: 0

Views: 63

Answers (1)

Fogmeister
Fogmeister

Reputation: 77641

This is very basic OOP stuff. Not really Obj-C.

Anyway, if you want the addNum1:andNum2: method in C to first run the method in B then create it like this...

- (int)addNum1:(int)_num1 andNum2:(int)_num2
{
    return [super addNum1:_num1 andNum2:_num2] * 0.5;
}

It's a bit clunky though and the naming convention is off and I wouldn't store the data that way and many other things. But this will do what you want.

Upvotes: 3

Related Questions