Class inheritance (C++/CX)

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

An authored base class can inherit a class, and optionally one or more interfaces which must be implemented by the base class. And in a class derived from the base class, you can override a virtual method in the base class.

Guidance

Class inheritance requires that a class be accessible; that is, declared public or protected. As a result, metadata about the class is injected into the corresponding Windows metadata file (.winmd). Class definitions that are declared private, abstract, or pure virtual ("=0") contradict the accessibility principle.

Virtual methods are naturally more affected by class inheritance. With the accessibility principle and virtual methods in mind, the following guidance is offered.

  • By default, all classes are extensible. You can make a class non-extensible with the sealed keyword, but an error is emitted if you declare a protected method in a sealed class.

  • It is recommended that base classes provide a default constructor.

  • A base class can define protected non-virtual methods.

  • Virtual methods must be declared protected or public, and can't specify a private modifier (including protected private). Otherwise, an error is emitted.

  • In constructors and destructors, virtual calls invoke the most derived override. Unless you are certain which override will be called, it is recommended that you not make virtual calls in a constructor or destructor.

Example

The following three examples author a base class, then a derived class, then focuses on overridding a method in a final derived class.

Authoring a base class

The following defines a base class, Button, which is used in the next example.

ref class Button 
{
public:
    Button() : s(0,0)
    {
    }
    Button(Size defaultSize) : s(defaultSize)
    {
        // In C++/CX, this is a virtual call. 
        // To call the function non-virtually, use Button::OnSizeChanged().
        OnSizeChanged(); 
    }
public:
    void SetSize(Size size)
    {
        s = size;
        OnSizeChanged();
    }
    void Measure(Size availableSize)
    {
        // Doesn’t call MeasureOverride(). It calls the most derived type.
        Size s = MeasureOverride(sz); 
 
        // The following calls the non-virtual MeasureOverride() directly.
        Size s2 = Button::MeasureOverride(sz);  
    }
    event Click;
public:
    virtual void OnPublicOverride() {}
protected: 
    virtual void RealignContent(Size sz) {}
    Size GetInternalSize() { return s; }
    virtual Size MeasureOverride(Size availableSize) {return availableSize;}
    virtual void OnClick() {}
    virtual void OnSizeChanged() {}
    int GetTemplateChild() 
    {
        return;
    }
private:
    Size s;
};

Authoring a derived class

The following code authors a class, BestCustomButton class, that is derived from the Button class. The RealignContent() method overrides the same method in the base class. Note that to be overridden, RealignContent() must be declared virtual in the base and derived classes, and that the override keyword must be specified in the derived class.

ref class BestCustomButton: public Button 
{ 
      public: 
      BestCustomButton(): Button(Size(120, 160))  
      { 
            // Call to base class protected method.
            Size sz = GetInternalSize(); 
            // Call to virtual method defined in base class calls the override 
            // from the most derived type.
            RealignContent(sz); 
       } 
       
       protected:
       // Define a new virtual method.
       virtual Size CustomMarginSize() {}  
       
       // Override a base class virtual method.
       virtual Size RealignContent(Size sz) override  
       { 
           // Call to a virtual method calls the override from the most derived type.
           Size szMargin = CustomMarginSize();  
           if(sz.Width < szMargin.Width) sz.Width = szMargin.Width; 
           return sz; 
        } 
};

Overridding methods in a derived class

The following code snippet shows how to override a method in the MyButton class that was derived from the BestCustomButton class defined in the previous example. As was shown in the BestCustomButton class, the CustomMarginSize() method is declared virtual in the base and derived class and is overridden in the derived class.

ref class MyButton sealed : public BestCustomButton 
{ 
      protected: 
      // overrides a base class virtual method 
      virtual Size CustomMarginSize()override 
            { return Size(160, 12); } 
};

See Also

Concepts

Authoring (C++/CX)