encapsulation

When a single variable cannot do the job of describing a requirement, the structure type solves the problem. Multiple types can be packaged together to form new types. This is the concept encapsulated in C. The new type, however, does not include operations on data classes. All operations are encapsulated by functions.

The combination of a set of data variables to form a structure-a preliminary encapsulation. C encapsulation style, data put together find package Struct, and then pass the data to the behavior in the form of reference or pointer.

#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; }Copy the code

C++ believes that C encapsulation is incomplete.

1. There is no separation of data and behavior. 2. There is no permission control. Encapsulation features: internal data open, logical abstraction, external interface. C++ adds permission controls, private protected public data and behavior together, open internally, and provide interfaces externally. Procedure: Class -> Class object -> Object. Object call behavior completes the requirement.

Class Date {protect:// attribute, member variable int year; int month; int day; Public :// behavior, member 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; }Copy the code
Class Date {protect:// attribute, member variable int year; int month; int day; Public :// behavior, member function void init(); void print(); } void Date:: init()// prevent collisions {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; }Copy the code

C++ multifile management: 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
Copy the code

Class file: date.cpp

#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; }}Copy the code

main.cpp

#include<iostrem>
#include "date.h"
using namespace std;
using namespace Space;
int main()
{
    Date d;
    d.init();
    d.print();

    return 0;
}
Copy the code

Implementation of application stack: 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, 0102, 4); } 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; }Copy the code

C++:

#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; }Copy the code

Stack. H (class file)

#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
Copy the code

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];
}
Copy the code

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; }Copy the code

C++ linked list implementation:

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; }Copy the code