1.2.5 Classes


Origin C supports many built-in classes, and also allows you to create your own.

Origin Defined Classes

Origin C includes predefined classes for working with Origin data types and user-interface objects. These classes help you quickly write Origin C code to accomplish common tasks. This section introduces the base classes to give you an overview of their capabilities. See the next chapter, Predefined Classes, or the Origin C Wiki for details and examples of Origin-defined classes.

User Defined Classes

Origin C supports user-defined classes. A user-defined class lets you create objects of your own type with methods (member functions) and data members.

The example below defines a Book class with:

  • a constructor to set the name on creation,
  • a const getter (safe to call on const objects),
  • and two setter overloads (from C-string or from string).
class Book
{
public:
    // Constructors
    Book() : m_strName("") {}                  // default
    Book(LPCSTR name) : m_strName(name) {}     // set name on creation

    // Getter (const so it can be called on const Book)
    string GetName() const
    {
        return m_strName;
    }

    // Setters (overloaded for convenience)
    void SetName(LPCSTR lpcszName)
    {
        m_strName = lpcszName;
    }
    void SetName(const string& name)
    {
        m_strName = name;
    }

private:
    string m_strName;
};

And here is a simple usage example that declares a Book object, sets its name, and outputs it:

void test_class()
{
    Book one;                    // default constructor
    one.SetName("ABC");
    out_str(one.GetName());

    const Book two("Origin C");  // construct with name; const object
    out_str(two.GetName());      // OK (GetName is const)
    // two.SetName("new");       // NOT allowed: 'two' is const
}

For more class features—such as constructors/destructors in larger examples or virtual methods—download this zip file, then browse to \Origin C Examples\Programming Guide\Extending Origin C to see EasyLR.c, EasyLR.h, and EasyFit.h.