C/C++(C++封装)

封装

当单一变量无法完成描述需求的时候,结构体类型解决了这一问题。可以将多个类型打包成一体,形成新的类型。这是 c 语言中封装的概念。但是,新类型并不包含,对数据类的操作。所的有操作都是通过函数的方式,去其进行封装。

对一组数据变量组进行结合形成结构体--初步的封装。
C语言的封装风格,数据放到一起找包Struct,然后把数据以引用或者指针的方式传给行为。

#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 数据和行为在一起,对内开放,对外提供接口。
过程:类 -> 类对象 ->对象。对象调用行为完成需求。

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.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;
    }
}

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
",pop(&st));

    return 0;
}

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;
}

个文件分离管理:
stack.h(class文件)

#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;
}

原文地址:https://www.cnblogs.com/intelwisd/p/8512087.html