标签:c++类
面向对象编程:
类:
定义类:
构造并使用对象
//
// main.cpp
// ClassAndObject
//
// Created by 06 on 15/1/17.
// Copyright (c) 2015年 黄永锐. All rights reserved.
//
#include <iostream>
using namespace std;
//类
class Time{
//默认是私有的
int hour;
int min;
int sec;
//
void dida(){
sec++;
if (60 == sec) {
sec = 0;
min++;
}
if (60 == min) {
min = 0;
hour++;
}
if (24 == hour) {
hour = 0;
}
}
//
void show(){
cout << hour << "时" << min << "分" << sec << "秒" << endl;
}
public:
//公开的初始化方法
void init(int h,int m,int s){
hour = h;
min = m;
sec = s;
}
//
void run(){
while (1) {
show();
dida();
//代表一个时间
time_t cur = time(0);//0是获取当前系统的时间
//注意理解
while (cur == time(0)) {
//死循环1秒钟
}
}
}
//C++中可以这样写
private:
public:
protected:
};
//主函数
int main(int argc, const char * argv[])
{
//创建对象
Time t;
//t.hour;//默认是私有的在类外拿不到
//t.show();//私有的
/*
应该开启一个线程 嘀嗒一下显示一下
t.dida();
t.show();
t.dida();
t.show();
t.dida();
t.show();
*/
t.run();
return 0;
}
标签:c++类
原文地址:http://blog.csdn.net/love9099/article/details/42834631