Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ - Nested Classes

  1. Jan 27, 2009 #1
    C++ -- Nested Classes

    A post or two I have been working with appeared to be associated with students in a C computer language programming course. In other words, the class may have not been object oriented. Also, mentioned in one post was the concept of housekeeping being less important than the actual logic of producing correct results, at least in a Computer Science class. I would like to address both of these issues from a standpoint of who the type of person I would hire. I am surprised C is being taught instead of C++ and that housekeeping is considered less important that the ultimate result producing logic.

    If I were hiring right now, I would want the type of programmer that knows C++ object oriented programming. Once I find these types of people, I then look at their housekeeping habits, such as not having memory leaks, always initializing every variable or object, and always checking NULL pointers prior to being used. If a potential prgrammer has these abilities, they are considered for the job and if they don't, the resume goes on the 'other' stack.

    Now on to a an open discussion of object oriented programming, which in my opinion should be taught. Here is an example:


    Over the years, I have used nested classes and non-nested classes in an actual production environment. I have found advantages and disavantages in using both concepts.

    Using non-nested inheritance....

    Code (Text):

    class Force
    {
    public:
        Force();
        virtual ~Force();

    protected:

    };

    class NuclearForce : public Force
    {
    public:
        NuclearForce();
        virtual ~NuclearForce();

    protected:

    };

    class ElectronForce : public Force
    {
    public:
        ElectronForce();
        virtual ~ElectronForce();

    protected:
     
    };
     
    Using nested without inheritance....


    Code (Text):

    class Force
    {
    public:
        Force();
        virtual ~Force();

        class NuclearForce
        {
        public:
            NuclearForce ();
            virtual ~NuclearForce();

        protected:

        };

        class ElectronForce
        {
        public:
            ElectronForce();
            virtual ~ElectronForce();

        protected:
     
        };

    protected:
    };
     
    I would really enjoy reading all comments and discussing object oriented technologies.
     
  2. jcsd
  3. Jan 28, 2009 #2
    Re: C++ -- Nested Classes

    I think most classes these days that intend to teach object oriented programming will just jump straight to something like Java... the problem with C++ from a teaching perspective is that it as a language is structured in a way that makes it difficult to adequately teach its object oriented elements until the student fully understands the C underpinnings. And teaching the C underpinnings can take an entire course's worth of time unto itself... So even when C++ is being taught instead of C, these C++ classes will often just be teaching vanilla C (except with cin/cout added) for a long time and then introduce object oriented programming quickly at the end.

    As for your second thing, it seems like nested and descendant classes would be likely to be used only for fairly different purposes. Aren't nested classes in C++ basically just using the enclosing class like a namespace?
     
  4. Jan 28, 2009 #3
    Re: C++ -- Nested Classes

    There is much more of a difference in nested classes relative to non-nested classes than what meets the eye. The naming convention, or the namespace, of course changes and how one accesses objects changes. However, imagine the potential difference in performance as I hope to convey.



    In my experience, nested classes can offer a huge advantage in performance and robustness during runtime except when a program is loading data during load time or saving during save time, and mapping the data to or from the nested slots. In these exceptions, the additional overhead is minimal.

    For example, say I am loading a huge Oracle database job with 100 tabular tables and I am holding the associated data in RAM in class data structures. In my view, there are two ways I can implement this which are described in my first post...nested and non-nested. The way I think of this is the nested concept is object oriented and the non-nested concept is tabular although both can have common object oriented features. I realize this will be a little abstract, so bear with me.

    Generally, when designing the class data structures, I want to share as many classes as possible. To implement this, I look for common denominators in groups of data. The classes that are unique do not have to be exposed but the shares ones do, so I wish to minimize class exposure wherever possible. If I create once class for each database table, then I have 100 classes plus the shared data classes. All visually capable interface and other supported classes are kept seperate for obvious reasons.

    In this non-nested case, or what I call the "outside in" method, in order to access one of the 100 objects, or tables, I might have to touch each 100 of the classes and then potentially cycle for specific data from there, such as embedded arrays, etc., EACH time I needed any data unless the active thread, by chance, was inside an object. This could result in exponentially more loops and hence more time, depending on what data I needed.

    In the nested case, or what I call "inside out", the quantity of loops can be drastically limited if I transfer the 100 tabular table data into the object oriented design classes when loaded from the database. There is a little overhead during this step but the increased performance is eye-popping drastic later on. Once I am in one of the 100 objects, I usually never have to access another object from outside because all the data I need in already nested inside each and every time.

    Make sense?

    If the program runs a lengthy time to perform one complex operation, the savings can be significant. Time is money right?
     
  5. Jan 28, 2009 #4
    Re: C++ -- Nested Classes

    In my original post, you may have noticed I mentioned non-nested inheritance and nested inheritance and the two primary concepts I presented. We can take the nested non-inheritance concept one notch further, to test the limits of a more scalable system. Here, I present an example of nested inheritance. The difference between the two, may not look like much but there is a world of difference. Not only do I get access to improved performance as mentioned in my prior post, and all the other inherent capabilities of C++, like code reuse of objects, but I may get the ability to hook into it with future program versioning.

    Code (Text):

    class Synapse
    {
    public:
        Synapse();
        virtual Synapse();

         class Force
         {
         public:
             Force();
             virtual ~Force();
         };

         class NuclearForce : public Force
         {
         public:
             NuclearForce ();
             virtual ~NuclearForce();

         protected:

         };

         class ElectronForce : public Force
         {
         public:
             ElectronForce();
             virtual ~ElectronForce();

         protected:
     
         };

    protected:

    };
     

    http://www.cs.cornell.edu/andru/papers/ncm04-tr.pdf
     
    Last edited: Jan 29, 2009
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C++ - Nested Classes
  1. C++ operator classes (Replies: 9)

Loading...