Small instances of the C ++ template class (2)

Source: Internet
Author: User
# Include <iostream> # include <conio. h> using namespace STD; const int size = 8; Template <class T> class smemory {// defines the class template smemory t data [size]; // The type is t, the data [] array with a length of size is the data member int count; public: smemory () {COUNT = 0;} void mput (t x); // mput () the type of function parameter X is t mget (); // It is declared that the type of return value is T. The member function mget ()}; Template <class T> void smemory <t> :: mput (t x) // defines the member function mput (). The parameter type of the function is t, this function is used to assign values to each element in the data array of data members {If (COUNT = 8) {cout <"memory is full"; return;} data [count] = X; count ++;} template <class T> T smemory <t>: mget () // defines the member function mget (). The return type of the function is T, this function is used to retrieve each element of the data array of the data member {If (COUNT = 0) {cout <"memory is empty"; return 0 ;}count --; return data [count];} int main () {smemory <int> mo1; int I; char CH = 'a'; // instantiate smemory, and create the object mo1 smemory <char> mo2; // instantiate the smemory and create the object mo2 for (I = 0; I <8; I ++) {mo1.mput (I ); // call the member function mput () mo2.mput (CH); ch ++; // call the member function mput ()} cout <"Get mo1 => "; for (I = 0; I <8; I ++) cout <mo1.mget (); // call the member function mget () cout <"\ ngET mo2 => "; for (I = 0; I <8; I ++) cout <mo2.mget (); // call the member function mget () getch ();}

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.