Chapter 18:Inheritance(Challenge Solution)


#1

My solution for Challenge Question (Inheritance). Please leave your opinions and comments regarding my coding. Any advices and critics regarding the coding are welcome! :wink:

Thank you!

(main.m)

[code]#import “StockHolding.h”
#import “ForeignStockHolding.h”
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
//Below are instances of StockHolding in local currency(Ringgit)
StockHolding *stock1 = [[StockHolding alloc]init];
StockHolding *stock2 = [[StockHolding alloc]init];
StockHolding *stock3 = [[StockHolding alloc]init];

    [stock1 setPurchaseSharePrice:2.30];
    [stock1 setCurrentSharePrice:4.50];
    [stock1 setNumberOfShares:40];
    
    [stock2 setPurchaseSharePrice:12.19];
    [stock2 setCurrentSharePrice:10.56];
    [stock2 setNumberOfShares:90];
    
    [stock3 setPurchaseSharePrice:45.10];
    [stock3 setCurrentSharePrice:49.51];
    [stock3 setNumberOfShares:210];
    
    NSArray *arrayOfShares = [NSArray arrayWithObjects:stock1, stock2, stock3, nil];
    for (StockHolding *a in arrayOfShares) {
        NSLog(@"In local currency.");
        NSLog(@"The cost is %.2f Ringgit while the value is %.2f Ringgit.", [a costInDollars], [a valueInDollars]); }
    
    //Below are 3 instances of ForeignStockHolding.
    //They are the stock1/stock2/stock3 in USD instead of Ringgit (MYR currency)
    ForeignStockHolding *usd_stock1 = [[ForeignStockHolding alloc]init];
    ForeignStockHolding *usd_stock2 = [[ForeignStockHolding alloc]init];
    ForeignStockHolding *usd_stock3 = [[ForeignStockHolding alloc]init];
    
        [usd_stock1 setPurchaseSharePrice:2.30]; //Purchase share price in local currency
        [usd_stock1 setCurrentSharePrice:4.50]; //Current Share Price in local currency
        [usd_stock1 setNumberOfShares:40]; //same number of shares as shown in the above in stock1
        [usd_stock1 setConversionRate:3.00]; //conversion rate from Ringgit (MYR local currency) to USD id 3.00
        
        [usd_stock2 setPurchaseSharePrice:12.19];
        [usd_stock2 setCurrentSharePrice:10.56];
        [usd_stock2 setNumberOfShares:90];
        [usd_stock2 setConversionRate:3.00];
        
        [usd_stock3 setPurchaseSharePrice:45.10];
        [usd_stock3 setConversionRate:49.51];
        [usd_stock3 setNumberOfShares:210];
        [usd_stock3 setConversionRate:3.00];
        
       NSArray *arrayOfShares_USD = [NSArray arrayWithObjects:usd_stock1, usd_stock2, usd_stock3, nil];
        for (ForeignStockHolding *b in arrayOfShares_USD) {
            NSLog(@"In USD currency.");
            NSLog(@"The cost is %.2f USD while the value is %.2f USD", [b costInDollars], [b valueInDollars]);
        }
    return 0;
}

}[/code]

(ForeignStockHolding.h)

[code]#import “StockHolding.h”

@interface ForeignStockHolding : StockHolding
{
float conversionRate;
}

@property float conversionRate;

@end[/code]

(ForeignStockHolding.m)

[code]#import “ForeignStockHolding.h”

@implementation ForeignStockHolding

@synthesize conversionRate;

-(float)costInDollars
{
float costInRinggit = [super costInDollars];
return costInRinggit * conversionRate;
}
-(float)valueInDollars
{
float valueInRinggit = [super valueInDollars];
return valueInRinggit * conversionRate;
}

@end[/code]

(StockHolding.h)

[code]#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;
}

@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

-(float)costInDollars;
-(float)valueInDollars;

@end[/code]

(StockHolding.m)

[code]#import “StockHolding.h”

@implementation StockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

-(float)costInDollars
{
return purchaseSharePrice * numberOfShares;
}
-(float)valueInDollars
{
return currentSharePrice * numberOfShares;
}

@end[/code]

Result:
2014-03-09 20:38:04.075 Stocks[750:303] In local currency.
2014-03-09 20:38:04.076 Stocks[750:303] The cost is 92.00 Ringgit while the value is 180.00 Ringgit.
2014-03-09 20:38:04.077 Stocks[750:303] In local currency.
2014-03-09 20:38:04.077 Stocks[750:303] The cost is 1097.10 Ringgit while the value is 950.40 Ringgit.
2014-03-09 20:38:04.077 Stocks[750:303] In local currency.
2014-03-09 20:38:04.078 Stocks[750:303] The cost is 9471.00 Ringgit while the value is 10397.10 Ringgit.
2014-03-09 20:38:04.078 Stocks[750:303] In USD currency.
2014-03-09 20:38:04.079 Stocks[750:303] The cost is 276.00 USD while the value is 540.00 USD
2014-03-09 20:38:04.079 Stocks[750:303] In USD currency.
2014-03-09 20:38:04.079 Stocks[750:303] The cost is 3291.30 USD while the value is 2851.20 USD
2014-03-09 20:38:04.080 Stocks[750:303] In USD currency.
2014-03-09 20:38:04.080 Stocks[750:303] The cost is 28413.00 USD while the value is 0.00 USD
Program ended with exit code: 0


#2

kohalex9, in your main.m why did you repeat the same PurchaseSharePrice, CurrentSharePrice, and NumberOfShares for all of your USD and Ringgit currency? I am new to programming but I feel as if this is very redundant…

[usd_stock1 setPurchaseSharePrice:2.30]; //Purchase share price in local currency
[usd_stock1 setCurrentSharePrice:4.50]; //Current Share Price in local curre
[usd_stock1 setNumberOfShares:40]; //same number of shares as shown in the above in stock1
[usd_stock1 setConversionRate:3.00]; //conversion rate from Ringgit (MYR local currency) to USD id 3.00

#3

Hi that my solution

main.m

[code]#import <Foundation/Foundation.h>
#import “Action.h”
#import “ActionEtrangere.h”

int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    //on definit nos instances et on set les valeurs
    Action *action1 = [[Action alloc] init];
    [action1 setNomActions:@"Action 0"];
    [action1 setPrixAchatAction:2.30];
    [action1 setPrixActuelAction:4.50];
    [action1 setNbActions:40];
    
    Action *action2 = [[Action alloc] init];
    [action2 setNomActions:@"Action 1"];
    [action2 setPrixAchatAction:12.19];
    [action2 setPrixActuelAction:10.56];
    [action2 setNbActions:90];
    
    ActionEtrangere *action3 = [[ActionEtrangere alloc] init];
    [action3 setNomActions:@"Action Etrangere"];
    [action3 setPrixAchatAction:45.10];
    [action3 setPrixActuelAction:49.51];
    [action3 setNbActions:210];
    [action3 setTauxConversion:0.94];
    
    
    //creer le tableau avec nos objets
    
    NSMutableArray *tab = [NSMutableArray arrayWithObjects:action1, action2, action3, nil];
  
    // on verifie combien de valeur contient notre tableau
  /*  NSUInteger nbTab = [tab count];
    NSLog(@"Notre tableau possede %lu cellules", (unsigned long)nbTab);*/
 
    // c'est partie pour la lecture du tableau
    for (Action *d in tab) {
        NSLog(@"%@: le cout euros est de %.2f et la valeur en euros est de %.2f",[d nomActions],[d coutEnEuros], [d valeurEnEuros]); //on affiche nos resultats
        
    }
    
}
return 0;

}
[/code]

ActionEtrangere.h

[code]#import <Foundation/Foundation.h>
#import “Action.h”

@interface ActionEtrangere : Action {
float tauxConversion;
}
@property float tauxConversion;

  • (float)coutEnEuros;
  • (float)valeurEnEuros;

@end
[/code]

and ActionEtrangere.m

[code]#import “ActionEtrangere.h”

@implementation ActionEtrangere
@synthesize tauxConversion;

  • (float)coutEnEuros {

    return [super prixAchatAction] * [super nbActions] * tauxConversion;

    }

  • (float)valeurEnEuros {

    return [super prixActuelAction] * [super nbActions] * tauxConversion;

    }

@end
[/code]


#4

Hey, we have almost exactly the same solution but I have a question.
My question is regarding this particular line of code in ForeignStockHolding.m

float costInRinggit = [super costInDollars];
return costInRinggit * conversionRate;

So here’s the question:
I can’t wrap my head around why “costInDollars” method to super returns the correct value.

In the message we are sending the method “costInDollars” and the receiver is the “super”.
The “super” is the StockHolding class. So when StockHoldingClass receives the method “costInDollars” it returns a float that was set equal to the instance variable “purchaseSharePrice” (x number of shares).

-(float)costInDollars
{
return purchaseSharePrice * numberOfShares;
}

But we have 3 instances of StockHolding and 3 Instances of ForeignStockHolding, I can’t see how sending that method to “super” returns the right value.
Where is the connection between the super class, and that specific instance of ForeignStock holding - which we had set the values for?

Thanks!
Sorry for the long message.


#5

[quote]Where is the connection between the super class, and that specific instance of ForeignStock holding - which we had set the values for?
[/quote]
There are two connections.

You can see the first one at the syntactical level:

@interface ForeignStockHolding : StockHolding
{
    float conversionRate;
}

@property float conversionRate;

@end

This is easy to see: ForeignStockHolding is a StockHolding.

But you can’t see the second connection, the connection that makes the whole thing work. This second connection is created by the code generated by the compiler and used by the Objective-C runtime system to give the illusion of magical things happening. (There is no magic; the whole thing is just a network of connected instances of data structures.)

[Become a competent programmer faster than you can imagine: pretty-function.org]


#6

Implementation (in files respective to ForeignStockHolding.h, ForeignStockHolding.m,main.m)

[code]#import “StockHolding.h”

@interface ForeignStockHolding : StockHolding
@property (nonatomic) float conversionRate;
@end
[/code]

[code]@implementation ForeignStockHolding

-(float)costInDollars
{

return [super costInDollars]/[self conversionRate];

}

-(float)valueInDollars
{

return [super valueInDollars]/[self conversionRate];

}
@end
[/code]

ForeignStockHolding *fourthStock = [[ForeignStockHolding alloc]init]; [fourthStock setCurrentSharePrice:1]; [fourthStock setPurchaseSharePrice:2]; [fourthStock setNumberOfShare:10]; [fourthStock setConversionRate:2];