การประกาศตัวแปร ในการประกาศตัวแปรใน C++/CLI รูปแบบ
type_variable variable_name;
โดย variable_name จะต้องขึ้นต้นด้วย a-z หรือ A-Z หรือ _ เท่านั้นจะขึ้นต้นด้วยตัวเลขไม่ได้ และหลังจากขึ้นต้นแล้วตัวถัดมาสามารถเป็นตัวเลขได้เช่น
int _a;
int a12b;
มีชนิดของตัวแปรแบบพิเศษสอง ชนิดด้วยกันคือ Pointer กับ Handle โดย Pointer จะใช้ * ส่วน Handle จะใช้ ^ ตัวแปรแบบ Pointer จะใช้ชี้ไปยัง address ของ
ข้อมูลใดๆ ส่วน Handle เป็นตัวแปรที่ใช้กับ Reference Type เช่น ต้องการประกาศตัวแปรชนิด String ก็เขียนได้ดังนี้
String^ str ="Hello"; หรือ String ^str ="Hello";
การกำหนดค่าให้กับตัวแปรเราสามารถกำหนดค่าให้กับตัวแปรได้หลังเครื่องหมาย = เช่น int a=3; int b=10; ถ้าต้องการสร้างตัวแปรที่มีชนิดเดี่ยวกันหลายๆตัวก็เขียนได้ดังนี้ int a,b,c;
การกำหนดค่าให้กับตัวแปรโดยใช้ Functional Notaion
มีรูปแบบดังนี้
int a(10); หมายความว่ากำหนดค่า 10 ให้กับตัวแปร a
ข้อสำคัญในการกำหนดค่าของต้วแปรอย่างเช่น int a,b,c=10; ในการกำหนดแบบนี้ตัวแปร a และ b จะมีค่าเท่ากับ 0 ส่วนตัวแปร c จะมีค่าเท่ากับ 10 แต่ถ้า
ต้องการกำหนดให้ตัวแปร a และ b และ c มีค่าเท่้ากับ 10 เราต้องเขียนดังนี้ int a=10,b=10,c=10;
ตัวอย่าง
Code:#include "stdafx.h"
using namespace System;
int main(){
int a,b,c=10;
int aa=20,bb=20,cc=40;
int d(50);
String^ str="Hello";
Console::WriteLine("a = {0} , b={1}, c={2}, d ={3}",a,b,c,d);
Console::WriteLine("aa = {0}, bb = {1}, cc = {2}",aa,bb,cc);
Console::ReadLine();
return 0;
}
ผลลัพธ์จะได้
a=0, b=0,c=10,d=50
aa=20,bb=20,cc=40
ชนิดของตัวแปรต่างๆBoolean,Character,Decimal,Floating point,Integer
::Integer::
integer เป็นการกำหนดชนิดของจำนวนเต็มต่างๆ เช่น
int,long,unsigned int,unsigned long,__int64,short
ตัวอย่าง Integer
Code:#include "stdafx.h"
using namespace System;
int main(){
int a(1);
long b(1000000000L);
short c(12345);
__int64 e(234324);
Console::WriteLine(a);
Console::WriteLine(b);
Console::WriteLine(c);
Console::WriteLine(e);
Console::ReadLine();
return 0;
}
::Decimal::
อยู่ในคลาส System.Decimal
ตัวอย่าง
Code:#include "stdafx.h"
using namespace System;
int main(){
Decimal a =(Decimal)453453454565654645654634.342343;
Decimal b =(Decimal)-111111113.3333333;
Console::WriteLine(a);
Console::WriteLine(b);
Console::ReadLine();
return 0;
}
::Floating point::
ได้แก่ float,double
ตัวอย่าง
Code:#include "stdafx.h"
using namespace System;
int main(){
double a =999999999.9999;
float b =8888.8888f;
Console::WriteLine(a);
Console::WriteLine(b);
Console::ReadLine();
return 0;
}
::Boolean::
ได้แก่ bool มีสองค่าคือ จริง(1) หรือ เท็จ(0)
ตัวอย่าง
Code:#include "stdafx.h"
using namespace System;
int main(){
bool a=1;
bool b=0;
Console::WriteLine("a= "+a);
Console::WriteLine("b= "+b);
Console::ReadLine();
return 0;
}
::Character::
ได้แก่ wchar_t
Code:#include "stdafx.h"
using namespace System;
int main(){
char a='a';
wchar_t b='b';
Console::WriteLine(a);//ชนิด char จะอยู่ใน integer เนื่องจากว่าจะแสดงรหัสในรูปของ ASCII ออกมา ดังนั้นจะได้ 97
Console::WriteLine(b);
Console::ReadLine();
return 0;
}
การจัดการกับ ตัวแปรชนิด referenceตัวแปรชนิดนี้จะให้ Handle ในการประกาศตัวแปรชนิดของตัวแปรreference ได้แก่ String,Object ต่างๆเช่น Array ฯ
ตัวอย่างการใช้งาน String
Code:#include "stdafx.h"
using namespace System;
int main(){
String^ str1 = "Hello";
String^ str2 =", How are you?";
Console::WriteLine(str1+str2);
Console::ReadLine();
return 0;
}
ผลลัพธ์จะแสดง Hello, How are you? ออกมา
การใช้งาน User Defined Typeชนิดของ User Defined Type เป็นชนิดที่กำหนดเอง โดยมีสองแบบด้วยกันคือ
Reference Type กับ Value Type
Value Type ได้แก่
value class,value struct,enum class ,enum struct
ตัวอย่างการใช้ enum
Code:#include "stdafx.h"
using namespace System;
enum class Sports {
Football,Basketball
};
int main(){
Console::WriteLine("Showing Sports");
Console::WriteLine("1. {0}",Sports::Football);
Console::WriteLine("2. {0}",Sports::Basketball);
Console::ReadLine();
return 0;
}
ปกติแล้วการประกาศข้อมูลชนิด enum สามาชิกต่างๆใน enum จะเรียงลำดับจาก 0 ไปเรื่อยๆดังนี้
Code:enum class Sports{
Football,Basketball
};
ถ้าแสดงลำดับของสมาชิกด้วยคำสั่ง
Code:Console::WriteLine("1. {0}",(int)Sports::Football);
Console::WriteLine("2. {0}",(int)Sports::Basketball);
ผลลัพธ์จะได้
0
1
ดังนั้นถ้าต้องการเปลี่ยนลำดับการแสดงใหม่เช่น ให้เริ่มที่ 100 ผลลัพธ์จะได้ 100,101,.. ตัวอย่าง
Code:#include "stdafx.h"
using namespace System;
enum struct Sports {
Football=100,Basketball,Tennis,Golf
};
int main(){
Console::WriteLine("Showing Sports");
Console::WriteLine("1. {0}",(int)Sports::Football);
Console::WriteLine("2. {0}",(int)Sports::Basketball);
Console::WriteLine("3. {0}",(int)Sports::Tennis);
Console::WriteLine("4. {0}",(int)Sports::Golf);
Console::ReadLine();
return 0;
}
ผลลัพธ์จะได้
1. 100
2. 101
3. 102
4. 103
สังเกตว่าตัวอย่างนี้จะใช้ enum struct ซึ่ง enum struct กับ enum class จะเหมือนกัน
การใช้งาน Value class รูปแบบ จะเป็นการสร้าง class ชึ้นมาชนิดเป็น value ตัวอย่าง
Code:#include "stdafx.h"
using namespace System;
value class Shapes{
public:
int width;
int height;
Shapes(int width,int height){
this->width=width;
this->height=height;
}
public:
String^ Show(){
return "This shape is "+width+" width and "+height+" height";
}
};
int main(){
Shapes s(10,20);
Console::WriteLine(s.Show());
Console::ReadLine();
return 0;
}
ในตัวอย่างนี้ ผลลัพธ์จะได้
This shape is 10 width and 20 height
สังเกตุว่าคลาสในตัวอย่างนี้เป็นชนิด value class ดังนั้นในการเรียกใช้งานคลาสไม่ต้องสร้าง instance ใหม่เรา
สามารถเรียกใช้งานคลาสโดยกำหนด parameter ที่จะส่งไปให้ constructor ได้เลย(Shapes s(10,20))
สำหรับข้อมูลชนิด Value เราสามารถใช้ dot (.) เรียกใช้สมาชิกได้ต่างจาก Reference ซึ่งต้องใช้ -> ในการเรียกสมาชิก
แล้วใน constructor ของคลาส Shapes นี้มีการใช้ this->width=width และ this->height=height หมายถึง
this เป็นการเรียกใช้ตัวแปรของคลาสเหมือนกับการใช้งาน this ใน C# ในกรณีที่ตัวแปรคลาสมีชื่อเดียวกับตัวแปรที่รับเข้ามาทาง
Constructor
การใช้งาน reference classรูปแบบ เป็นการสร้างคลาสโดยใช้ keyword Ref นำหน้าเช่น ref class class_name
ตัวอย่าง
Code:#include "stdafx.h"
using namespace System;
ref class Shapes{
public:
int width;
int height;
String^ shape_name;
Shapes(int width,int height,String^ shape_name){
this->width=width;
this->height=height;
this->shape_name =shape_name;
}
String^ ShowDetails(){
return String::Format("Shape 's Name is {0} ,this shape has {1} Width and {2} Height ",shape_name,width,height);
}
};
void main(){
Shapes^ s =gcnew Shapes(3,4,"A");
Console::WriteLine(s->ShowDetails());
Console::ReadLine();
}
ผลลัพธ์จะได้
Shape 's name is A , this shape has 3 Width and 4 Height ดังรูปที่ 1
paedotnet attached the following image(s):

[With great power comes great responsibility]