Homework 15 2016 6 2 templates

Source: Internet
Author: User

#include <iostream>
#include <cmath>
#include <cstdio>
using namespace Std;

Template <typename T, int capacity>

Class Array

{
Public
Array ();
~array ();
bool Empty ();
void push (T value);
T pop ();
int size ();
t& operator[] (int index);

Private
t* elements;
int num;
};


Class Arrayexception

{

Public
Arrayexception (const char *msg);
const char *what () const;
Private
const char *message;
};

Template <typename T, int capacity>
Array<t,capacity>::array () {
num = 0;
elements = new T[capacity];
}

Template <typename T, int capacity>
Array<t,capacity>::~array () {
delete [] elements;
}

Template <typename T, int capacity>
BOOL Array<t,capacity>::empty () {
if (num==0) return true;
else return false;
}

Template <typename T, int capacity>
void Array<t,capacity>::p ush (T value) {
if (num >= capacity) throw Arrayexception ("Array full Exception");
else {
Elements[num] = value;
num++;
}
}

Template <typename T, int capacity>
T Array<t,capacity>::p op () {
if (num = = 0) throw arrayexception ("Array Empty Exception");
else {
T temp = elements[num-1];
Elements[num-1] = 0;
num--;
return temp;
}
}


Template <typename T, int capacity>
int Array<t,capacity>::size () {
return num;
}

Template <typename T, int capacity>
t& array<t,capacity>::operator[] (int index) {
if (Index < 0 | | index >= num) throw arrayexception ("Out of Range Exception");
else return Elements[index];
}

Arrayexception::arrayexception (const char *msg) {
message=msg;
}

Const char* Arrayexception::what () const{
return message;

}


void Test ()
{
Array<int,1> Intarray;


try {
Intarray.push (1);
}

catch (Arrayexception ex) {
cout << ex.what () << Endl;
}

try {

cout << intarray[3] << Endl;

}

catch (Arrayexception ex) {

cout << ex.what () << Endl;

}

try {

cout << intarray.pop () << Endl;

}

catch (Arrayexception ex) {

cout << ex.what () << Endl;
}

}
int main () {


Array<int,15> A;
for (int i = 0; I <15; i++) a.push (i);
cout<< "a.size==" <<a.size () <<endl;
cout<< "a[0]==" <<a[0]<<endl;
cout<< "A[a.size () -1]==" <<a[a.size () -1]<<endl;
cout<< "a[-1]==" <<a[-1]<<endl;
cout<< "A[a.size ()]==" <<a[a.size ()]<<endl;

cout << Endl;

return 0;

}

Homework 15 2016 6 2 templates

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.