C++: Generic functions and classes - Applications solved


1) Build a function generator (templates) that can determine the maximum number of elements an array whatever standard type of its elements. Write the main () function to call built for 2 arrays of different types. Arrays elements will be read from the keyboard in OOP technique.
#include<iostream>
using namespace std;
 
template<class T>
void read(T a[], int* n, char c)
{
  cout<<"Read the string "<<c<<endl;
  cout<<"\tEnter size: ";
  cin>>*n;
  cout<<"\tEnter elements:\n";
  for(int i=0;i<*n;i++)
  {
    cout<<"\t\telement ["<<i+1<<"]= ";
    cin>>a[i];
  }
}
 
template<class T>
void display(T a[], int n, char c)
{
  cout<<"String "<<c<<" is: ";
  for(int i=0;i<n;i++)
    cout<<a[i]<<' ';
  cout<<endl;
}
 
template<class T>
T maximum(T a[], int n)
{
  T max;
  max=a[0];
  for(int i=1;i<n;i++)
    if(a[i]>max)
      max=a[i];
  return max;
}
 
void main()
{
 int a[20];
 double b[20];
 int m,n;
 
 read(a,&m,'A');
 read(b,&n,'B');
 
 display(a,m,'A');
 display(b,n,'B');
 
 cout<<"\nMaximumul of string A is: "<<maximum(a,m);
 cout<<"\nMaximumul of string B is: "<<maximum(b,n);
 
 int var;
 cin>>var;
}
2) Build a function generator (templates) that can determine the sum features a numeric array whatever standard type of its elements. To write the main () function to call built for 2 arrays of different types. Arrays elements will be read from the keyboard in OOP technique.
#include<iostream>
using namespace std;

template<class T>
void read(T a[], int* n, char c)
{
  cout<<"Read the string "<<c<<endl;
  cout<<"\tEnter size: ";
  cin>>*n;
  cout<<"\tEnter elements:\n";
  for(int i=0;i<*n;i++)
  {
    cout<<"\t\telement ["<<i+1<<"]= ";
    cin>>a[i];
  }
}
 
template<class T>
void display(T a[], int n, char c)
{
  cout<<"The string "<<c<<" is: ";
  for(int i=0;i<n;i++)
    cout<<a[i]<<' ';
  cout<<endl;
}
 
template<class T>
T amount(T a[], int n)
{
  T sum;
  sum=0;
  for(int i=0;i<n;i++)
  {
    sum=a[i]+sum;
  }
  return sum;
}
 
void main()
{
 int a[20];
 double b[20];
 int m,n;
 
 read(a,&m,'A');
 read(b,&n,'B');
 
 display(a,m,'A');
 display(b,n,'B');
 
 cout<<"\nThe sum of string A is: "<<amount(a,m);
 cout<<"\nThe sum of string B is: "<<amount(b,n);
 
 int var;
 cin>>var;
}
3) Build a generic function that reverses the values ​​of two variables together with which is called.
//Example of function template

#include<iostream>
using namespace std;
 
template <class X>
void change(X &a, X &b)
{
X temp;
temp=a;
a=b;
b=temp;
}
void main()
{
int i=0, j=10;
float x=10.3, y=4.8;
char a='A', b='B';
 
cout<<"Original integer values :"<<i<<' '<<j<<endl;
change(i,j); //reverses the entires
cout<<"Integer values reversed :"<<i<<' '<<j<<endl<<endl;
cout<<"Original real values :"<<x<<' '<<y<<endl;
change(x,y); //reverses float
cout<<"Real values reversed :"<<x<<' '<<y<<endl<<endl;
cout<<"Original data values :"<<a<<' '<<b<<endl;
change(a,b); //reverses the entires
cout<<"Character values reversed :"<<a<<' '<<b<<endl<<endl;
 
 int var;
 cin>>var;
}
4) Build a program that creates a generic function with two generic types.
#include<iostream>
using namespace std;
 
template <class type1, class type2>
 
void f (type1 x, type2 y)
/* goes two lines provided that between these two lines
 to be passed any instructions */
{
	cout<<x<<' '<<y<<endl;
}
 
void main()
{
	f(10, "hi");
	f(0.3, 12L);
 
	int var;
	cin>>var;
}
5) Build a generic function that reverses the values ​​of two variables together with which is called using an explicit overload generic functions.
#include<iostream>
using namespace std;
 
//Example of function template

template <class X> void change(X &a, X &b)
{
	X temp;
	temp=a;
	a=b;
	b=temp;
}
 
void change(int &a, int &b)
{
	int temp;
	temp=a;
	a=b;
	b=temp+1;
	cout<<"Overwritten function."<<endl;
}
 
void main()
{
	int i=0, j=10;
	float x=10.3, y=4.8;
	char a='A', b='B';
	cout<<"Original integer values:"<<i<<' '<<j<<endl; change(i,j); //reverses the entire
	cout<<"Integer values reversed:"<<i<<' '<<j<<endl<<endl;
	cout<<"Original real values:"<<x<<' '<<y<<endl; change(x,y); //reverses float
	cout<<"Real values reversed:"<<x<<' '<<y<<endl<<endl;
	cout<<"Original character values:"<<a<<' '<<b<<endl; change(a,b); //reverses the entire
	cout<<"Character values reversed:"<<a<<' '<<b<<endl<<endl;
 
	int var;
	cin>>var;
}
6) Make a generic class for working with static stack.
#include<iostream>
using namespace std;
 
 
template <class X>
class Stack_Vector
{
	X *memory;
	int size;
	int vf;
	public:
	Stack_Vector(int);
	~Stack_Vector(){delete memory;}
	virtual int push(X);
	virtual int pop(X&);
	virtual int top(X&);
	virtual int empty()
	{
		vf=-1;
		return vf;
	}
	virtual int full()
	{
		vf=size-1;
		return vf;
	}
};
template <class X> Stack_Vector<X>::Stack_Vector(int dim)
{
	size=dim;
	memory=new X[size];
	vf=-1;
}
template <class X> int Stack_Vector<X>::push(X v)
{
	if(vf<size-1)
	{
		memory[++vf]=v;
		return 1;
	}
	else return 0;
}
template <class X> int Stack_Vector<X>::pop(X& v)
{
	if(vf!=-1)
	{
		v=memory[vf--];
		return 1;
	}
	else return 0;
}
template <class X> int Stack_Vector<X>::top(X& t)
{
	if(vf!=-1)
	{
		t=memory[vf];
		return 1;
	}
	else return 0;
}
void main()
{
	int c;
	Stack_Vector<int>*a=new Stack_Vector<int>(255);
	cout<<"Enter a number (-1 is stopping): ";
	cin>>c;
	while(c!=-1)
	{
		a->push(c);
		cout<<"Enter a number (-1 is stopping): ";
		cin>>c;
	}
	cout<<endl;
	while(a->pop(c))
	{
		cout<<c<<" ";
	}
	cout<<endl;
	delete a;
 
	int var;
	cin>>var;
}
7) Will develop an application to implement a linked list elements are integers. Requirement is to adapt this application so that the data type of the elements to be universal.
#include<iostream>
using namespace std;
#include<stdio.h>
#include<conio.h>

struct node
{
	int inf;
	struct node* next;
};
 
typedef struct node node;
class stack
{
	node *top;
	public:
	stack();
	~stack();
	void init(int);
	void push(int); //adding an item in the stack
	void display();
	int pop(); //remove an item from the stack
	int goala();
};
 
stack::stack()
{
	top=NULL;
}
void stack::init(int val)
{
	top=new node;
	top->next=NULL;
	top->inf=val;
}
void stack::push(int val)
{
	node* aux=new node;
	aux->inf=val;
	aux->next=top;
	top=aux;
}
int stack::pop()
{
	node *aux=top;
	int n;
	if (top)
	{
		n=top->inf;
		top=top->next;
		delete(aux);
	}
	else cout<<"List is empty!";
	return n;
}
void stack::display()
{
	node *aux=top;
	while(aux)
	{
		cout<<aux->inf<<" ";
		aux=aux->next;
	}
	cout<<endl;
}
stack::~stack()
{
	node *aux=top;
	while(top)
	{
		aux=top;
		top=top->next;
		delete(aux);
	}
}
int stack::goala()
{
	if(top!=NULL) return 1;
	else return 0;
}
void optiune()
{
	getch();
	cout<<endl<<"Enter option: "<<endl;
	cout<<"1.Initialization"<<endl;
	cout<<"2.Introduction element"<<endl;
	cout<<"3.Remove element"<<endl;
	cout<<"4.Display stack"<<endl;
	cout<<"5.Destruction stack"<<endl;
	cout<<"0.STOP"<<endl;
}
void main()
{
	stack s,s1;
	cout<<"This program uses the list as a stack, implementing"<<endl;
	cout<<"Basic operations on stack. ENTER"<<endl;
	optiune();
	int opt,initVal;
	cin>>opt;
	while (opt)
	{
		switch(opt)
		{
			case 0:break;
 
			case 1:cout<<"Enter a value:";
			cin>>initVal;
			s.init(initVal);
			break;
 
			case 2:cout<<"Enter the new element:";
			cin>>initVal;
			s.push(initVal);
			break;
 
			case 3:cout<<"Top element was:"<<s.pop();
			getch();
			break;
 
			case 4:cout<<"Elements that are in the stack are:";
			s.display();
			getch();
			break;
 
			case 5:s.~stack();
			cout<<"The list was destroyed!";
			getch();
			break;
 
			default:cout<<"Bad option chosen!";
			getch();
		}
		optiune();
		cin>>opt;
	}
 
	cout<<"Goodbye!";
	getch();
 
	int var;
	cin>>var;
}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.