"Thinkinginc++" 38, select overload or Default parameter

Source: Internet
Author: User

Header file

/*** book: "thinkinginc++" * Function: Select overload or Default parameter, header file * Time: September 6, 2014 14:54:28* Author: cutter_point*/#ifndef  mem_h_included# Define  mem_h_includedtypedef unsigned char byte;class mem{    byte* MEM;    int size;    void ensureminsize (int minSize);    member functions to increase the size of the memory block public:    Mem ();    Mem (int sz);    The second constructor ensures that the Mem object has a SZ-sized storage area of    ~mem ();         destructor frees space    int msize ();    Tells us how many bytes are still in the current Mem object    byte* pointer ();    byte* pointer (int minSize); Point A pointer to a piece of memory. The memory is at least minsize large}; #endif//mem_h_included

Definition file

/*** book: "thinkinginc++" * Function: Select overload or Default parameter, declare file * Time: September 6, 2014 14:54:58* Author: cutter_point*/#include "Mem.h" #include <    Cstring>using namespace Std;/*class mem{byte* Mem;    int size;    void ensureminsize (int minSize);    member functions to increase the size of the memory block Public:mem ();    Mem (int sz);         The second constructor ensures that the Mem object has a SZ-sized storage area of ~mem ();    destructor Frees space int msize ();    Tells us how many bytes are still in the current Mem object byte* pointer (); byte* pointer (int minSize); Point A pointer to a piece of memory.    The memory has at least minSize large};*///void ensureminsize (int minSize);  The member function to increase the size of the memory block void mem::ensureminsize (int minSize) {if (Size < minSize)//determines that only the smallest size given will be re-applied for space {byte*     Newmem=new Byte[minsize]; Apply for a new space/* Today to do a problem card in the use of memset function, originally thought int a[100];        Memset (A, max,sizeof (a));    is to assign all the array A to max, and now we know that his fill is in bytes and is typically used to assign values to the initial value of a character variable.        *///starting from newmem+size, altogether minsize-size all resets to 0 memset (newmem+size, 0, minsize-size);        MEMCPY provides replication of general memory.        That is, memcpy has no limitations on what needs to be replicated and is therefore more widely used. memcpy (Newmem, mem, size);        The contents of the mem are copied to Newmem, and the length is size//Reclaim space memory Delete []mem; Mem=newmem;   The new address is assigned to the object size=minsize; The new size gives the object}}//mem ();    Mem::mem () {mem=0; size=0;}    Mem (int sz);    The second constructor ensures that the Mem object has a SZ-sized storage area of mem::mem (int sz) {mem=0;    size=0;  Ensureminsize (SZ);         The initial space is set to SZ length}//~mem (); destructor frees Space Mem::~mem () {delete []mem;}    int msize (); Tells us how many bytes in the current Mem object are still int mem::msize () {return size;}  byte* pointer (); Point A pointer to a piece of memory. The memory has at least minsize large byte* Mem::p ointer () {return Mem;}    byte* pointer (int minSize); byte* Mem::p ointer (int minSize) {ensureminsize (minSize); return mem;}

Test the main function

/*** book: "thinkinginc++" * Function: Select the overload or default parameter, test the file, create a string class * as a tool for other classes to simplify their memory management (for example, it can also hide the more complex memory management details provided by the operating system *) *    Date: September 6, 2014 14:55:32* Author: cutter_point*//*class mem{byte* Mem;    int size;    void ensureminsize (int minSize);    member functions to increase the size of the memory block Public:mem ();    Mem (int sz);         The second constructor ensures that the Mem object has a SZ-sized storage area of ~mem ();    destructor Frees space int msize ();    Tells us how many bytes are still in the current Mem object byte* pointer (); byte* pointer (int minSize); Point A pointer to a piece of memory. This memory has at least minsize large};*/#include "Mem.cpp" #include <cstring> #include <iostream>using namespace Std;class   mystring{mem* buf;    Simplifies memory management public:mystring ();    MyString (char* str);    Heavy-duty ~mystring ();    Link two string void concat (char* str); void print (ostream& os);};/ /mystring (); Mystring::mystring () {buf=0;}    MyString (char* str);    Overloaded mystring::mystring (char* str)//with STR constructs a string type {buf=new Mem (strlen (str) +1);//Create a memory space for STR//construct a string type with str strcpy ((char*) buf->pointer (), str); Copy the past}//~mystring (); MyString::~mystring () {delete buf;//Reclaim Space memory}//link two string//void concat (char* str); void Mystring::concat (char* str) {if (    !BUF)//If the memory of BUF (pointer to Mem) is 0, create a memory space for him buf=new Mem; The transverse link string, pointer returns a pointer to a memory block with at least Buf->mszie () +strlen (str) +1 strcat ((char*) Buf->pointer (buf->msize () + strlen (str) +1), str);} void print (ostream& os), void MyString::p rint (ostream& os) {if (!BUF) return;//when buf=0, direct end os<   <buf->pointer () <<endl;    Not 0, then the output content}int main () {MyString s ("My test string");    S.print (cout);    S.concat ("This is Cutter_point");    S.print (cout);    MyString S2;    S2.concat ("Use default construct by Cutter_point");    S2.print (cout); return 0;}



"Thinkinginc++" 38, select overload or Default parameter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.