C++的哲學(xué)就是把所有東西都封裝一下,提供訪問控制(安全控制),提供更多的方法和功能。這種封裝也可以稱為抽象,通過更高一層的抽象來實(shí)現(xiàn)隱藏和安全。
0 結(jié)構(gòu)體封裝和控制:訪問控制+函數(shù)指針+構(gòu)造析構(gòu)
1 原生指針封裝和定制:迭代器、智能指針
2 C風(fēng)格字符串封裝和定制:string類
3 函數(shù)封裝和定制:函數(shù)對(duì)象
4 原生數(shù)組封裝和控制:array類、vector類
5 棧封裝和控制:stack類
6 浮點(diǎn)數(shù)也可以封裝和定制,可以定義一下浮點(diǎn)數(shù)比較方式
7 整數(shù)也可以封裝和定制,可以判斷一下是否上溢和下溢
8 封裝malloc+構(gòu)造函數(shù):new
9 FILE封裝一下:stream類
0 結(jié)構(gòu)體封裝和控制:訪問控制與構(gòu)造析構(gòu)
參考:
編程基礎(chǔ):從面向過程到面向?qū)ο?,從結(jié)構(gòu)體到類
C++的逐層抽象:從結(jié)構(gòu)體到類、模板
1 原生指針封裝和定制:迭代器、智能指針
參考:
C++ 簡單模仿STL中容器的迭代器的底層實(shí)現(xiàn)機(jī)制
C++ 從使用指針的迭代操作到使用模板技術(shù)的指針類(迭代器)
詳解C++四個(gè)智能指針的使用和實(shí)現(xiàn)
C++ 類封裝如何提升安全和可維護(hù)性,以智能指針封裝裸指針為例
C++ 引用計(jì)數(shù)與shared_ptr智能指針(以實(shí)現(xiàn)String類為例)
2 C風(fēng)格字符串封裝和定制:string類
#include #include class String{public: String(const char* cstr = 0); String(const String& str); String& operator=(const String& str); ~String(){ delete[] m_data; } char* get_c_str() const{ return m_data; }private: char* m_data;};String::String(const char* cstr){ if(cstr) { m_data = new char[strlen(cstr)+1]; strcpy(m_data,cstr); } else { m_data = new char[1]; *m_data = ”; }}String::String(const String& str){ m_data = new char[strlen(str.m_data)+1]; strcpy(m_data,str.m_data);}String& String::operator=(const String& str){ if(this == &str) return *this; delete[] m_data; m_data = new char[strlen(str.m_data)+1]; strcpy(m_data,str.m_data); return *this;}int main(){ String str(“abc”); String str2(str); String str3 = str2; printf(“%s”,str3.get_c_str()); return 0;}
3 函數(shù)封裝和定制:函數(shù)對(duì)象
#include class Fib {public: Fib() : a0_(1), a1_(1) {} int operator()() { int temp = a0_; a0_ = a1_; a1_ = temp + a0_; return temp; }private: int a0_, a1_;};int main(){ Fib fib; for(int i=1;i<50;i++) printf("%2d %d",i,fib()); getchar();}
4 數(shù)組封裝和控制:array類、vector類
#ifndef INTARRAY_H#define INTARRAY_H#include // for assert()class IntArray{private: int m_length{}; int* m_data{};public: IntArray() = default; IntArray(int length): m_length{ length } { assert(length >= 0); if (length > 0) m_data = new int[length]{}; }IntArray(std::initializer_list list) // allow IntArray to be initialized via list initialization: IntArray(static_cast(list.size())) // use delegating constructor to set up initial array{// Now initialize our array from the listint count{ 0 };for (auto element : list){m_data[count] = element;++count;}} ~IntArray() { delete[] m_data; // we don’t need to set m_data to null or m_length to 0 here, since the object will be destroyed immediately after this function anyway } void erase() { delete[] m_data; // We need to make sure we set m_data to nullptr here, otherwise it will // be left pointing at deallocated memory! m_data = nullptr; m_length = 0; } int& operator[](int index) { assert(index >= 0 && index < m_length); return m_data[index]; } // reallocate resizes the array. Any existing elements will be destroyed. This function operates quickly. void reallocate(int newLength) { // First we delete any existing elements erase(); // If our array is going to be empty now, return here if (newLength m_length) ? m_length : newLength }; // Now copy the elements one by one for (int index{ 0 }; index = 0 && index <= m_length); // First create a new array one element larger than the old array int* data{ new int[m_length+1] }; // Copy all of the elements up to the index for (int before{ 0 }; before < index; ++before) data[before] = m_data[before]; // Insert our new element into the new array data[index] = value; // Copy all of the values after the inserted element for (int after{ index }; after = 0 && index < m_length); // If we're removing the last element in the array, we can just erase the array and return early if (m_length == 1) { erase(); return; } // First create a new array one element smaller than the old array int* data{ new int[m_length-1] }; // Copy all of the elements up to the index for (int before{ 0 }; before < index; ++before) data[before] = m_data[before]; // Copy all of the values after the removed element for (int after{ index+1 }; after < m_length; ++after) data[after-1] = m_data[after]; // Finally, delete the old array, and use the new array instead delete[] m_data; m_data = data; –m_length; } // A couple of additional functions just for convenience void insertAtBeginning(int value) { insertBefore(value, 0); } void insertAtEnd(int value) { insertBefore(value, m_length); } int getLength() const { return m_length; }};#endif#include #include "IntArray.h"int main(){ // Declare an array with 10 elements IntArray array(10); // Fill the array with numbers 1 through 10 for (int i{ 0 }; i<10; ++i) array[i] = i+1; // Resize the array to 8 elements array.resize(8); // Insert the number 20 before element with index 5 array.insertBefore(20, 5); // Remove the element with index 3 array.remove(3); // Add 30 and 40 to the end and beginning array.insertAtEnd(30); array.insertAtBeginning(40); // Print out all the numbers for (int i{ 0 }; i
5 棧封裝和定制:stack類
#include #include #include #include #include using namespace std;template class Stack { private: vector elems; // elements public: void push(T const&); // push element void pop(); // pop element T top() const; // return top element bool empty() const { // return true if empty. return elems.empty(); } }; template void Stack::push (T const& elem) { // append copy of passed element elems.push_back(elem); } template void Stack::pop () { if (elems.empty()) { throw out_of_range(“Stack::pop(): empty stack”); } // remove last element elems.pop_back(); } template T Stack::top () const { if (elems.empty()) { throw out_of_range(“Stack::top(): empty stack”); } // return copy of last element return elems.back(); } int main() { try { Stack intStack; // stack of ints Stack stringStack; // stack of strings // manipulate int stack intStack.push(7); cout << intStack.top() <<endl; // manipulate string stack stringStack.push("hello"); cout << stringStack.top() << std::endl; stringStack.pop(); stringStack.pop(); } catch (exception const& ex) { cerr << "Exception: " << ex.what() <<endl; return -1; } }
ref
C++ 包裝裸指針、裸數(shù)組、字符串成智能指針、array類和string類
-End-