zoukankan      html  css  js  c++  java
  • C++/CLI几点说明:

    结构:

    // Definition of a struct to represent rectangles
    struct RECTANGLE
    {
      int Left;                            // Top left point
      int Top;                             // coordinate pair

      int Right;                           // Bottom right point
      int Bottom;                          // coordinate pair
    };

    类:

    class CBox                             // Class definition at global scope
    {
      public:
        double m_Length;                   // Length of a box in inches
        double m_Width;                    // Width of a box in inches
        double m_Height;                   // Height of a box in inches
    };

    CBox boxes[5];                       // Array of CBox objects declared
      CBox match(2.2, 1.1, 0.5);           // Declare match box
      CBox cigar(8.0, 5.0, 1.0);           // Declare cigar Box
      CBox* pB1 = &cigar;             // Initialize pointer to cigar object address
      CBox* pB2 = 0;                       // Pointer to CBox initialized to null

      cout << endl
           << "Address of cigar is " << pB1     // Display address
           << endl
           << "Volume of cigar is "
           << pB1->Volume();                    // Volume of object pointed to

      pB2 = &match;
      if(pB2->Compare(pB1))                     // Compare via pointers
        cout << endl
             << "match is greater than cigar";
      else
        cout << endl
             << "match is less than or equal to cigar";

      pB1 = boxes;                              // Set to address of array
      boxes[2] = match;                         // Set 3rd element to match
      cout << endl                              // Now access thru pointer
           << "Volume of boxes[2] is " << (pB1 + 2)->Volume();

      cout << endl;

    // Class representing a height
    value class Height
    {
    private:
      // Records the height in feet and inches
      int feet;
      int inches;

    public:
      // Create a height from inches value
      Height(int ins)
      {
        feet = ins/12;
        inches = ins%12;
      }

      // Create a height from feet and inches
      Height(int ft, int ins) : feet(ft), inches(ins){}
    };

    Height myHeight = Height(6,3);
      Height^ yourHeight = Height(70);
      Height hisHeight = *yourHeight;

      Console::WriteLine(L"My height is {0}", myHeight);
      Console::WriteLine(L"Your height is {0}", yourHeight);
      Console::WriteLine(L"His height is {0}", hisHeight);

    ref class Box 
    {
      public:
        // No-arg constructor supplying default field values
        Box(): Length(1.0), Width(1.0), Height(1.0)
       {
         Console::WriteLine(L"No-arg constructor called.");
       }
        // Constructor definition using an initialisation list
        Box(double lv, double bv, double hv):
                                 Length(lv), Width(bv), Height(hv)
       {
         Console::WriteLine(L"Constructor called.");
       }

       // Function to calculate the volume of a box
       double Volume()
       {
         return Length*Width*Height;
       }

      private:
        double Length;                     // Length of a box in inches
        double Width;                      // Width of a box in inches
        double Height;                     // Height of a box in inches
    };

    Box^ aBox;                           // Handle of type Box^
      Box^ newBox = gcnew Box(10, 15, 20);
      aBox = gcnew Box;                    // Initialize with default Box
      Console::WriteLine(L"Default box volume is {0}", aBox->Volume());
      Console::WriteLine(L"New box volume is {0}", newBox->Volume());

    属性的定义:

    using namespace System;

    ref class Name
    {
      private:
      array<String^>^ Names;

    public:
      Name(...array<String^>^ names) : Names(names) {}

      // Scalar property specifying number of names
      property int NameCount
      {
        int get() {return Names->Length; }
      }

      // Indexed property to return names
      property String^ default[int]
      {
        String^ get(int index)
        {
          if(index >= Names->Length)
            throw gcnew Exception(L"Index out of range");
          return Names[index];
        }

        void set(int index, String^ name)
        {
          if(index >= Names->Length)
            throw gcnew Exception(L"Index out of range");
          Names[index] = name;
        }
      }

      // Indexed property to return initials
      property wchar_t Initials[int]
      {
        wchar_t get(int index)
        {
          if(index >= Names->Length)
            throw gcnew Exception(L"Index out of range");
          return Names[index][0];
        }
      }
    };

    int main(array<System::String ^> ^args)
    {
      Name^ myName = gcnew Name(L"Ebenezer", L"Isaiah", L"Ezra", L"Inigo",
                                                                   L"Whelkwhistle");
      myName[myName->NameCount - 1] = L"Oberwurst";  // Change last indexed property

      // List the names
      for(int i = 0 ; i < myName->NameCount ; i++)
        Console::WriteLine(L"Name {0} is  {1}", i+1, myName[i]);

      // Output the initials
      Console::Write(L"The initials are:");
      for(int i = 0 ; i < myName->NameCount ; i++)
        Console::Write(L" {0}", myName->Initials[i]);
      Console::WriteLine();

      return 0;
    }

    1、定义标量属性

  • 相关阅读:
    Oracle 单引号与双引号的区别
    oracle 生成随机数
    oracle 表空间及查看所有用户的表空间
    字节和字符的区别
    java io
    Oracle 基础
    gvim 常用键
    Python之struct
    Python之MySQLdb
    Python之sqlite3
  • 原文地址:https://www.cnblogs.com/panxihua/p/1865884.html
Copyright © 2011-2022 走看看