Amazon.com Widgets Chapter 8 Ex5 Importing classes
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 10:51:21 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
|-+  Old Stuff
| |-+  Chapter Study
| | |-+  Chapter 8 - Inheritance
| | | |-+  Chapter 8 Ex5 Importing classes
Pages: [1]   Go Down
Print
Author Topic: Chapter 8 Ex5 Importing classes  (Read 759 times)
wellaged37
Newbie
*
Posts: 39






« on: March 24, 2009, 12:37:29 PM »

This is the first time I've had to deal with a number of classes at one time in a project. It occurred to me that I don't know which classes to import to which other classes. For example, the classes Circle, Triangle, GraphicObject and others are creating the confusion for me. There must be a simple rule to use to sort out the import directives. Does it have to do with the relationship between superclass and subclass?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #1 on: March 24, 2009, 01:13:22 PM »

Yes, it does have to do with the relationships.  If you're using a subclass of a class, you import the header file for that subclass.  Since it is a subclass, that class will import the header file of its superclass (either explicitly by name or implicitly with an import like <Foundation/Foundation.h>).

In exercise 5, since Rectangle (or Circle or Triangle) is a subclass of GraphicObject, you only need to import Rectangle.h.  Rectangle.h will need to declare GraphicObject as its header file and import GraphicObject.h (as we presume it's been labeled):

        NSObject
             |
             v
    GraphicObject
             |
             v
===========
|            |           |
Square  Circle   Rectangle
   
Imports can be nested.  That is when you import Square.h, it will import GraphicObject.h, which in turn will import NSObject.h.   All of this happens during preprocessing, as described in more detail in Chapter 12. And as a result, you don't need to import all of the header files that are involved here.
               
Hope this helps,

Steve Kochan

« Last Edit: March 25, 2009, 11:18:01 AM by skochan » Logged
wellaged37
Newbie
*
Posts: 39






« Reply #2 on: March 25, 2009, 08:54:58 AM »

I want to confirm whether I understand what you are saying.
Let's say that I import Square.h. Therefore, I don't need to import Circle.h, Rectangle.h or GraphicObject.h. If this is correct, then I have the following questions.
Do I put the import directive for Square.h in the file that holds the main() function?
Should I use graphic sketches like the one in your reply to help me decide which declarative file to import? It seems to me that if you continue your sketch to show subclasses of Square, then the last subclass is the one to import.

Thanks.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #3 on: March 25, 2009, 09:08:40 AM »

I want to confirm whether I understand what you are saying.
Let's say that I import Square.h. Therefore, I don't need to import Circle.h, Rectangle.h or GraphicObject.h. .

No, you will need to import Circle.h if you use Circle objects, and Rectangle.h if you use Rectangle objects in your program.   Maybe a simpler way to think of this is that you import the header file for whatever class (and associated methods) you want to use in your program.  That's so the Objective-C compiler knows the name of your class and its methods.

Cheers,

Steve Kochan

Logged
wellaged37
Newbie
*
Posts: 39






« Reply #4 on: March 25, 2009, 11:12:19 AM »

I think your second reply is going to work for me and I will use that idea for now. At a later date I will revisit your first reply and hopefully I will understand it.
Thanks Steve.
Logged
Pages: [1]   Go Up
Print
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 classroomM.com. All rights reserved.