Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
May 23, 2018, 01:43:08 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
Author Topic: Instance Variables of different Classes (Question)  (Read 1162 times)
Posts: 49

« on: February 21, 2012, 07:59:36 PM »

...each instance of a class gets its own instance variables, even if they're inherited.
So in the Program 8.1, when ClassB uses ClassA's method which contains ins. variable x, does it mean that ClassB will get its own x even thought it was declared in ClassA? And if ClassB makes changes to x, it won't affect ClassA's, initial value of x?
Full Member
Posts: 175

« Reply #1 on: February 22, 2012, 06:48:01 AM »


ClassB inherits the initVar method and instance variable x.  So any ClassB object will have its own x instance variable.  Below I modified the value of x.

Code: (Objective-C)
//Here is ClassB's printVar method. I modified it so that it will add 1 to the value of x.
-(void) printVar
   x = x + 1;
   NSLog(@"x = %i", x);

output: 2012-02-22 07:38:25.219 ClassA[333:603] x = 101
Pages: [1]   Go Up
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 All rights reserved.