Example of "thinkinginc++" 35, overloading

Source: Internet
Author: User

Header file

/*** book: "thinkinginc++" * Function: Example of overloading * Time: August 28, 2014 16:03:37* Author: cutter_point*/#ifndef stash3_h_included#define Stash3_h _includedclass stash{    int size;   The number of bytes used to represent the data to be saved    int quantity;   Total number of memory blocks    int next;       The number of objects already stored, used to indicate where the next empty place    //Dynamic request array size is byte    unsigned char* storage;    void inflate (int increase); This is only called by the Add function and is part of the internal implementation, which is used to increase the memory public:    Stash (int size);    0 The number of starting memory blocks    Stash (int size, int initquantity);  Function overload    ~stash ();    int Add (void* element);    void* Fetch (int index);    int count ();}; #endif//stash3_h_included



Definition file

/*** book: "thinkinginc++" * Function: example of overloading, definition of header file * Time: August 28, 2014 16:03:42* Author: cutter_point*/#include "Stash3.h" #include ".    /require.h "#include <iostream> #include <cassert>using namespace std;const int increment=100;   The basic number of memory increases per/*int size;   Represents the number of bytes occupied by an object int quantity;       Total number of memory blocks int next; The number of objects already stored, used to indicate where the next empty place//Dynamic request array size is byte void inflate (int increase);    This is only called by the Add function, which is part of the internal implementation to increase the memory public part: Stash (int sz);  0 The number of starting memory blocks stash (int sz, int initquantity); Function Overload ~stash (), int add (void* element), void* Fetch (int index), int count (), *///void inflate (int increase);//This is only called by the Add function    , which is part of the internal implementation, is used to increase memory void stash::inflate (int increase)//increase memory size, increasing the allocation size of memory {assert (increase >= 0);    if (Increase = = 0) return;  int newquantity=quantity+increase;  This is the new size, the old addition to increase the int newbytes=newquantity*size;    Bytes (Number of memory blocks * size of each block) int oldbytes=quantity*size;   unsigned char* b=new unsigned char[newbytes];        Newbytes bytes corresponding to the space//bar old space data put on the new up for (int i=0; i < oldbytes; ++i)    B[i]=storage[i];    Reclaim the old space Delete [] storage; Bar storage points to the new spatial location,!!!!    August 12, 2014 23:18:33 here is the wrong check half day storage=b; Get a new total number of memory blocks quantity=newquantity;}    Stash (int size);    0 The number of starting memory blocks Stash::stash (int sz) {size=sz;    quantity=0;    next=0; storage=0;}  Stash (int sz, int initquantity);    function overload stash::stash (int sz, int initquantity) {size=sz; quantity=0;    Here is still 0, because there is no space allocated to him next=0;    storage=0; Allocate space Inflate (initquantity) by calling functions;} ~stash ();        Stash::~stash () {if (storage! = 0) {cout<< "Freeing storage" <<endl;    delete []storage; }}//int Add (void* Element), int stash::add (void* element) {if (next >= quantity)//Starting free place is larger than total memory inflate (incre ment);   Add memory int startbytes=next*size;    All already occupied memory unsigned char* e= (unsigned char*) element; for (int i=0; i < size; ++i) {storage[startbytes+i]=e[i];//Add to existing back} next++;    Represents the location of the first empty memory return (NEXT-1); Returns the number of objects}//void* fetch (int index); VOid* stash::fetch (int index) {require (0 <= index, "stash::fetch (-) index");   if (index >= next) return 0;  No corresponding value return & (Storage[index*size]);    Returns an object of index}//int count (), int stash::count () {return next; The total number of memory data}




Test file

/*** book: "thinkinginc++" * Function: Test reload * Time: August 28, 2014 16:03:48* Author: cutter_point*/#include "Stash3.cpp" #include <fstream        > #include <string>int main () {Stash intstash (sizeof (int)); cout<< "!"    <<endl;   for (int i=0; i <; ++i) Intstash.add (&i); The data is brought into the object//void* stash::fetch (int index) for (int j=0; j < Intstash.count (); ++j) cout<< "Intstash.    Fetch ("<<j<<") = "<<* (int*) Intstash.fetch (j) <<endl;    const int bufsize=80;   Stash Stringstash (sizeof (char) *bufsize, 100);    overloaded function Ifstream in ("Stash3Test.cpp");    Assure (in, "Stash3Test.cpp");    String line;   while (Getline (on, line)) Stringstash.add ((char*) line.c_str ());    Bar string type converted to char* int k=0;    char* CP; void* stash::fetch (int index) while ((Cp= (char*) Stringstash.fetch (k++))! = 0) cout<< "Stringstash.fetch (" &    lt;<k<< ") =" <<cp<<endl; return 0;}





Example of "thinkinginc++" 35, overloading

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.