Challenge Solution Question [inheritance issues?]


#1

So I have a question regarding inheritance of the “foreign stocks”

Currently my code works listed below:

Foreignstockholding.h

[code]#import <Foundation/Foundation.h>
#import “StockHolding.h” //importing stockholding variables

@interface ForeignStockHolding : StockHolding
//changed class of stockholing from NSObjecto into subclass of Stockholdings
{
float ConversionVariable;
}

@property float ConversionVariable;

@end
[/code]

ForeignStockHolding.m

[code]#import “ForeignStockHolding.h”

@implementation ForeignStockHolding

@synthesize ConversionVariable;

  • (float)costInDollars
    {
    float foreigncost = [super costInDollars];
    return foreigncost * ConversionVariable;
    }

  • (float)valueInDollars
    {
    float foreignvalue = [super valueInDollars];
    return foreignvalue * ConversionVariable;
    }

@end[/code]

StockHoldings.h

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

@interface StockHolding : NSObject

// adding instance variables
{
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;
}
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;
@property float ConversionVariable;

  • (float)costInDollars;

  • (float)valueInDollars;

@end[/code]

StockHolding.m

[code]#import “StockHolding.h”

@implementation StockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

  • (float)costInDollars
    {
    int n = [self numberOfShares];
    return [self purchaseSharePrice] * n;
    }

-(float)valueInDollars
{
int n = [self numberOfShares];
return [self currentSharePrice] * n;
}

@end[/code]

main.m

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

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

@autoreleasepool {
    
    
    //Creating an instance of the stocks
    StockHolding *stockone = [[StockHolding alloc] init];
    StockHolding *stocktwo = [[StockHolding alloc] init];
    StockHolding *foriegnstockthree = [[ForeignStockHolding alloc] init];
 
    

    //Give the instance variables values
    [stockone setPurchaseSharePrice: 2.3];
    [stockone setCurrentSharePrice: 4.5];
    [stockone setNumberOfShares: 40];
    
    [stocktwo setPurchaseSharePrice:12.19];
    [stocktwo setCurrentSharePrice:10.56];
    [stocktwo setNumberOfShares:90];
    
    [foriegnstockthree setPurchaseSharePrice:45.1];
    [foriegnstockthree setCurrentSharePrice:49.51];
    [foriegnstockthree setNumberOfShares:210];

    
    //set converion variable
    
    [foriegnstockthree setConversionVariable:0.94];
    
    

    //Create an empty array
    NSMutableArray *stocklist = [NSMutableArray array];
    
    //add above objects to array
    [stocklist addObject];
    [stocklist addObject];
    [stocklist addObject:foriegnstockthree];
    
    
    for (StockHolding *d in stocklist) {
        NSLog(@"The %d stocks purchased at %f per share costing %f is now selling at %f per share valuing at %f dollars", [d numberOfShares], [d purchaseSharePrice], [d costInDollars], [d currentSharePrice], [d valueInDollars]);
    }
    
    
    
    
    
}
return 0;

}[/code]

So I first solved the challenge when I inputed 0.94 in place of ConversionVariable in the foreignstockholding.m portion. But I wanted to be able to change the conversion factor easily so I replaced the 0.94 with the “ConversionVariable” to hold the place and declared the variable value in the main.m area. But xcode gave me the error:
"No visible @interface for “stockHolding” declares the selector “setConversionVariable”

I resolved the issue by declaring @property in the stockholding.h area.

My question is since ForeignStockHolding inherits from Stockholdings should the @property declaration of the ConversionVariable in the ForeignStockingHolding Suffice? It seems the code is quite redundant right now. Please correct me if my understanding is flawed as I am quite new to programming.

Thanks in advance!


#2

"No visible @interface for “stockHolding” declares the selector “setConversionVariable” .

The reason for the error is that there is no property named conversionVariable in StockHolding class.
There is, however, a similar sounding property named [color=#FF0000]ConversionVariable[/color], with a case error. You need to name the property without any case errors.

Change [color=#FF0000]ConversionVariable[/color] to conversionVariable and try again. Also I would change the name of the property to conversionRate.


#3

Thanks for the Clarification. I don’t quite understand the “case error”. How come even though the declarations has been consistent with: “ConversionVariable” it produces an error, however, when the property is declared as “conversionVariable” It does not?
Thanks in advance!