/**
*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++);
}
cppTest-4.0:结构体
最新推荐文章于 2025-07-09 09:39:05 发布