| Structure  
         
          |  |  |  |   
          |  | #include <iostream.h>
struct human
{
	float height;
	int weight;
}one;
int main()
{
	human two;
	one.height=170.5;
	one.weight=50;
	two.height=165.7;
	two.weight=40;
	cout<<one.height<<"\n";
	cout<<two.weight;
	return 0;
} |  |   
          |  |  |  |  [Download Code]
 ตัวอย่างนี้แสดงถึงการใช้คำสั่ง struct 
        ครับ โดยเริ่มแรกเราสร้าง structure ชื่อว่า human ขึ้นมา จากนั้นก็ให้ในนี้มีข้อมูลชนิด 
        float, int ครับ ส่วนท้ายของคำสั่ง struct เราก็ได้สร้างตัวแปรขึ้น 1 ตัว 
        ซึ่งเป็นตัวแปรชนิด structure ครับ พอเริ่มต้นส่วนของเมน ผมก็ได้สร้างตัวแปรชนิด 
        structure ขึ้นอีก 1 ตัว ครับ ซึ่งเป็นอีกวิธีหนึ่ง ส่วนการให้ค่าเราก็จะใช้ 
        . ช่วยครับ 
         
          |  |  |  |   
          |  | #include <iostream.h>
struct teacher
{
	int age;
	int dd;
};
struct student
{
	int age;
	char grade;
	teacher one;
};
int main()
{
teacher two,*pt;
student three[10];
	two.dd=3;
	two.age=30;
	pt->dd=5;
	pt->age=35;
	three[0].grade='A';
	three[1].grade='C';
	three[2].one.dd=5;
	cout<<two.dd<<"\n";
	cout<<pt->age<<"\n";
	cout<<three[1].grade<<"\n";
	cout<<three[2].one.dd;
	return 0;
} |  |   
          |  |  |  |  [Download Code]
 ตัวอย่างนี้เป็นการแสดงถึงการใช้ structure 
        แบบต่างๆ โดยเริ่มแรกเราสร้าง structure ขึ้นมา 2 อัน สังเกตในส่วนของ student 
        นะครับ ผมได้ สร้าง one ขึ้นมาเพื่อให้เราสามารถเรียก structure ของ teacher 
        จาก student ได้ครับ พอเริ่มส่วนของผมได้แสดงถึงการให้ 
        structure ร่วมกับพอยต์เตอร์และอะเรย์ครับ และส่วน three[2].one.dd นั้นก็คือผลจากการที่เราสร้าง 
        one ขึ้นมาไงครับ การให้ค่าของพอย์ตเตอร์จะแตกต่างกับตัวอื่นคือจะใช้ 
        ->แทน นะครับ และสุดท้ายการสร้าง structure นั้น จะสร้างก่อนหรือในส่วนของเมนก็ได้นะครับ 
 Union 
         
          |  |  |  |   
          |  | #include <iostream.h>
union human
{
	float height;
	int weight;
}one;
int main()
{
	human two;
	one.height=170.5;
	one.weight=50;
	two.height=165.7;
	two.weight=40;
	cout<<one.height<<"\n";
	cout<<two.weight;
	return 0;
} |  |   
          |  |  |  |  [Download Code]
 อันนี้เหมือนกับตัวอย่างแรกเพียงแต่เปลี่ยนจาก 
        struct เป็น union หลังจาก Run โปรแกรมก็จะเห็นความแตกต่างนะครับ นั่นคือใน 
        1 ตัวแปรนั้นจะสามารถเก็บค่าได้เพียงค่าเดียว นั่นคือ ตอนแรกเราให้ one.height=170.5 
        จากนั้น เราจึงให้ one.weight=50 ดังนั้นค่าของ height จึงถูกแทนที่ด้วยค่าของ 
        weight ครับ ส่วนวิธีการอื่นๆก็เหมือนกับ struct ครับ Enumerated  
         
          |  |  |  |   
          |  | #include <iostream.h>
enum number{one,two,three};
int main()
{
	number start=one;
	cout<<start<<"\n";
	start=three;
	cout<<start<<"\n";
	return 0;} |  |   
          |  |  |  |  [Download Code]
 หลังจาก Run ตัวอย่างนี้ จะเห็นว่าเราสร้าง 
        number ขึ้นมา แล้วให้ในนี้มีตัวแปร one ซึ่งมีค่าเท่ากับ 0 ,two มีค่าเท่ากับ 
        1 ไปเรื่อยๆครับ พอเริ่มส่วนของเมน เราก็สร้าง start ซึ่งเป็นชนิด enumerated 
        ขึ้นมาแล้วให้เท่ากับ one ซึ่งก็คือเท่ากับ 0 แหละครับ นอกนั้นก็ไม่มีอะไรแล้วครับ |