Using Default Arguments with Template Classes : template class « Class « C++

Home
C++
1.Bitset
2.Class
3.Console
4.Data Structure
5.Data Type
6.Deque
7.Development
8.File
9.Function
10.Generic
11.Language
12.List
13.Map Multimap
14.Overload
15.Pointer
16.Qt
17.Queue Stack
18.Set Multiset
19.STL Algorithms Binary search
20.STL Algorithms Heap
21.STL Algorithms Helper
22.STL Algorithms Iterator
23.STL Algorithms Merge
24.STL Algorithms Min Max
25.STL Algorithms Modifying sequence operations
26.STL Algorithms Non modifying sequence operations
27.STL Algorithms Sorting
28.STL Basics
29.String
30.Valarray
31.Vector
C / ANSI-C
C Tutorial
C++ Tutorial
Visual C++ .NET
C++ » Class » template classScreenshots 
Using Default Arguments with Template Classes
   
#include <iostream>
#include <cstdlib>
using namespace std;
   
template <class T=int, int size=10class MyClass {
  T a[size]// size of array is passed in size
public:
  MyClass() {
    register int i;
    for(i=0; i<size; i++a[i= i;
  }
  T &operator[](int i);
};
   
template <class T, int size>
T &MyClass<T, size>::operator[](int i)
{
  if(i<|| i> size-1) {
    cout << "\nIndex value of ";
    cout << i << " is out-of-bounds.\n";
    exit(1);
  }
  return a[i];
}
   
int main()
{
  MyClass<int, 100> intarray;  
  MyClass<double> doublearray; 
  MyClass<> defarray;          
   
  cout << "int array: ";
  for(int i=0; i<100; i++intarray[i= i;
  for(int i=0; i<100; i++cout << intarray[i<< endl;
   
  cout << "double array: ";
  for(int i=0; i<10; i++doublearray[i(doublei/3;
  for(int i=0; i<10; i++cout << doublearray[i<< endl;
   
  cout << "defarray array: ";
  for(int i=0; i<10; i++defarray[i= i;
  for(int i=0; i<10; i++cout << defarray[i<< endl;
   
  return 0;
}
  
    
    
  
Related examples in the same category
1.template class with type parameter
2.template class with generic parameter
3.Demonstrate a very simple safe pointer class.
4.A generic safe-array class that prevents array boundary errors.
5.Get storage off stack for array
6.template extending
7.sequence template
8.Template Version of Generic binary sorted Tree.
9.template class with two generic parameters
10.generic stack template class
11.Using exceptions with templates.
12.Passing by reference and using virtual functions in exceptions.
13.Using Non-Type Arguments with Generic Classes
14.Generic Classes: demonstrates a generic stack.
15.An Example with Two Generic Data Types
16.Applying Template Classes: A Generic Array Class
17.Explicit Class Specializations for generic template class
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.