C++类的定义
来源:互联网 发布:淘宝网三角梅哪家好 编辑:程序博客网 时间:2024/06/10 08:21
#include <iostream>#include <string.h>#include <time.h>using namespace std;class Date;class Person;class Date{private: int year,month,day;public: void setDate(int y,int m,int d) { year = y; month = m; day = d; } int getYear(){return year;} int getMonth(){return month;} int getDay(){return day;} bool isLeapYear(){return ((year % 400 == 0) ||(year % 4 == 0 && year %100 !=0));} void print(){cout<<year<<"."<<month<<"."<<day<<endl;}};//日期类class Person{private: char name[20]; char sex;//f为女性,m为男性,u为不知性别 char idno[20]; Date birthdate;public: void setName(char* s) { if(strlen(s)<=19) strcpy(name,s); else strcpy(name,"unknown"); } char* getName(){return name;} void setSex(char c) { if(c != 'f' && c!= 'm') { sex = 'u'; return; } sex = 'c'; } char getSex(){return sex;} void setIdno(char* id) { if(strlen(id)>=19) { strcpy(idno, "unknown"); return; } strcpy(idno,id); } char* getIdno(){return idno;} void setBrithdate(int year,int month,int day) { birthdate.setDate(year,month,day); } int getAge() { time_t ltime;//说明time_t结构变量ltime time(<ime);//取得当前时间 tm* today = localtime(<ime);//转换为本地时间 int ctyear = today->tm_year+1900;//取得当前年份 return ctyear - birthdate.getYear(); } void show() { cout<<(sex=='f'?"她是":"他是")<<getName()<<",身份证号为"<<getIdno()<<";"; cout<<"出生日期为"; birthdate.print(); cout<<";年龄为"<<getAge()<<endl; }};int main(){ Person a,b; a.setIdno("32011319004094814"); a.setName("汪涵"); a.setSex('m'); a.setBrithdate(1990,4,9); a.show(); b=a; b.show(); b.setIdno("320113198909024815"); b.setName("丽丽"); b.setSex('f'); b.setBrithdate(1989,9,2); b.show();}
阅读全文
0 0
- Objective-C 类的定义
- [C++]类的定义 class
- c++_2: 类的定义
- Object C 类的定义
- C 的类和类的定义
- 【C++】数组类的定义(类模板)
- (C++)定义类的拷贝构造函数
- Objective-c中类的定义
- Object C 类的定义和实现
- C++:类的定义及初始化问题
- object-c中类的定义
- object-c中类的定义
- Objective-C类的定义和实现
- <C++>9.类成员函数的定义
- c++/MFC类的声明和定义
- C指针的定义
- C 指针的定义
- Object-C 类定义
- xXx隐私助手1.0
- 查找输入整数二进制中1的个数
- sql手注实践之dvwa
- Pygame 中用于视频叠加图形的 pygame 对象。
- cpp学习总结
- C++类的定义
- 【JQuery】--选择器
- 算法模板之堆-优先队列
- 712. Minimum ASCII Delete Sum for Two Strings
- 「复制、拷贝、替身、软连接、硬连接」区别详解
- 编译原理学习笔记2
- 剑指offer — 数组中出现次数超过一半的数字
- Pygame 中用于表示图像的对象。
- 机房收费系统—调用Excel表