Widgets Instance Variables of different Classes (Question)
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 28, 2015, 02:29:00 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Chapter 8
| | |-+ Instance Variables of different Classes (Question)
Pages: [1] Go Down
Author Topic: Instance Variables of different Classes (Question) (Read 662 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:

Login with username, password and session length

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 (c) 2009 All rights reserved.