User Tools

Site Tools


c_-_c_templates:class_template_-_array_template

C - C++ Templates - Class Template - Array Template

#include <iostream>
using namespace std;
 
template <class T, int n>
class forgetcode
{
  T a[n];
 
public:
  void setvalue (int x, T v)
  {
    a[x]=v;
  }
 
  T getvalue(int x) 
  {
    return a[x];
  }
};
 
 
int main () 
{
  forgetcode <int,5> f1;
  forgetcode <float,5> f2;
  int a[5]={1,2,3,4,5};
  float b[5]={1.1,2.2,3.3,4.4,5.5};
 
  for(int i=0;i<5;i++)
  {
    f1.setvalue(i,a[i]);
    cout << f1.getvalue(i) << '\n';
  }
 
  for(int i=0;i<5;i++)
  {
    f2.setvalue(i,b[i]);
    cout << f2.getvalue(i) << '\n';
  }
 
  return 0;
}
c_-_c_templates/class_template_-_array_template.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki