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

การเขียนโปรแกรม C++/CLI ครั้งที่ 5 Options · View
paedotnet
Posted: Friday, January 04, 2008 3:28:14 PM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 344
Location: bkk

การจัดการทางด้านคลาสต่างๆ

ปกติแล้วในการสร้างคลาส/struct จะมีอยู่ สอง แบบคือใช้ ref class และ ref struct โดย ref class สมาชิกในคลาสนี้จะค่าเริ่มต้นในการเข้าถึงเป็นแบบ private ส่วน ref struct จะมีการเข้าถึง
เป็นแบบ public แต่เราสามารถกำหนดชนิดของการเข้าถึงเป็นแบบอื่นได้เช่น private,public,protected
ในการสร้าง ref struct ก็มีรูปแบบดังนี้

ref struct   struct_name
{
// member....
};

ส่วน การสร้างคลาสมีรูปแบบดังนี้
ref class class_name
{
// member....
};

ในการสร้าง object เพื่อเรียกใช้งานจะใช้ gcnew  ดังนี้
Class_name^   class_instance = gcnew Class_name()

ตัวอย่าง

#include "stdafx.h"
using namespace System;
ref class Test{
private:
 int value;
public:
 Test(){
 value=10;
 }
 int ShowValue(){
 return value;
 }
};
void main(){
Test^ t = gcnew Test();
Console::WriteLine(t->ShowValue());
Console::ReadLine();
}

 

ผลลัพธ์จะแสดง 10

การสืบทอดคลาส

ในการสืบทอดคลาสจะใช้เครื่องหมาย : แล้วตามด้วย คลาสแม่ที่เป็น public ดังนี้
ref class A : public B{
};
ในตัวอย่างนี้คลาสลูก A สืบทอดมาจากคลาสแม่ B โดยคลาส B จะต้องระบุ public นำหน้าด้วย
ตัวอย่างการสืบทอดคลาส


#include "stdafx.h"
using namespace System;
ref class TestParent{
protected:
 int value;
};
ref class TestChild : public TestParent{
public:
 TestChild(){
 value=20;
 }
 int ShowValue(){
 return value;
 }
};
void main(){
TestChild^  t =gcnew TestChild();
Console::WriteLine(t->ShowValue());
Console::ReadLine();
}
 
 
ผลลัพธ์จะแสดง 20

การใช้งาน static member
รูปแบบ static  variable_type   variable_name;
ในการใช้งาน static member จะต้องมีkeyword   static ด้วย
ในการเรียกใช้ static member จะใช้ เครื่องหมาย :: แล้วตามด้วย static member
ตัวอย่างการใช้งาน static member

#include "stdafx.h"
using namespace System;
ref class TestClass{
public:
 static int a; //กำหนดตัวแปรเป็นชนิด static
};
void main(){
 TestClass::a=10; //กำหนดค่าให้กับ static member
 Console::WriteLine(TestClass::a); //เรียกใช้ static member
 Console::ReadLine();
}
 
 
ผลลัพธ์จะได้ 10

การกำหนดค่าตัวแปรพร้อมกับ  constructor

ตัวอย่าง

#include "stdafx.h"
using namespace System;
ref class TestClass{
private:
 int a,b;
public:
 TestClass() : a(3),b(4){  //กำหนดค่าตัวแปร
 }
 int Sum(){
 return a+b;
 }
};
void main(){
TestClass^ t =gcnew TestClass();
Console::WriteLine(t->Sum());
Console::ReadLine();
}

การใช้งาน Virtual method
ในการประกาศ virtual มีรูปแบบดังนี้

virtual int ShowValue(){
}
ในการ override virtual method
มีรูปแบบดังนี้

virtual void ShowValue() override{
}

ตัวอย่าง

#include "stdafx.h"
using namespace System;
ref class TestClass{
public:
 int a,b;
 virtual void Sum(){
  Console::WriteLine("TestClass : "+(a+b));
 }
};
ref class TestOverride : public TestClass{
public:
 virtual void Sum() override{
  Console::WriteLine("TestOverride :"+(a+b));
 }
};
void main(){
TestOverride^ t =gcnew TestOverride();
t->a=14;
t->b=20;
t->Sum();
Console::ReadLine();
}

 

การใช้ Hiding method
จะใช้ new ดังนี้
void ShowValue() new{
}

การใช้งาน abstract class
การสร้าง abstract class จะต้องมี keyword   abstract ดังนี้
มีรูปแบบดังนี้
ref class TestAbstract  abstract{
};
ในการประกาศ abstract method จะมีรูปแบบดังนี้
virtual void Show() = 0
ใน abstract class จะไม่สามารถสร้าง object ของคลาสที่เป็น abstract โดยใช้ gcnew ได้
ตัวอย่าง


#include "stdafx.h"
using namespace System;
ref class TestAbstract abstract{
public:
 int value;
 virtual void Show() = 0; // abstract method
 TestAbstract(int i) : value(i){
 }
};
ref class TestCallAbstract : public TestAbstract{
public:
 TestCallAbstract(int i ) : TestAbstract(i){ //เรียกใช้ constructor ของ คลาสแม่
 }
 virtual void Show() override{
  Console::WriteLine("Value is  : "+value);
 }
};
void main(){
TestAbstract^ t =gcnew TestCallAbstract(100);
t->Show();
Console::ReadLine();
}

ผลลัพธ์ Value is : 100

Interface class
สำหรับคลาส ที่กำหนดเป็น Interface จะมีการเข้าถึงเป็นแบบ public โดยอัตโนมัติเพื่อที่จะได้ให้คลาสอื่นเรียกใช้งานได้
การสร้าง interface class มีรูปแบบดังนี้
interface class  interface_name{
//virtual method....
};

ตัวอย่าง


#include "stdafx.h"
using namespace System;
interface class TestInterface{
property String^ Value;
void Show();
};
ref class TestCallInterface : public TestInterface{
private:
 String^ val;
public:
 virtual void Show(){
  Console::WriteLine(val);
 }
 virtual property String^ Value{
  String^ get(){
  return val;
  }
  void set(String^ value){
  val=value;
  }
 }
};
void main(){
TestInterface^  i =gcnew TestCallInterface();
i->Value="Hello";
i->Show();
Console::ReadLine();
}
 
 
ผลลัพธ์ Hello


[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