免费爱碰视频在线观看,九九精品国产屋,欧美亚洲尤物久久精品,1024在线观看视频亚洲

      C++認為C的東西都可以封裝一下,實現(xiàn)定制、隱藏、易用、安全

      C++的哲學(xué)就是把所有東西都封裝一下,提供訪問控制(安全控制),提供更多的方法和功能。這種封裝也可以稱為抽象,通過更高一層的抽象來實現(xiàn)隱藏和安全。

      0 結(jié)構(gòu)體封裝和控制:訪問控制+函數(shù)指針+構(gòu)造析構(gòu)

      1 原生指針封裝和定制:迭代器、智能指針

      2 C風(fēng)格字符串封裝和定制:string類

      3 函數(shù)封裝和定制:函數(shù)對象

      4 原生數(shù)組封裝和控制:array類、vector類

      5 棧封裝和控制:stack類

      6 浮點數(shù)也可以封裝和定制,可以定義一下浮點數(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中容器的迭代器的底層實現(xiàn)機制

      C++ 從使用指針的迭代操作到使用模板技術(shù)的指針類(迭代器)

      詳解C++四個智能指針的使用和實現(xiàn)

      C++ 類封裝如何提升安全和可維護性,以智能指針封裝裸指針為例

      C++ 引用計數(shù)與shared_ptr智能指針(以實現(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ù)對象

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

      鄭重聲明:本文內(nèi)容及圖片均整理自互聯(lián)網(wǎng),不代表本站立場,版權(quán)歸原作者所有,如有侵權(quán)請聯(lián)系管理員(admin#wlmqw.com)刪除。
      用戶投稿
      上一篇 2022年6月20日 13:11
      下一篇 2022年6月20日 13:12

      相關(guān)推薦

      • 泰拉瑞亞烏龜套(泰拉瑞亞烏龜套和神圣套哪個好)

        今天小編給各位分享泰拉瑞亞烏龜套的知識,其中也會對泰拉瑞亞烏龜套和神圣套哪個好進行解釋,如果能碰巧解決你現(xiàn)在面臨的問題,別忘了關(guān)注本站,現(xiàn)在開始吧! 泰拉瑞亞甲蟲套和烏龜套哪個好 …

        2022年11月26日
      • 存儲過程語法(sql server存儲過程語法)

        今天小編給各位分享存儲過程語法的知識,其中也會對sql server存儲過程語法進行解釋,如果能碰巧解決你現(xiàn)在面臨的問題,別忘了關(guān)注本站,現(xiàn)在開始吧! oracle存儲過程基本語法…

        2022年11月26日
      • 前三季度,市場規(guī)模超過五萬億元 信息消費展現(xiàn)蓬勃生機

        家居企業(yè)個性化全屋定制系統(tǒng),備受消費者青睞;主打?qū)I(yè)電競的新款高性能便攜式計算機,銷量表現(xiàn)創(chuàng)新高;物流企業(yè)推出數(shù)智化供應(yīng)鏈興農(nóng)服務(wù)項目,助力優(yōu)質(zhì)農(nóng)產(chǎn)品出深山…… 不久前,工信部發(fā)布…

        2022年11月24日
      • 園屬于什么結(jié)構(gòu)(園的結(jié)構(gòu)和部首)

        園 yuán:全包圍結(jié)構(gòu),平穩(wěn)端正中稍帶左收右展。 外部“口” 體態(tài)端莊,稍抗肩,稍帶左輕右重。左豎起筆稍抖,豎身勿重,稍左斜,垂露收筆;第二筆橫折壓著左豎起筆,橫畫稍抗肩,不要重…

        2022年11月24日
      • 三部門組織開展2022年智慧健康養(yǎng)老產(chǎn)品及服務(wù)推廣目錄申報工作

        為促進典型智慧健康養(yǎng)老產(chǎn)品和服務(wù)推廣應(yīng)用,推動智慧健康養(yǎng)老產(chǎn)業(yè)發(fā)展,工業(yè)和信息化部辦公廳、民政部辦公廳、國家衛(wèi)生健康委辦公廳近日印發(fā)關(guān)于組織開展2022年智慧健康養(yǎng)老產(chǎn)品及服務(wù)推廣…

        2022年11月23日
      • 今天武漢封控小區(qū)名單最新(武漢今天封控小區(qū)名單)

        當(dāng)前許多省市的疫情持續(xù)不斷,個別地方的疫情更是有擴散蔓延趨勢。據(jù)悉,近日武漢疫情波及范圍也越來越廣了 ,截至2022年11月23日9:00,武漢市有高低風(fēng)險地區(qū),在經(jīng)開區(qū)、黃陂區(qū)、…

        2022年11月23日
      • 小米無線開關(guān)藍牙版開售:一粒電池用兩年 到手價39元

        小米無線開關(guān)藍牙版已在京東商城開售,到手價僅39元,號稱“一物三用”。 小米無線開關(guān)藍牙版外殼通過72小時UV測試不易發(fā)黃,底部升級防滑硅膠墊,配備3M無痕膠貼,官方稱,一粒電池可…

        2022年11月23日
      • 華為兒童手表4X系列系統(tǒng)更新 支持健康碼截圖等功能

        華為官方宣布,華為兒童手表 4X 系列現(xiàn)已支持健康碼截圖功能! 據(jù)介紹,在最新的1.3.0.198 版本中,華為兒童手表 4X、華為兒童手表 4X 新耀款已經(jīng)可以通過家長手機端的【…

        2022年11月21日
      • 簡短而有吸引力的標題(100個超強吸引人的標題)

        眾所周知,在創(chuàng)作過程中,我們的標題非常重要。 無論是在郵件中、軟文中、還是百度競價的創(chuàng)意,還是圖文信息的標題…… 讀者第一眼看到的是我們的標題,如果我們的標題沒有吸引力的話,那么讀…

        2022年11月18日
      • 網(wǎng)易Q3財報:營收244億,激發(fā)數(shù)字生產(chǎn)力服務(wù)文化及社會生產(chǎn)

        11月17日,網(wǎng)易公布2022年第三季度財報。本季度,網(wǎng)易聚焦技術(shù)研發(fā),持續(xù)激發(fā)數(shù)字生產(chǎn)力,業(yè)務(wù)獲得穩(wěn)步發(fā)展,加快數(shù)字普惠跨產(chǎn)業(yè)賦能。 三季度,網(wǎng)易凈收入244億元,歸屬于公司股東…

        2022年11月18日

      聯(lián)系我們

      聯(lián)系郵箱:admin#wlmqw.com
      工作時間:周一至周五,10:30-18:30,節(jié)假日休息