.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

การเขียนโปรแกรม C++/CLI ครั้งที่ 1 Options · View
paedotnet
Posted: Wednesday, December 19, 2007 8:35:12 PM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 345
Location: bkk
การเขียนโปรแกรม C++/CLI
CLR = Common Language Runtime เป็นสิ่งที่สำคัญของ .net Framework
CRT = C/C++ Run-Time เป็น Library
MSIL =Microsoft Intermediate Language สิ่งที่ใช้ในการ คอมไพล์ C++/CLI
CLI =Common Language Infrastructure จะสนับสนุนการเขียนโปรแกรมแบบ Dynamic Component Model
เครื่องหมาย / ที่คั้นอยู่ระหว่าง C++/CLI หมายถึงการ Binding ระหว่าง C++ และ CLI
เริ่มต้นเปิด Visual Studio 2008 หรือ Visual studio 2005 ขึ้นมา
ไปที่ File->new->Project เลือก Other language( ในกรณีที่ไม่ได้ตั้งค่าให้ Visual c++ เป็นค่าเริ่มต้น)
หลังจากนั้นเลือก Visual c++ แล้วเลือกลูกศรด้านหน้าเพื่อเปิดส่วนที่ซ่อนอยู่ออกมา แล้วเลือก CLR แล้วเลือก CLR Controle Application
ดังรูป ที่ 1แล้วตั้งชื่อตรงช่อง Name จากนั้นคลิกที่ปุ่ม Ok
ภาษา C++/CLI จะคล้ายๆกับ C# กับ C++ นะครับดังนั้นรูปแบบต่างๆก็จะใกล้เคียงกันเอาละครับหลังจากที่สร้างโปรเจค์แล้วเรามาเริ่มกันเลยดีกว่า
ในการรันหรือคอมไพล์ภาษา C++/CLI ก็จะเหมือนกับ C#

ในตัวอย่างแรกนี้ผมจะแสดงให้ขึ้นคำว่า Hello ออกมานะครับ ดังนี้

Code:
#include "stdafx.h"
using namespace System;
int main(){
    Console::WriteLine("Hello..");
    Console::ReadLine();
    return 0;
}


จากนั้นก็กด Ctrl+F5 เพื่อผลลัพธ์จะแสดง Hello.. ออกมา
คำอธิบาย ในตัวอย่างนี้บรรทัดแรกจะต้อง include ไฟล์ .h ก่อน คือ stafx.h
หลังจากนั้นเป็นการเรียกใช้ คลาสต่างๆที่อยู่ใน เนมสเปซ System ซึ่งจะคล้ายๆกับ C# (using System;)
ต่อไปเป็นฟังก์ชั้นหลักนั้นก็คือ main โดย ฟังก์ชั้นนี้เป็นฟังก์ชั้นหลักที่ต้องมีเพราะโปรแกรมจะเริ่มต้นที่ main() เสมอเหมือนกับ เมธอด Main() ใน C#
ในการแสดงผลจะใช้คำสั่ง Console::WriteLine("ข้อความ");
และบรรทัดสุดท้ายมี return 0; ที่ต้องมีบรรทัดนี้เพราะว่าในฟังก์ชั้น main() มีการกำหนดให้มีการส่งค่ากลับ(int)
แต่ถ้าไม่ต้องการส่งค่ากลับก็ใช้ void main() ก็ได้


paedotnet attached the following image(s):
pic1.jpg



[With great power comes great responsibility]
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by