CSCI 265: Advanced Programming


Examples Using Templates


//1.cc

#include< iostream.h >

//Template Function

template< class T > T return_val(T a, T b){
	if (a > b)
		return a;
	else
		return b;
}

//Template Function -- Overloaded
template< class T > T return_val(T a){
	return a;
}

//Template Class with a non-type parameter

template< class T , int size> class test{
		T capacity[size];
	public:
		test(){
			for (int i = 0; i < size; i++){
				capacity[i] = 100;
			}
		}
		
		void print_details(){
			for (int i= 0; i < size; i++){
				cout << "capacity: " << capacity[i] << endl;
			}
		}
};

main(){

	int x = 10, y = 100;
	
	//First Template Function is Invoked
	cout << "return value: " << return_val(x,y) << endl << endl;
	
	//Second Template Function is Invoked
	cout << "return value: " << return_val(x) << endl << endl;

	//Size is specified as another parameter.	
	test my_test;
	my_test.print_details();

}
------------------------------------------------------------------------------
2.cc

#include< iostream.h >

#define MAX 10


//Template Class 

template< class T > class test{
		T capacity[MAX];
	public:
		test(){
			for (int i = 0; i < MAX; i++){
				capacity[i] = 100;
			}
		}
		
		void print_details(){
			for (int i= 0; i < MAX; i++){
				cout << "capacity: " << capacity[i] << endl;
			}
		}
};

//A derived template class from a template class

template< class T > class better_test:public test{
		T value[MAX];
	public:
		better_test(){
			test::test();
			for (int i = 0; i < MAX; i++){
				value[i] = 100;
			}			
		}	
		void print_details(){
			test::print_details();
			for (int i= 0; i < MAX; i++){
				cout << "value: " << value[i] << endl;
			}
		}
};

main(){

	//Object of test class.	
	test my_test;
	my_test.print_details();
	cout << "------------------------------------------" << endl;

	//Object of better_test class.
	better_test my_better_test;
	my_better_test.print_details();

}
-------------------------------------------------------------------------------
//3.cc

#include< iostream.h >

#define MAX 10


//Non-Template Class 

class test{
		int capacity[MAX];
	public:
		test(){
			for (int i = 0; i < MAX; i++){
				capacity[i] = 100;
			}
		}
		
		void print_details(){
			for (int i= 0; i < MAX; i++){
				cout << "capacity: " << capacity[i] << endl;
			}
		}
};

//A derived template class from a non-template class

template< class T > class better_test:public test{
		T value[MAX];
	public:
		better_test(){
			//test::test();
			for (int i = 0; i < MAX; i++){
				value[i] = 100;
			}			
		}	
		void print_details(){
			test::print_details();
			for (int i= 0; i < MAX; i++){
				cout << "value: " << value[i] << endl;
			}
		}
};

main(){

	//Object of test class.	
	test my_test;
	my_test.print_details();
	cout << "------------------------------------------" << endl;

	//Object of better_test class.
	better_test my_better_test;
	my_better_test.print_details();

}
-------------------------------------------------------------------------------
//4.cc

#include< iostream.h >

#define MAX 10


//Template Class 

template< class T > class test{
		T capacity[MAX];
	public:
		test(){
			for (int i = 0; i < MAX; i++){
				capacity[i] = 100;
			}
		}
		
		void print_details(){
			for (int i= 0; i < MAX; i++){
				cout << "capacity: " << capacity[i] << endl;
			}
		}
};

//A derived non-template class from a template class


//class better_test:public test{
class better_test:public test{
		int value[MAX];
	public:
		better_test(){
			//test::test();
			test::test();
			for (int i = 0; i < MAX; i++){
				value[i] = 100;
			}			
		}	
		void print_details(){
			//test::print_details();
			test::print_details();
			for (int i= 0; i < MAX; i++){
				cout << "value: " << value[i] << endl;
			}
		}
};

main(){

	//Object of test class.	
	test my_test;
	my_test.print_details();
	cout << "------------------------------------------" << endl;

	//Object of better_test class.
	better_test my_better_test;
	my_better_test.print_details();

}
------------------------------------------------------------------------------
//5.cc

#include< iostream.h >

#define MAX 2


//Template Class 

template< class T > class test{
		T capacity[MAX];
	public:
		test(){
			for (int i = 0; i < MAX; i++){
				cin >> capacity[i];
			}
		}
		
		void print_details(){
			for (int i= 0; i < MAX; i++){
				cout << "capacity: " << capacity[i] << endl;
			}
		}

		friend void f(); //A Friend Function.
};

//Friend Function able to access the private data members (of the objects of
//ALL template classes)!!

void f(){
	 	cout << "Enter 2 integer values: " << endl;	
		test int_test;
		cout << "Before......." << endl << endl;	
		int_test.print_details();

		for (int i = 0; i < MAX; i++)
			int_test.capacity[i] = -999;
		
		cout << endl << "After......." << endl << endl;	
		int_test.print_details();
	
		cout << "---------------------------" << endl;
		cout << "Enter 2 float values: " << endl;	
		test float_test;
		
		cout << "Before......." << endl << endl;	
		float_test.print_details();
		
		for (int i = 0; i < MAX; i++)
			float_test.capacity[i] = -999.9;
		cout << endl << "After......." << endl << endl;	
		float_test.print_details();
}	

main(){

	f();

}
-------------------------------------------------------------------------------
//6.cc

//An Example of templates and Operator Overloading.

#include< iostream.h >

#define MAX 10

//Template Class -- Interface

template< class T > class vector{
		T capacity[MAX];
		int next;	

	public:
		vector();
		void add(T a);
		T remove_from(int);
		void print();
		void operator!();
		~vector();

};
		
//Template Class -- Implementation.

//Constructor
template< class T > vector::vector(){
	for (int i = 0; i < MAX; i++){
		capacity[i] = 0;
	}
	next = 0;	
}
		
//Adds an element
template< class T > void vector::add(T a){
	if (next < MAX){
		capacity[next] = a;
		next++;
		cout << "--------Element is Added-------"<< endl << endl;	
	}else
		cout << "Vector is FULL. Cannot add any element!" << endl;
}

//Removes an element from a specific place.
template< class T > T vector::remove_from(int index){
	if (index < next){
		T tmp = capacity[index];
		for (int i = index; i < next; i++)
			capacity[i] = capacity[i+1];
		next--;	
		cout << "--------Element is Removed-------"<< endl << endl;	
		return tmp;
	}else
		cout << "No element at the requested position!" << endl;
}

//Print function.
template< class T > void vector::print(){
	cout << "--------Printing the Details-------"<< endl << endl;	
	for (int i= 0; i < next; i++)
		cout << "capacity: " << capacity[i] << endl << endl;
}

//Reverses the "filled" contents of the Vector 
//Overloaded operator function

template< class T > void vector::operator!(){
	for (int i = 0; i < next/2; i++){
		T tmp = capacity[i];
		capacity[i] = capacity[next-1-i];
		capacity[next-1-i] = tmp;
	}
	cout << "Vector is Reversed!" << endl << endl;
	print();
}

//Destructor
template< class T > vector::~vector(){};

main(){

	vector my_vector;

	my_vector.add(10);
	my_vector.add(20);
	my_vector.add(30);
	my_vector.add(40);
	my_vector.add(50);
	my_vector.add(60);
	my_vector.add(70);
	
	my_vector.print();
	my_vector.remove_from(0);
	my_vector.print();

	!my_vector; //Operator Function Invocation.
}
--------------------------------------------------------------------------------
//7.cc -- This one DID NOT work!

//An Example of templates and Operator Overloading (as a friend function)..

#include< iostream.h >

#define MAX 5

//Template Class -- Interface

template< class T > class vector{
		T capacity[MAX];
		int next;	

	public:
		vector();
		void add(T a);
		T remove_from(int);
		void print();
		friend void operator!(vector v);
		~vector();

};
		
//Template Class -- Implementation.

//Constructor
template< class T > vector::vector(){
	for (int i = 0; i < MAX; i++){
		capacity[i] = 0;
	}
	next = 0;	
}
		
//Adds an element
template< class T > void vector::add(T a){
	if (next < MAX){
		capacity[next] = a;
		next++;
		cout << "--------Element is Added-------"<< endl << endl;	
	}else
		cout << "Vector is FULL. Cannot add any element!" << endl;
}

//Removes an element from a specific place.
template< class T > T vector::remove_from(int index){
	if (index < next){
		T tmp = capacity[index];
		for (int i = index; i < next; i++)
			capacity[i] = capacity[i+1];
		next--;	
		cout << "--------Element is Removed-------"<< endl << endl;	
		return tmp;
	}else
		cout << "No element at the requested position!" << endl;
}

//Print function.
template< class T > void vector::print(){
	cout << "--------Printing the Details-------"<< endl << endl;	
	for (int i= 0; i < next; i++)
		cout << "capacity: " << capacity[i] << endl << endl;
}

//Reverses the contents of the Vector 
//Overloaded operator function


template< class T > void operator!(vector a){
	for (int i = 0; i < a.next/2; i++){
		T tmp = a.capacity[i];
		a.capacity[i] = a.capacity[a.next-1-i];
		a.capacity[a.next-1-i] = tmp;
	}
	cout << "Vector is Reversed!" << endl << endl;
	a.print();
}

//Destructor
template< class T > vector::~vector(){};

//template class vector;
template class vector;

//This one DID NOT work....
void operator!(vector);


main(){

	vector my_vector;

	my_vector.add(10);
	my_vector.add(20);
	my_vector.add(30);
	my_vector.add(40);
	my_vector.add(50);
	my_vector.add(60);
	my_vector.add(70);
	
	my_vector.print();
	my_vector.remove_from(0);
	my_vector.print();

	!my_vector; //Operator Function Invocation -- did not work..
}
-------------------------------------------------------------------------------
//container.h

//Simple container class -- indicates templates and String Class objects

#ifndef CONTAINER_H
#define CONTAINER

#include< iostream.h >
#define MAX 10

//Interface of the class container.
template < class T > class container{
	T arr[MAX];
	int counter;

public:
	container();
	~container();

	void add(T a);
	void print();
};

#endif
-------------------------------------------------------------------------------
//container.cc

//Implementation of the container class
#include "container.h"

//Constructor 
template< class T > container::container(){counter = 0;}

//Destructor 
template< class T > container::~container(){};

//Add funtions -- adds and element....checks for duplicates before adding.
template< class T > void container::add(T a){
		bool flag = false; //built-in boolean type of C++

		//Check for duplicates.	
		for (int i = 0; i < counter; i++){
			if (arr[i] == a){
				cout << "Already in!" << endl;
				flag = true;	
			}
		}
			
		//Add if unique.	
		if (!flag){		
			arr[counter] = a;
			counter++;
			cout << "Element added!" << endl;	
		}	
}

//Simple Print function.
template< class T > void container::print(){
		cout << "Printing: ----------------"  << endl;
		for (int i = 0; i < counter; i++)
			cout << "Element: " << arr[i] << endl;
		cout << "--------------------------"  << endl;
		cout << endl;
}

//Explicit Instantiation of Templates
template class container < char * >;
template class container< int >;
--------------------------------------------------------------------------------
//container_main.cc

//main function...creates string and integer containers.
#include "container.h"

main(){
	//String container.	
	container< char * > string_container;

	string_container.add("CS265");
	string_container.add("C Programming");
	string_container.add("C++ Programming");
	string_container.print();

	//Integer Container.	
	container< int > int_container;
	int_container.add(10);
	int_container.add(20);
	int_container.add(20);
	int_container.print();
}
-------------------------------------------------------------------------------
//container_makefile

# Sample makefile for container program


CC = g++
FLAGS = -g 
CMD = container_exec
RM = /bin/rm -f
LP = /usr/ucb/lpr

OBJS = container.o container_main.o
CCFILES = container.cc container_main.cc
HFILES = container.h 

$(CMD):	$(OBJS)
	$(CC)  -o $(CMD) $(OBJS)

container.o:	container.cc container.h
	$(CC) $(FLAGS) -c container.cc 

container_main.o:	container_main.cc container.h
	$(CC) $(FLAGS) -c container_main.cc 

clean:
	$(RM) $(OBJS) core

print: 
	$(LP)	$(HFILES) $(CCFILES)