Extending on the previous challenge with Inheritance!


#1

[code]
//main.m
NSMutableArray *stockArray=[NSMutableArray array];

    for(int i=1;i<5;i++)
    {
        
        StockHolding *s=[[StockHolding alloc] init];
        s.purchaseSharePrice=2.3*i;
        s.currentSharePrice=5.8*i;
        s.numberOfShares=54*i;
        
        [stockArray addObject:s];
        
        
        NSLog(@"The value of %i stock is $%0.2f and it's cost is $%0.2f",
              i, [[stockArray objectAtIndex:i-1] valueInDollars],
              [[stockArray objectAtIndex:i-1] costInDollars]
              );
        
        
        
        if(i==5){
            
            ForeignStockHolding *f=[[ForeignStockHolding alloc] init] ;
            f.purchaseSharePrice=2.3;
            f.currentSharePrice=5.8;
            f.numberOfShares=54;
            f.conversionRate=0.94;
            
            [stockArray addObject:f];
            
            NSLog(@"The value of %i stock is $%0.2f and it's cost is $%0.2f",
                  i, [[stockArray objectAtIndex:i-1] valueInDollars],
                  [[stockArray objectAtIndex:i-1] costInDollars]
                  );

        }

//ForeignStockHolding.m//////////////////////////////////

#import “ForeignStockHolding.h”

@implementation ForeignStockHolding
@synthesize conversionRate;

-(float)costInDollars{
float value=[super costInDollars];
return value*conversionRate;
}

-(float)valueInDollars{
float value=[super valueInDollars];
return value*conversionRate;
}

@end

//ForeignStockHolding.h //////////////////////////////////

#import <Foundation/Foundation.h>
#import “StockHolding.h”

@interface ForeignStockHolding : StockHolding
{
float conversionRate;
}

@property float conversionRate;

@end

    }[/code]

StockHolding.h and StockHolding.m
stays the same as in the previous challenge!