Answers for "c++ template"

C++
4

c++ template

#include <iostream>
using namespace std;

template <typename T>
void Swap(T &n1, T &n2)
{
	T temp;
	temp = n1;
	n1 = n2;
	n2 = temp;
}

int main()
{
	int i1 = 1, i2 = 2;
	float f1 = 1.1, f2 = 2.2;
	char c1 = 'a', c2 = 'b';

	cout << "Before passing data to function template.\n";
	cout << "i1 = " << i1 << "\ni2 = " << i2;
	cout << "\nf1 = " << f1 << "\nf2 = " << f2;
	cout << "\nc1 = " << c1 << "\nc2 = " << c2;

	Swap(i1, i2);
	Swap(f1, f2);
	Swap(c1, c2);

        cout << "\n\nAfter passing data to function template.\n";
	cout << "i1 = " << i1 << "\ni2 = " << i2;
	cout << "\nf1 = " << f1 << "\nf2 = " << f2;
	cout << "\nc1 = " << c1 << "\nc2 = " << c2;

	return 0;
}
Posted by: Guest on August-14-2020
2

c++ template

// function template
#include <iostream>
using namespace std;

template <class T>
T GetMax (T a, T b) {
  T result;
  result = (a>b)? a : b;
  return (result);
}

int main () {
  int i=5, j=6, k;
  long l=10, m=5, n;
  k=GetMax<int>(i,j);
  n=GetMax<long>(l,m);
  cout << k << endl;
  cout << n << endl;
  return 0;
}
Posted by: Guest on December-15-2020
0

c++ template

// template specialization
#include <iostream>
using namespace std;

// class template:
template <class T>
class mycontainer {
    T element;
  public:
    mycontainer (T arg) {element=arg;}
    T increase () {return ++element;}
};

// class template specialization:
template <>
class mycontainer <char> {
    char element;
  public:
    mycontainer (char arg) {element=arg;}
    char uppercase ()
    {
      if ((element>='a')&&(element<='z'))
      element+='A'-'a';
      return element;
    }
};

int main () {
  mycontainer<int> myint (7);
  mycontainer<char> mychar ('j');
  cout << myint.increase() << endl;
  cout << mychar.uppercase() << endl;
  return 0;
}
Posted by: Guest on December-02-2020
0

c++ template

template <class myType>
myType GetMax (myType a, myType b) {
 return (a>b?a:b);
}
Posted by: Guest on October-24-2020
0

c++ template

int x,y;
GetMax <int> (x,y);
Posted by: Guest on July-24-2021
0

c++ template

template <class  T>//or <typename T> it´s the same
 //T can be int, float, double, etc. 
//simple use example: 
T sum(T a, T b)
{
  return a + b;
}
sum(5.0f, 10f);//sum using float
sum(2,3);//sum using int
Posted by: Guest on October-12-2021
0

c++ template

// class templates
#include <iostream>
using namespace std;

template <class T>
class mypair {
    T a, b;
  public:
    mypair (T first, T second)
      {a=first; b=second;}
    T getmax ();
};

template <class T>
T mypair<T>::getmax ()
{
  T retval;
  retval = a>b? a : b;
  return retval;
}

int main () {
  mypair <int> myobject (100, 75);
  cout << myobject.getmax();
  return 0;
}
Posted by: Guest on July-24-2021
0

c++ template

// function template II
#include <iostream>
using namespace std;

template <class T>
T GetMax (T a, T b) {
  return (a>b?a:b);
}

int main () {
  int i=5, j=6, k;
  long l=10, m=5, n;
  k=GetMax(i,j);
  n=GetMax(l,m);
  cout << k << endl;
  cout << n << endl;
  return 0;
}
Posted by: Guest on July-24-2021
0

c++ template

// sequence template
#include <iostream>
using namespace std;

template <class T, int N>
class mysequence {
    T memblock [N];
  public:
    void setmember (int x, T value);
    T getmember (int x);
};

template <class T, int N>
void mysequence<T,N>::setmember (int x, T value) {
  memblock[x]=value;
}

template <class T, int N>
T mysequence<T,N>::getmember (int x) {
  return memblock[x];
}

int main () {
  mysequence <int,5> myints;
  mysequence <double,5> myfloats;
  myints.setmember (0,100);
  myfloats.setmember (3,3.1416);
  cout << myints.getmember(0) << '\n';
  cout << myfloats.getmember(3) << '\n';
  return 0;
}
Posted by: Guest on July-24-2021
0

c++ template

#include <bits/stdc++.h>
using namespace std;
template <class T>
class Vector{
    public:
        T *arr;
        T size;

        Vector(int size){
            this->size = size;
            arr = new T[size];
        }
        T dotProduct(Vector &v){
            T d = 0;
            for(int i = 0; i<size; i++){
                d += this->arr[i] * v.arr[i];
            }
            return d;
        }
};
int main(){
    Vector<int> v1(3);
    v1.arr[0] = 1;
    v1.arr[1] = 2;
    v1.arr[2] = 3;
    Vector<int> v2(3);
    v2.arr[0] = 4;
    v2.arr[1] = 5;
    v2.arr[2] = 6;
    int a = v1.dotProduct(v2);
    cout<<a<<endl;
    Vector<float> v3(3);
    v3.arr[2] = 3.2;
    v3.arr[0] = 1.2;
    v3.arr[1] = 2.2;
    Vector<float> v4(3);
    v4.arr[0] = 4.4;
    v4.arr[1] = 5.4;
    v4.arr[2] = 6.4;
    float a1 = v3.dotProduct(v4);
    cout<<a1<<endl;

    return 0;
}
Posted by: Guest on September-11-2021

Browse Popular Code Answers by Language