Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
October 18, 2017, 06:29:42 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
  Print  
Author Topic: Instance Variables of different Classes (Question)  (Read 955 times)
MCaEd
Newbie
*
Posts: 49


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

Quote
...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?
Logged
dharr19
Full Member
***
Posts: 175


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

Hi,

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
Logged
Pages: [1]   Go Up
  Print  
 
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 classroomM.com. All rights reserved.