class template in c++ code example

Example 1: class in c#

public class Car
{  
    // fields
    public bool isDriving = false;
    
    // constructor
    public Car( string make )
    {
        Make = make;
    }
	
    // properties
    private string _make = string.Empty;
    public string Make
    {
        get { return _make; }
        set { _make = value; } 
    }
    
    // methods
    public void drive()
    {
        if( isDriving )
        {
			// Car is already moving
        }
        else
		{
            // start driving the car
            isDriving = true;
    	}        
    }

    public void stop()
    {
		if( isDriving )
		{
			// stop the car 
			isDriving = false;
		}
        else
        {
			// car is already not moving
        }        
    }      
}

// ---
// An example of using this class in a console app

using System;
					
public class Program
{
	public static void Main()
	{		
        // construct a new class of type Car and set the Make
      	// property to "VW" using the constructor.
		Car newCar = new Car( "VW" );
		
      	// display the make of our new car
		Console.WriteLine( newCar.Make );
		
      	// call the drive method of the car class
		newCar.drive();		
		
      	// display the value of the isDriving property to
		Console.WriteLine( newCar.isDriving );
		
      	// call the stop method of the car class
		newCar.stop();
		
      	// display the value of the isDriving property
		Console.WriteLine( newCar.isDriving );
	}
}

// the class 
public class Car
{  
    // fields
    public bool isDriving = false;
    
    // constructor w
    public Car( string make )
    {
        Make = make;
    }
	
    // properties
    private string _make = string.Empty;
    public string Make
    {
        get { return _make; }
        set { _make = value; } 
    }
    
    // methods
    public void drive()
    {
        if( isDriving )
        {
        		// Car is already moving
        }
        else
		{
            // start driving the car
            isDriving = true;
    	}        
    }

    public void stop()
    {
		if( isDriving )
		{
			// stop the car 
			isDriving = false;
		}
        else
        {
			// car is already not moving
        }        
    }      
}

Example 2: function template

template <class T>
void swap(T & lhs, T & rhs)
{
 T tmp = lhs;
 lhs = rhs;
 rhs = tmp;
}

void main()
{
  int a = 6;
  int b = 42;
  swap<int>(a, b);
  printf("a=%d, b=%d\n", a, b);
  
  // Implicit template parameter deduction
  double f = 5.5;
  double g = 42.0;
  swap(f, g);
  printf("f=%f, g=%f\n", f, g);
}

/*
Output:
a=42, b=6
f=42.0, g=5.5
*/

Example 3: templates classes in c++

// 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;
}

Example 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;
}

Example 5: template in c++

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

Example 6: how to write a template c++

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

Tags:

Cpp Example