C++学习笔记之封装程序记录
2019-03-07 21:28
351 查看
[code]//用C语言进行日期编写 #include<iostream> using namespace std; struct Date //数据的封装 { int year; int month; int day; }; /*对数据进行处理*/ void init(Date &d) { cout << "year month day" << endl; cin >> d.year >> d.month >> d.day; } void print(Date &d) { cout << d.year << "/" << d.month << "/" << d.day << endl; } bool isLeapYear(Date &d) { if (d.year % 4 == 0 && d.year % 100 != 0 || d.year % 400 == 0) return true; else return false; } int main() { Date d; init(d); print(d); if (isLeapYear(d)) cout << d.year << "is leap year" << endl; else cout << d.year << "is not leap year" << endl; return 0; }
[code]//用C++语言类编写日期 #include<iostream> using namespace std; class Date { public: void init() { cout << "year month day" << endl; cin >> year >> month >> day; } void show() { cout << year << "/" << month << "/" << day << endl; } bool IsleapYear() { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) return true; else return false; } private: int year; int month; int day; }; int main() { Date d; d.init(); d.show(); if (d.IsleapYear()) cout << "is leap year" << endl; else cout << "is not leap year" << endl; return 0; }
[code]//另一种用C++语言表达日期方式, 该写法便于进行多文件编程 #include<iostream> using namespace std; class Date //类名相当于命名空间 { public: void init(); void show(); bool IsleapYear(); private: int year; int month; int day; }; void Date::init() { cout << "year month day" << endl; cin >> year >> month >> day; } void Date::show() { cout << year << "/" << month << "/" << day << endl; } bool Date::IsleapYear() { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) return true; else return false; } int main() { Date d; d.init(); d.show(); if (d.IsleapYear()) cout << "is leap year" << endl; else cout << "is not leap year" << endl; return 0; }
[code]//对列表进行封装 #include<iostream> using namespace std; typedef struct node { int data; struct node * next; }Node; class List { public: void createlist(); void insertList(int data); void traverseList(); private: Node * head; }; void List::createlist() { head = new Node; head->next = NULL; } void List::insertList(int data) { Node * cur = new node; cur->data = data; cur->next = head->next; head->next = cur; } void List::traverseList() { Node * t = head->next; while (t) { cout << t->data << endl; t = t->next; } } int main() { List L; L.createlist(); L.insertList(1); L.insertList(2); L.insertList(3); L.insertList(4); L.insertList(5); L.traverseList(); return 0; }
[code]//C语言应用结构体,函数构造栈 #include<iostream> using namespace std; struct Stack { char buf[1024]; int top; }; void init(Stack &s) { memset(s.buf, 0, 1024); s.top = 0; } void push(Stack &s,char ch) { s.buf[s.top++] = ch; } char pop(Stack &s) { return s.buf[--s.top]; } bool IsEmpty(Stack &s) { return s.top == 0; } bool IsFull(Stack &s) { return s.top == 1024; } int main() { Stack s; init(s); if (!IsFull(s)) push(s, 'd'); if (!IsFull(s)) push(s, 'c'); if (!IsFull(s)) push(s, 'b'); if (!IsFull(s)) push(s, 'a'); while (!IsEmpty(s)) cout << pop(s) << endl; return 0; }
[code]//用C++类封装栈 #include<iostream> using namespace std; class Stack { public: void init() { memset(buf, 0, 1024); top = 0; } void push(char ch) { buf[top++] = ch; } char pop() { return buf[--top]; } bool IsEmpty() { return top == 0; } bool IsFull() { return top == 1024; } private: char buf[1024]; int top = 0; }; int main() { Stack s; s.init(); if (!s.IsFull()) s.push('e'); if (!s.IsFull()) s.push('f'); if (!s.IsFull()) s.push('g'); if (!s.IsFull()) s.push('h'); while( !s.IsEmpty()) cout << s.pop()<< endl; return 0; }
相关文章推荐
- c++学习笔记(1)——环境搭建、经典教程推荐及 Visual Studio 2005/2008中如何编译和运行C++程序
- C++ primer第二次阅读学习笔记(第17章:用于大型程序的工具:命名空间)
- 传智播客JDBC_规范和封装jdbc程序代码学习笔记
- PKU C++程序设计实习 学习笔记1
- C++学习笔记——1.创建基本程序
- 高质量C++程序指南 学习笔记
- 【C++ 学习笔记小程序05】 const限定符 浮点数 算数运算符
- onvif学习笔记7:一个C++封装的onvif代码的阅读笔记
- c++学习笔记之封装篇(上)
- C++学习笔记10——类和封装
- 个人C++学习笔记记录(一)
- C++数据的封装和类体内定义成员函数的学习笔记
- 【C++学习笔记】03_C++程序结构
- 菜鸟C++精髓学习笔记--C++程序内部执行过程(预处理、编译、链接过程的作用)
- [LUA学习笔记01]用VS2003(C++)编译Hello Lua程序
- 慕课学习C++笔记02-封装(上)
- 学习笔记之深入浅出MFC 第8章 C++重要性质-----类封装
- C++学习之路指针记录笔记(一)
- 【C++】学习笔记草稿版系列8(封装)
- 【C++学习笔记】结构与函数:一个BMI计算小程序