Whidbey C++
by Richard Grimes



Listing One

StringBuilder^ sb = gcnew StringBuilder;
sb->Append("Today is ");
sb->Append(DateTime::Now.DayOfYear);
sb->Append(" days from the beginning of the year");

Listing Two

using namespace System;
ref class Base
{
public:
   virtual void a(){Console::WriteLine("called Base::a");}
   virtual void b(){Console::WriteLine("called Base::b");}
};
ref class Derived : Base
{
public:
   void a () override {Console::WriteLine("called Derived::a override");}
   void b () new {Console::WriteLine("called Derived::b new");}
};
void main()
{
   Derived^ d = gcnew Derived;
   Console::WriteLine("call a through a Derived reference");
   d->a();
   Console::WriteLine("call b through a Derived reference");
   d->b();

   Base^ b = d;
   Console::WriteLine("call a through a Base reference");
   b->a();
   Console::WriteLine("call b through a Base reference");
   b->b();
}
Results:
call a through a Derived reference

called Derived::a override
call b through a Derived reference
called Derived::b new
call a through a Base reference
called Derived::a override
call b through a Base reference
called Base::b


Listing Three

ref class Person
{
public:
   property String^ Name
   {
      String^ get() { return name; }
      void set(String^ n) { name = n; }
   }
   property int Age;
private:
   String^ name;
};


Listing Four

ref class Base
{
   int data;
public:
   property int Data
   {
      int get(){return data;}
   protected:
      void set(int x){data = x;}
   }
};
ref class Derived : public Base
{
public:
   Derived(int i)
   {
      Data = i;
   }
};


Listing Five

ref struct Test
{
   delegate void Del();
   void CalleeOne(){}
   void CalleeTwo(){}

   void Caller()
   {
      Del^ del1 = gcnew Del(this, &Test::CalleeOne);
      Del^ del2 = gcnew Del(this, &Test::CalleeTwo);

      // Create a delegate that will call both CalleeOne and CalleeTwo
      Del^ del3 = del1 + del2; // Calls Delegate::Combine
      // Create a delegate that will call just CalleeTwo
      Del^ del4 = del3 - del1; // Calls Delegate::Remove
   }
};


Listing Six

using namespace System;
ref class Month
{
   int month;
public:
   Month(int m) : month(m){}
   static Month^ operator ++ (Month^ m)
   {
      m->month = m->month == 12 ? 1 : m->month + 1;
      return m;
   }
   String^ ToString () override
   {
      return String::Format("value is {0}", month);
   }
};
void main()
{
   Month^ m = gcnew Month(6);
   Console::WriteLine("before {0}", m);
   ++m;
   Console::WriteLine("after {0}", m);
}





3


