1 Managed c++ (使用vs2002,2003,建立visual c++下的console application(.net ) project,project還有各種類型)
?? 注: 此時的project setting里一定是 :using Managed Extenstions ->Yes
簡單實例
#using?<mscorlib.dll>
using?namespace?System;

__gc?class?G?


{
public:
????int?k;
????int?sum(int);
};


G::sum(int?i)?
{return?i*(i?+?1)/2;}

int?main()


{
????G?*?g?=?new?G;
????Console::WriteLine(g->sum(4));?//?結果輸出10
????return?0;
}?
接口實例
#using?<mscorlib.dll>
using?namespace?System;


__gc?__interface?Ibase1?
{
????int?f(int);
};

__gc?__interface?Ibase2?
{
????int?f(int);
};

__gc?struct?C:?Ibase1,?Ibase2?
{

????int?f(int?i)?
{?//?接口方法的實現(xiàn)
????????return?2*i-1;
????};
};


int?main(void)
{
????C*?c?=?new?C;
????Console::WriteLine((c?->?f(1)).ToString());?//?輸出結果為1
????Console::WriteLine((__try_cast?(c)->f(2)).ToString());?
????//?輸出結果為3

????Console::WriteLine((__try_cast?(c)->f(3)).ToString());
????//?輸出結果為5

????return?0;
}property實例:
#using?<mscorlib.dll>
using?namespace?System;


__gc?class?G?
{
public:

????__property?int?get_Size()?
{
????????Console::WriteLine(S"get_屬性");
????????return?nSize;
????};

????__property?void?set_Size(int?i)?
{?
????????Console::WriteLine(S"set_屬性");
????????nSize?=?i;
????};
private:
????int?nSize;
};


int?main()?
{
????G?*?pG?=?new?G;
????pG->Size?=?10;?//?調(diào)用set_Size
????int?i?=?pG->Size;?//?調(diào)用get_Size
????Console::WriteLine(i);
}?

//程序結果為:

?????//? set_屬性

????????//? get_屬性

????????//???? 10

代理實例:
#using?<mscorlib.dll>
using?namespace?System;

__delegate?int?GetDayOfWeek();?//?委派方法的聲明
__gc?class?MyCalendar


{
public:

????MyCalendar()?:?m_nDayOfWeek(4)?
{}

????int?MyGetDayOfWeek()?
{
????????Console::WriteLine("非靜態(tài)方法");?
????????return?m_nDayOfWeek;?
????}

????static?int?MyStaticGetDayOfWeek()?
{?
????????Console::WriteLine("靜態(tài)方法");?
????????return?6;?
????}
private:
????int?m_nDayOfWeek;
};

int?main(void)


{
????GetDayOfWeek?*?pGetDayOfWeek;?//?聲明委派類型變量
????int?nDayOfWeek;

????//?將類的靜態(tài)方法MyStaticGetDayOfWeek綁定成委派
????pGetDayOfWeek?=?new?GetDayOfWeek(0,?&MyCalendar::MyStaticGetDayOfWeek);
????nDayOfWeek?=?pGetDayOfWeek->Invoke();?//?委派的調(diào)用
????Console::WriteLine(nDayOfWeek);

????//?將一個類的實例綁定成委派
????MyCalendar?*?pcal?=?new?MyCalendar();
????pGetDayOfWeek?=?
????????static_cast(Delegate::Combine(pGetDayOfWeek,
????????new?GetDayOfWeek(pcal,?&MyCalendar::MyGetDayOfWeek)));
????nDayOfWeek?=?pGetDayOfWeek->Invoke();
????Console::WriteLine(nDayOfWeek);

????//?刪除綁定委派的類實例
????pGetDayOfWeek?=?
????????static_cast(Delegate::Remove(pGetDayOfWeek,
????????new?GetDayOfWeek(pcal,?&MyCalendar::MyGetDayOfWeek)));

????return?0;
}?


/**//* 輸出結果是:

?????? 靜態(tài)方法
?????????
????????? 6

???????????? 靜態(tài)方法

??????????????? 非靜態(tài)方法

?????????????????? 4*/2 c++\CLI (使用vs2005 ,之前的版本不支持,建立visual c++下的CLR Console Application ,當然還有其他的類型可選,比如CLR empty ...^_^,)
? 注:此時project setting 中common language runtime support : Common Language Runtime Support (/clr) ,此時的project中已經(jīng)ref system....DLL,所以不用想上面那樣#using <mscordll>
簡單實例
#include?"stdafx.h"
using?namespace?System;

int?main(array<System::String?^>?^args)


{
????String^?str?=?gcnew?String("Hello?World");
????Object^?o1?=?gcnew?Object();
????Console::WriteLine(str);
????return?0;
}interface 實例
using?namespace?System;

interface?class?IDog


{
????void?Bark();
};

ref?class?Dog?:?IDog


{
public:
????void?Bark()

????
{
????????Console::WriteLine("Bow?wow?wow");
????}
};

void?_tmain()


{
????Dog^?d?=?gcnew?Dog();
????d->Bark();
}property實例
ref?class?Base


{
public:
????ref?struct?SBaseData

????
{
????public:
????????SBaseData()

????????
{
????????????x?=?10;
????????}
????????SBaseData(int?n)

????????
{
????????????x?=?n;
????????}
????????int?x;????????
????};
private:
????SBaseData^?_bd;
public:
????Base()

????
{
????????_bd?=?gcnew?SBaseData();
????}
????virtual?property?SBaseData^?Data

????
{
????????SBaseData^?get()

????????
{
????????????return?_bd;
????????}
????????void?set(SBaseData^?val)

????????
{
????????????_bd->x?=?val->x;
????????}
????}
};
ref?class?Derived?:?Base

????
{
????public:
????????ref?struct?SDerivedData?:?Base::SBaseData

????????
{
????????public:
????????????SDerivedData(int?n1,?int?n2)?:?SBaseData(n1)

????????????
{
????????????????y?=?n2;
????????????}
????????????SDerivedData()?:?SBaseData()

????????????
{
????????????????y?=?100;
????????????}
????????????int?y;????????
????????};
????private:
????????SDerivedData^?_bd;
????public:
????????Derived()

????????
{
????????????_bd?=?gcnew?SDerivedData();
????????}

????????virtual?property?SBaseData^?Data

????????
{
????????????SBaseData^?get()?override?//=?Base::Data::get

????????????
{
????????????????return?_bd;
????????????}
????????????void?set(SBaseData^?val)?override?//=?Base::Data::set

????????????
{
????????????????try

????????????????
{
????????????????????_bd->x?=?val->x;
????????????????????_bd->y?=?safe_cast<SDerivedData^>(val)->y;
????????????????}
????????????????catch(System::InvalidCastException?^)

????????????????
{
????????????????????//log?an?error
????????????????}
????????????}
????????}
????????virtual?property?SDerivedData^?SData

????????
{
????????????SDerivedData^?get()??

????????????
{
????????????????return?_bd;
????????????}
????????????void?set(SDerivedData^?val)?

????????????
{
????????????
????????????????_bd->x?=?val->x;
????????????????_bd->y?=?val->y;
????????????????????????????
????????????}
????????}
????};
int?main(array<System::String?^>?^args)



{
????Derived^?d?=?gcnew?Derived;
????//Base^?d?=?gcnew?Derived;???//?error?,?y?is?not?a?member?of?Base?class
????int?num?=?d->Data->x;
????int?num2?=?d->SData->y;
????System::Console::WriteLine(num);
????System::Console::WriteLine(num2);
????return?0;
}index實例
using?namespace?System;
using?namespace?System::Collections;
ref?class?R


{
private:
????Hashtable^?h;
public:
????R()

????
{
????????h?=?gcnew?Hashtable();
????}

????//Named?property
????property?int?Age[String^]

????
{
????protected:
????????int?get(String^?s)

????????
{
????????????if(h->ContainsKey(s))

????????????
{
????????????????for?each(DictionaryEntry?de?in?h)

????????????????
{
????????????????????if(s->CompareTo(de.Key)?==?0)
????????????????????????return?(int)de.Value;
????????????????}
????????????}
????????????return?0;
????????}????

????????void?set(String^?s,?int?age)

????????
{
????????????h->Add(s,age);
????????}
????}

????//Default?property
????property?int?default[String^]

????
{
????????int?get(String^?s)

????????
{
????????????return?Age[s];
????????}

????????void?set(String^?s,?int?age)

????????
{
????????????Age[s]?=?age;
????????}
????}????
};

int?main(array<System::String?^>?^args)



{
????R^?r?=?gcnew?R();

????r["Nish"]?=?27;
????r["Smitha"]?=?15;
????r["Chris"]?=?21;
????r["Don"]?=?87;

????Console::WriteLine(r["Nish"]);
????Console::WriteLine(r["George"]);
????Console::WriteLine(r["Smitha"]);
????Console::WriteLine(r["Don"]);
????Console::WriteLine(r["Bert"]);
????return?0;
