cppTest-4.0:結構體

/**
 *cppTest-4.0:結構體
 *
 *c++對比比c的結構體:
 *1、多了函數成員!
 *2、定義結構體變量時不用在前面加struct關鍵字!
 *3、結構體是一種特殊的類,因此定義結構體類型時不能初始化變量!這個與c語言相同!詳情請看如下例子。 
 *author 煒sama
 */
#include<iostream.h>
#include<conio.h>
#define	TIMES	12800000
struct	time{
	int	hours;
	int	minutes;
	int	seconds;
};//分號不能漏!!!
void update(struct time *t);
void display(struct time *t);
void delay();
int main()
{
	struct test{
		int i;
		//int i1=0;//錯誤! 
		//const int i2=0;//錯誤! 
		//static const int i3=0;//錯誤! 
		char c;
		void print(){cout<<i<<"--"<<c<<endl;};//c++的結構體可以有函數成員!默認是public的
	};
	test t;//c++才支持這樣定義結構體變量!如果是c語言的話應該如此:struct test t;
	t.i=10;
	t.c='c';
	cout<<t.i<<endl;
	cout<<t.c<<endl;
	t.print();

	struct time t1;
	t1.hours=0;
	t1.minutes=0;
	t1.seconds=0;
	for(;!kbhit();){
		update(&t1);
		display(&t1);
	}
	
	return 0;
}
void update(struct time *t)
{
	t->seconds++;
	if(t->seconds==60){
		t->seconds=0;
		t->minutes++;
	}
	if(t->minutes==60){
		t->minutes=0;
		t->hours++;
	}
	if(t->hours==24) t->hours=0;
	delay();
}
void display(struct time *t)
{
	cout<<t->hours<<":"<<t->minutes<<":"<<t->seconds<<endl;
}
void delay()
{
	long int i;
	for(i=0;i<3*TIMES;i++);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章