< C++ .NET Properties 6 | Main | C++ .NET Delegates & Events 1 >


 

 

Properties 7

get me and set me!

 

 

 

 

 

What we have in this page?

 

  1. Declaring and Using Virtual Properties

  2. Using Multidimensional Properties

  3. Overloading Property Accessor Methods

  4. Declaring Abstract and Sealed Properties

  5. Quick Reference

 

 

Declaring and Using Virtual Properties

 

The following example shows how to declare and use virtual properties.

// more on property keyword

// compile with: /clr

#include "stdafx.h"

 

using namespace System;

 

interface struct IEFace

{

   public:

      property int VirtualProperty1;

      property int VirtualProperty2

      {

         int get();

         void set(int i);

      }

};

// implement virtual events

ref class PropImpl : public IEFace

{

   int MyInt;

   public:

      virtual property int VirtualProperty1;

      virtual property int VirtualProperty2

      {

         int get() { return MyInt; }

         void set(int i) { MyInt = i; }

      }

};

 

int main()

{

   PropImpl ^ MyPI = gcnew PropImpl();

   MyPI->VirtualProperty1 = 300;

   Console::WriteLine("MyPI->VirtualProperty1: {0}", MyPI->VirtualProperty1);

   MyPI->VirtualProperty2 = 400;

   Console::WriteLine("MyPI->VirtualProperty2: {0}", MyPI->VirtualProperty2);

   return 0;

}

 

Output:

 

 

A program example that shows how to declare and use virtual properties

 

Using Multidimensional Properties

 

Multidimensional properties let you define property accessor methods that take a non-standard number of parameters. The following is an example program.

// more on property keyword

// compile with: /clr

#include "stdafx.h"

 

using namespace System;

 

ref class X

{

   double d;

   public:

      X() : d(0) {}

      property double MultiDimProp[int, int, int]

      {

         double get(int, int, int) { return d; }

         void set(int i, int j, int k, double dou)

         {

            // do something with those ints

            d = dou;

         }

      }

 

      property double MultiDimProp2[int]

      {

         double get(int) { return d; }

         void set(int i, double dou)

         {

            // do something with those ints

            d = dou;

         }

      }

};

 

int main()

{

   X ^ MyX = gcnew X();

 

   MyX->MultiDimProp[0,0,0] = 4.5;

   System::Console::WriteLine("MyX->MultiDimProp[0, 0, 0]: {0}", MyX->MultiDimProp[0, 0, 0]);

   return 0;

}

 

Output:

Multidimensional properties program example

 

Overloading Property Accessor Methods

 

It is possible to overload indexed properties as shown in the following example.

// more on property keyword

// compile with: /clr

#include "stdafx.h"

 

using namespace System;

 

ref class X

{

  double d;

  public:

    X() : d(0.0) {}

    property double MyProp[int]

    {

       double get(int i) { return d; }

       double get(System::String ^ i) { return 2*d; }

       void set(int i, double l) { d = i * l; }

    }   // end MyProp definition

};

 

int main()

{

   X ^ MyX = gcnew X();

 

   MyX->MyProp[2] = 7.7;

   System::Console::WriteLine("MyX->MyProp[1]: {0}", MyX->MyProp[1]);

   System::Console::WriteLine("MyX->MyProp[\"Test string\"]: {0}", MyX->MyProp["Test string"]);

   return 0;

}

 

Output:

 

 

 

 

 

 

 

-----------------------------------------------

Overloading the indexed properties program example

 

Declaring Abstract and Sealed Properties

 

Although specified as valid in the ECMA C++/CLI, in the Microsoft Visual C++ 2005 compiler you cannot specify the abstract (C++) or sealed keywords on trivial properties, or on the property declaration of a non-trivial property. To declare a sealed or abstract property, you must define a non-trivial property and specify the abstract or sealed keywords on the get and set accessor functions. Consider the following program example.

// properties, abstract and sealed

// compile with: /clr

#include "stdafx.h"

 

using namespace System;

 

ref struct A abstract

{

   protected:

      int m_i;

   public:

      A() { m_i = 100; }

      // define abstract property

      property int Prop_1

      {

         virtual int get() abstract;

         virtual void set(int i) abstract;

      }

};

 

ref struct B : A

{

   private:

      int m_i;

   public:

      B() { m_i = 200; }

      // implement abstract property

      property int Prop_1

      {

         virtual int get() override { return m_i; }

         virtual void set(int i) override { m_i = i; }

      }

};

 

ref struct C

{

   private:

     int m_i;

   public:

     C() { m_i = 300; }

     // define sealed property

     property int Prop_2

     {

        virtual int get() sealed { return m_i; }

        virtual void set(int i) sealed { m_i = i; };

     }

};

 

int main()

{

   B b1;

  

   // call implementation of abstract property

   System::Console::WriteLine("b1.Prop_1: {0}", b1.Prop_1);

   C c1;

   // call sealed property

   System::Console::WriteLine("c1.Prop_2: {0}", c1.Prop_2);

   return 0;

}

 

Output:

Declaring and using a sealed or abstract property

 

Quick Reference

 

To

Do this

Create a property for a C++ class.

Use the property keyword with get and set methods. For example:

 

property void sWeight(int n) { ... }

property int gWeight() { ... }

 

Implement a read-only property.

Implement only the get method.

Implement a write-only property.

Implement only the set method.

Implement an indexed property.

Implement a property whose get and set methods take parameters that are used to determine which value to get or set. For example:

 

property void sPay[Person^, Amount^] { ... }

property Amount^ gPay[Person^] { ... }

 

 

Table 2

 

 

 

 

 

Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

 


 

< C++ .NET Properties 6 | Main | C++ .NET Delegates & Events 1 >