---
title: "Classes"
url: https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes
language: cpp
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Classes

Programming language: C++. Other languages: [C#](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/java.md), [JavaScript](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/js.md), [MATLAB](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/matlab.md), [PHP](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/php.md), [Python](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/python.md), [Ruby](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/ruby.md), [Swift](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/swift.md).

A class is a user-defined type that holds a list of fields, like a [struct](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/cpp.md). You define a class with the `class` keyword:

```slice
module M
{
    class TimeOfDay
    {
        short hour;         // 0 - 23
        short minute;       // 0 - 59
        short second;       // 0 - 59
    }
}
```

Unlike a struct, a class can be empty.

Classes offer capabilities that structs do not:

- **Extensibility** You can extend a class through [inheritance](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/class-inheritance.md) and [optional fields](https://docs.zeroc.com/ice/3.8/slice/fields/cpp.md#optional-fields).
- **Graph preservation** Ice preserves the references between class instances: two references to the same instance in a [class graph](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/self-referential-classes/cpp.md) still refer to one instance after unmarshaling.
- **Null value** A class parameter or field can be null, whereas a struct parameter or field cannot.
- **Slicing** With the [sliced format](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slicing-values-and-exceptions/cpp.md#slice-formats), a receiver can unmarshal an instance of a derived class it does not know as the most-derived base class it knows.

Marshaling a class takes more time and space than marshaling a struct, so use a class only when you need one of these capabilities.

> **Note**
>
> A class represents data that you transmit over the wire, just like a struct. You can't define operations on a Slice class or implement an interface with a Slice class.

## Language Mapping

A Slice class is mapped to a C++ class with the same name. The generated class contains a public data member for each Slice field (just as for [structures](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/cpp.md) and [exceptions](https://docs.zeroc.com/ice/3.8/slice/exceptions/cpp.md)). Consider the following class definition:

```slice
class TimeOfDay
{
    short hour;         // 0 - 23
    short minute;       // 0 - 59
    short second;       // 0 - 59
    string tz;          // e.g. GMT, PST, EDT...
}
```

The Slice compiler generates the following code for this definition:

```cpp
class TimeOfDay;
using TimeOfDayPtr = std::shared_ptr<TimeOfDay>;

class TimeOfDay : public Ice::Value
{
public:
    TimeOfDay() noexcept = default;
    TimeOfDay(
        std::int16_t hour,
        std::int16_t minute,
        std::int16_t second,
        std::string tz) noexcept;

    [[nodiscard]] TimeOfDayPtr ice_clone() const;

    std::int16_t hour;
    std::int16_t minute;
    std::int16_t second;
    std::string tz;
};
```

There are a number of things to note about this generated code:

1. The generated class `TimeOfDay` inherits from `Ice::Value`. `Ice::Value` is the ultimate ancestor of all mapped classes.
2. The generated class contains a public data member for each Slice field.
3. The generated class has a constructor that takes one argument for each data member, as well as a default constructor.
4. The generated class has a function, `ice_clone`, which returns a shallow polymorphic copy of this class instance.

### Generated Constructors

Classes have two constructors:

- **Default constructor** Default-constructs each data member. This constructor is `= default` and does not initialize a data member of type `bool`, of an integer, floating-point or enumeration type. The Slice compiler generates a data member initializer for each field with a [default value](https://docs.zeroc.com/ice/3.8/slice/fields/cpp.md#default-values).
- **One-shot constructor** Takes one parameter for each data member, so you can construct and initialize a class instance in a single statement.

For derived classes, the one-shot constructor has one parameter for each of the base class's data members, plus one parameter for each of the derived class's data members, in base-to-derived order. For example:

```slice
class Base
{
    int i;
}

class Derived extends Base
{
    string s;
    string greeting = "hello";
}
```

This generates:

```cpp
class Base;
using BasePtr = std::shared_ptr<Base>;

class Derived;
using DerivedPtr = std::shared_ptr<Derived>;

class Base : public Ice::Value
{
public:
    Base() noexcept = default;
    explicit Base(std::int32_t i) noexcept;

    [[nodiscard]] BasePtr ice_clone() const;

    std::int32_t i;
};

class Derived : public Base
{
public:
    Derived() noexcept = default;
    Derived(std::int32_t i, std::string s, std::string greeting) noexcept;

    [[nodiscard]] DerivedPtr ice_clone() const;

    std::string s;
    std::string greeting{"hello"};
};
```

Note that single-parameter constructors are defined as `explicit`, to prevent implicit argument conversions.

### Printing Classes

You can print any class instance by calling `ice_print` on this instance. `ice_print` is defined on `Ice::Value`. Alternatively, you can print a shared pointer to a class instance (for example, a `TimeOfDayPtr`) with `operator<<`:

```cpp
TimeOfDayPtr breakTime = ...;
cout << "Taking a break at " << breakTime << endl;
```

`operator<<` just calls `Value::ice_print` when the shared pointer is not null.

You can use the metadata directive `"cpp:custom-print"` to tell the Slice compiler that you want to use your own custom print implementation. For example:

```slice
["cpp:custom-print"]
class TimeOfDay { ... }
```

The Slice compiler then generates an `ice_print` override declaration in the mapped C++ class, and you are responsible to implement this member function.
