Inner classes

From Rosetta Code
Revision as of 03:32, 29 January 2023 by Garbanzo (talk | contribs) (C++ entry)
Inner classes is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Definition

In object-oriented programming, an inner or nested class is a class declared entirely within the body of another class or interface.

It is not the same as a subclass which is a class which inherits members from another class or classes and can be declared anywhere the parent class(es) are within scope. However, inner classes may nevertheless be subclasses of other classes as well.

Task

If your language supports the object oriented paradigm, explain what support it has for inner classes and, if there is no support, what if anything can be done to simulate them.

Illustrate your answer by creating an inner class with a constructor, an instance method and field and show how to instantiate it.

If you language does not support the object oriented paradigm, you may either omit this task or describe (and substitute in your example) any equivalent structure(s) which the language does have.

Reference
Related task


C++

C++ supports nested/inner classes. There is no difference but the term nested is more common.

#include <iostream>
#include <vector>

class Outer
{
    int m_privateField;
    
public:
    // constructor for Outer class
    Outer(int value) : m_privateField{value}{}
    
    // define a nested class
    class Inner
    {
        int m_innerValue;
        
    public:
        // constructor for Inner class
        Inner(int innerValue) : m_innerValue{innerValue}{}
        
        // adds the values from the outer and inner class objects
        int AddOuter(Outer outer) const
        {
            // a nested class has access to the private members of the outer class
            return outer.m_privateField + m_innerValue;
        }
    };
};

int main()
{
    // a nested class can be constructed like any other class; it does not
    // need an instance of the outer class
    Outer::Inner inner{42};
    
    // create an outer class and pass it to the inner class
    Outer outer{1};  
    auto sum = inner.AddOuter(outer);
    std::cout << "sum: " << sum << "\n";
    
    // a common usage of nested types is for containers to define their iterators
    std::vector<int> vec{1,2,3};
    std::vector<int>::iterator itr = vec.begin();
    std::cout << "vec[0] = " << *itr << "\n";
}
Output:
sum: 43
vec[0] = 1

Wren

Strictly speaking, Wren does not support inner classes.

However, it is possible to declare a class within either a method or function and we can use this feature to simulate an inner class.

To make things a little more interesting the inner class in the following example also inherits from the outer class.

class Outer {
    static makeInner {
        class Inner is Outer {
            construct new(field) {
                super(field + 1) // call parent class constructor
                _field = field
            }

            method {
                System.print("Inner's field has a value of %(_field)")
                outerMethod
            }
        }
        return Inner
    }

    construct new(field) {
        _field = field
    }

    outerMethod {
        System.print("Outer's field has a value of %(_field)")
    }
}

var Inner = Outer.makeInner
var inner = Inner.new(42)
inner.method
Output:
Inner's field has a value of 42
Outer's field has a value of 43