封裝
當單一變量沒法完成描述需求的時候,結構體類型解決了這一問題。能夠將多個類型打包成一體,造成新的類型。這是 c 語言中封裝的概念。可是,新類型並不包含,對數據類的操做。所的有操做都是經過函數的方式,去其進行封裝。ios
對一組數據變量組進行結合造成結構體--初步的封裝。 C語言的封裝風格,數據放到一塊兒找包Struct,而後把數據以引用或者指針的方式傳給行爲。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<<"year month day"<<endl; 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<<"leap year"<<endl; else cout<<"not leap year"<<endl; return 0; }
C++ 認爲c封裝不完全。函數
1.數據和行爲沒有分離。 2.沒有權限控制。 封裝的特色:對內數據開放,邏輯抽象,對外提供接口。 C++增長權限控制,private protected public 數據和行爲在一塊兒,對內開放,對外提供接口。 過程:類 -> 類對象 ->對象。對象調用行爲完成需求。spa
class Date { protect://屬性,成員變量 int year; int month; int day; public://行爲,成員函數 void init() { cin>>year; cin>>month; cin>>day; } void print() { cout<<"year:"<<"month:"<<"day"<<endl; cout<<year<<":"<<month<<":"<<day<<endl; } } int main() { Date d; d.init(); d.print(); return 0; }
class Date { protect://屬性,成員變量 int year; int month; int day; public://行爲,成員函數 void init(); void print(); } void Date:: init()//防止衝突 { cin>>year; cin>>month; cin>>day; } void Date:: print() { cout<<"year:"<<"month:"<<"day"<<endl; cout<<year<<":"<<month<<":"<<day<<endl; } int main() { Date d; d.init(); d.print(); return 0; }
c++多文件中的管理: date.h:指針
#ifndef DATE_H #define DATE_H using namespace Spac { class Date { private: int year; int month; int day; public: void init(); void print(); int getYear(); bool isLeapYear(); } } #endif
class文件:date.cppcode
#include<iostream> #include "date.h" using namespace std; using namespace Space { void Date:: init() { cin>>year; cin>>month; cin>>day; } void Date:: print() { cout<<"year:"<<"month:"<<"day"<<endl; cout<<year<<":"<<month<<":"<<day<<endl; } }
main.cpp對象
#include<iostrem> #include "date.h" using namespace std; using namespace Space; int main() { Date d; d.init(); d.print(); return 0; }
應用棧的實現: C:接口
#include<stdio.h> #include<stdlib.h> typedef struct stack { char space[1024]; int top; }Stack; void init(Stack *s) { s->top = 0; memset(s->space,0,1024); } int isEmpty(Stack * s) { return s->top == 0; } int isFull(Stack *s) { return s->top == 1024; } char pop(Stack *s) { return s.space[--s.top]; } void push(Stack * s,char c) { s.space[s.top++] = c; } int main() { Stack st; init(&st); if(!isFull) push(&st,'a'); if(!isFull) push(&st,'b'); if(!isFull) push(&st,'c'); if(!isFull) push(&st,'d'); if(!isFull) push(&st,'e'); while(!isEmpty(&st)) printf("%c\n",pop(&st)); return 0; }
C++:ci
#include <iostream> #include <stdlib.h> #include <stdio.h> #include <string.h> using namespace std; class Stack { public: void init(); bool isEmpty(); bool isFull(); void push(int data); int pop(); private: int space[1024]; int top; }; void Stack::init() { memset(space,0,sizeof(space)); top = 0; } bool Stack::isEmpty() { return top == 0; } bool Stack::isFull() { return top == 1024; } void Stack::push(int data) { space[top++] = data; } int Stack::pop() { return space[--top]; } int main() { Stack s; s.init(); if(!s.isFull()) s.push(10); if(!s.isFull()) s.push(20); if(!s.isFull()) s.push(30); if(!s.isFull()) s.push(40); if(!s.isFull()) s.push(50); while(!s.isEmpty()) cout<<s.pop()<<endl; return 0; }
個文件分離管理: stack.h(class文件)rem
#ifndef STACK_H #define class Stack { public: void init(); bool isEmpty(); bool isFull(); void push(int data); int pop(); private: int space[1024]; int top; }; #endif
stack.cpp:
#include<iostream> #inlcude "stack.h" #include<stdlib.h> #include<string.h> void Stack::init() { memset(space,0,sizeof(space)); top = 0; } bool Stack::isEmpty() { return top == 0; } bool Stack::isFull() { return top == 1024; } void Stack::push(int data) { space[top++] = data; } int Stack::pop() { return space[--top]; }
main.cpp
#include<iostream> #include "stack.h" using namespace std; int main() { Stack s; s.init(); for(char v = "a";!st.isFull()&& v != 'z'+1;v++) { st.push(v); } while(!s.isEmpty()) cout<<s.pop()<<endl; return 0; }
C++鏈表的實現:
class List { private: Node * head; public: void initList(); void insertList(); void traverseList(); void deleteNode(Node * pfind); Node * searchList(int find); void sortList(); void destroy(); }; void List:: initList() { head = new Node; head->next = NULL; } void List:: insertList(int d) { Node * cur = new Node; cur->data = d; cur->next = head->next; head->next = cur; } void List:: traverseList() { Node * ph = head->next; while(ph != NULL) { cout<<ph->data<endl; ph = ph->next; } } void List:: deleteNode(Node * pfind); Node * List:: searchList(int find); void List:: sortList(); void List:: destroy(); int main() { List list; list.init(); for(int i = 0;i < 10;i++) { list.insertList(i); } list.traverseList(); return 0; }