c++入门(类和对象and继承for系统时间显示)
来源:互联网 发布:java架构师 在线课 编辑:程序博客网 时间:2024/06/15 20:39
文件名:<1> time.h ;<2> time.cpp ;<3> CurrTime.h ;<4> CurrTime.cpp ;<5> main.cpp 。。。
1、代码:class c_CurrTime : public c_Time //c_CurrTime类 继承 c_Time 类
2、代码: c_Time* p = &CurrTime; // 基类的指针只能调用基类的方法,而不能调用派生类方法
std::cout<< p -> getHour() << ":" << p -> getMinute() << ":" << p -> getSecond() <<std::endl;
<3> CurrTime.h
1 //文件名 CurrTime.h 2 3 #ifndef CURRENT_TIME_H //防止重复包含的宏开关 4 #define CURRENT_TIME_H 5 6 #include "time.h" 7 8 class c_CurrTime : public c_Time //继承 c_Time 类 9 { 10 public: 11 c_CurrTime(); 12 ~c_CurrTime(); 13 14 int getHour(); //修改基类的getHour()方法 15 16 public: 17 void init_Time(); 18 }; 19 20 #endif
<4> CurrTime.cpp
1 //文件名 CurrTime.cpp 2 3 #include "CurrTime.h" 4 5 #include <iostream> 6 #include <ctime> //系统时间库 7 8 c_CurrTime::c_CurrTime() //派生 构造函数 9 :c_Time() //调用 基类的 无参数默认 构造函数 10 { 11 init_Time(); 12 std::cout<< "c_CurrTime construction !" <<std::endl; 13 14 } 15 16 c_CurrTime::~c_CurrTime() //派生 析构函数 17 { 18 std::cout<< "c_CurrTime destruction !" <<std::endl; 19 } 20 21 void c_CurrTime::init_Time() //获取系统时间 22 { 23 time_t t = time (0); 24 tm tt = *localtime(&t); 25 26 setHour(tt.tm_hour); //调用基类的方法 27 setMinute(tt.tm_min); 28 setSecond(tt.tm_sec); 29 30 } 31 32 int c_CurrTime::getHour() //时间转为十二小时制:若小时大于12,则减掉12 33 { 34 int temp = c_Time::getHour(); //m_Hour为类C_Time的private成员,类c_CurrTime无法直接访问,可通过调用基类方法 35 if(temp > 12) 36 { 37 temp -= 12; 38 } 39 return temp; 40 }
<5> main.cpp
1 #include <iostream> 2 3 #include "CurrTime.h" 4 5 int main() 6 { 7 c_CurrTime CurrTime; 8 //CurrTime.init_Time(); 9 std::cout<< CurrTime.getHour() << ":" << CurrTime.getMinute() << ":" << CurrTime.getSecond() <<std::endl; 10 11 c_Time time = CurrTime; //也可写成 c_Time time(CurrTime); 12 std::cout<< time.getHour() << ":" << time.getMinute() << ":" << time.getSecond() <<std::endl; 13 14 c_Time* p = &CurrTime; //基类的指针只能调用基类的方法,而不能调用派生类方法 15 std::cout<< p -> getHour() << ":" << p -> getMinute() << ":" << p -> getSecond() <<std::endl; 16 17 return 0; 18 }
编译、链接、执行
阅读全文
0 0
- c++入门(类和对象and继承for系统时间显示)
- c++入门(类和对象and继承)
- C显示系统时间
- c语言显示系统时间
- C++:类与对象入门 Intro to Class and Object
- Objective-C入门03:类和对象
- 对象和类继承
- 类和对象 -----继承
- 类和对象:继承
- 显示当前系统时间(Linux C++)
- C#面向对象(OOP)入门—第二天—多态和继承(继承)
- 类和对象(时间)
- 继承Binder类绑定服务显示时间
- C语言中的面向对象(1)-类模拟和多态,继承(转载)
- MFC状态栏编程(显示系统时间和进度条)
- 状态栏编程(显示系统时间和进度条)
- 使用DOS维护系统 (显示时间和日期)
- (Effective C++)第六章 继承与面向对象(Inheritance and Object-Oriented Design)
- Target Sum
- ubuntu下在线/离线安装chrome浏览器
- 10、实现迭代服务器端和客户端
- 设置eclipse中python脚本的编码格式
- Django 博客开发教程 10
- c++入门(类和对象and继承for系统时间显示)
- 面试:----技术分析之Struts2的拦截器技术
- 总结《Ray Tracing in One Weekend》
- 11、socket缓冲区以及阻塞模式
- Android开发(三、清单文件AndroidManifest)
- inet_pton、inet_ntop函数
- 1、mybatis基础知识
- mybatis的逆向工程
- mybatis对象之间映射关系以及数据库表建立的时候外键的添加