TEMPLATES IN C++
C++ MY PATTERN:-
#include<windows.h>
#include<conio.h>
#include <math.h>
using namespace std;
void sleep(int m)
{
for(int j=0;j<m*21000;j++)
{
}
}
void gotoRowCol(int rpos, int cpos)
{
int xpos=cpos, ypos = rpos;
COORD scrn;
HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
scrn.X = xpos; scrn.Y = ypos;
SetConsoleCursorPosition(hOuput,scrn);
}
void Fan(int x, char v)
{
gotoRowCol(10,5);
for(int i=9;i<=18;i++)
{
gotoRowCol(i,15);
cout<<v;
}
double z = 14;
double j = 10;
for(j=8 , z=14;j<20 , z<40;j++ , z+=2.5)
{
gotoRowCol(j+1,z+1);
cout<<v;
}
double a=20;
double b=14.5;
for(a=20 , b=14.5;a>=10 , b<40;a-- , b+=2.5)
{
gotoRowCol(a-1,b+1);
cout<<v;
}
for(int c=9;c<=18;c++)
{
gotoRowCol(c,40);
cout<<v;
}
}
void Fan1(int y, char c)
{
for(int q=23;q<=33;q++)
{
gotoRowCol(3,q);
cout<<c;
}
for(int w=23;w<=33;w++)
{
gotoRowCol(25,w);
cout<<c;
}
double r;
double t;
for(r=22,t=3;r<32,t<=26;r++,t+=2.2)
{
gotoRowCol(t+1,r+1);
cout<<c;
}
double p;
double k;
for(p=3 , k=32;p<26 , k>21;p++ , k-=0.5)
{
gotoRowCol(p+1,k+1);
cout<<c;
}
}
int main()
{
int x;
int rows=50;
int cols=40;
char c;
cout<<"Enter a character: ";
cin>>c;
while(true)
{
gotoRowCol(rows/2,cols/2);
{
Fan(x,c);
sleep(2000);
Fan(x,' ');
Fan1(x,c);
sleep(2000);
Fan1(x,' ');
}
}
}
TEMPLATE SWAPPING:-
#include<iostream>
using namespace std;
#include<string>
class sample
{
public:
int a,b;
float fa,fb;
char ch1,ch2;
string str1,str2;
template<class T> void swap (T &a, T &b);
void menu();
};
template <class T>
void sample :: swap(T &a, T &b)
{
T temp =a;
a=b;
b=temp;
}
void sample:: menu()
{
cout <<"\ WELCOME TO THE WORLD OF TEMPLATe************ \n";
cout<<"********* f -> float number swapping\n";
cout<<"********* c -> char swapping \n";
cout<<"********* s -> string swapping \n";
cout<<"********* m -> menu \n";
cout<<"********* q -> quit \n";
cout<< "*************** ANY OPTION PLEASE ???? ****** \n";
}
int main ()
{
sample obj;
char code;
obj.menu();
while ((code= cin.get() )!='q')
{
switch (code)
{
case 'i':
cout<<"enter two values\n";
cin>>obj.a>>obj.b;
cout<<"before swapping \n";
cout<<"a= "<<obj.a<<" , b= "<<obj.b<<endl;
obj.swap(obj.a,obj.b);
cout<<"after swapping \n";
cout<<"a= "<<obj.a<<" , b= "<<obj.b<<endl;
break;
case 'f':
cout<<"enter two values\n";
cin>>obj.fa>>obj.fb;
cout<<"before swapping \n";
cout<<"fa= "<<obj.fa<<" , fb= "<<obj.fb<<endl;
obj.swap(obj.fa,obj.fb);
cout<<"after swapping \n";
cout<<"fa= "<<obj.fa<<" , fb= "<<obj.fb<<endl;
break;
case'c':
cout<<"enter two values\n";
cin.ignore();
obj.ch1= cin.get();
cin.ignore();
obj.ch2=cin.get();
cout<<"before swapping \n";
cout<<"ch1= "<<obj.ch1<<" , ch2= "<<obj.ch2<<endl;
obj.swap(obj.ch1,obj.ch2);
cout<<"after swapping \n";
cout<<"ch1= "<<obj.ch1<<" , ch2= "<<obj.ch2<<endl;
break;
case's':
cout<<"enter two values\n";
cin.ignore();
obj.str1= cin.get();
cin.ignore();
obj.str2=cin.get();
cout<<"before swapping \n";
cout<<"str1= "<<obj.str1<<" , str2= "<<obj.str2<<endl;
obj.swap(obj.str1,obj.str2);
cout<<"after swapping \n";
cout<<"str1= "<<obj.str1<<" , str2= "<<obj.str2<<endl;
break;
case'm':
obj.menu();
break;
}
}
return 0;
cout<<"**************** MADE BY SHOBHIT GOEL ************************/n";
cout<<"***********************THANK YOU ******************************/n";
}
TEMPLATE SUM OF AN ARRAY:-
#include<iostream>
using namespace std;
#include<string>
class sample
{
public:
int a,b;
float fa,fb;
char ch1,ch2;
string str1,str2;
template<class T> void swap (T &a, T &b);
void menu();
};
template <class T>
void sample :: swap(T &a, T &b)
{
T temp =a;
a=b;
b=temp;
}
void sample:: menu()
{
cout <<"WELCOME TO THE WORLD OF TEMPLATe \n";
cout<<"i-> integer swapping\n";
cout<< " f -> float number swapping\n";
cout<< " c -> char swapping \n";
cout<<" s -> string swapping \n";
cout<<" m -> menu \n";
cout<<" q -> quit \n";
cout<< " ANY OPTION PLEASE ???? \n";
}
int main ()
{
sample obj;
char code;
obj.menu();
while ((code= cin.get() )!='q')
{
switch (code)
{
case 'i':
cout<<"enter two values\n";
cin>>obj.a>>obj.b;
cout<<"before swapping \n";
cout<<"a= "<<obj.a<<" , b= "<<obj.b<<endl;
obj.swap(obj.a,obj.b);
cout<<"after swapping \n";
cout<<"a= "<<obj.a<<" , b= "<<obj.b<<endl;
break;
case 'f':
cout<<"enter two values\n";
cin>>obj.fa>>obj.fb;
cout<<"before swapping \n";
cout<<"fa= "<<obj.fa<<" , fb= "<<obj.fb<<endl;
obj.swap(obj.fa,obj.fb);
cout<<"after swapping \n";
cout<<"fa= "<<obj.fa<<" , fb= "<<obj.fb<<endl;
break;
case'c':
cout<<"enter two values\n";
cin.ignore();
obj.ch1= cin.get();
cin.ignore();
obj.ch2=cin.get();
cout<<"before swapping \n";
cout<<"ch1= "<<obj.ch1<<" , ch2= "<<obj.ch2<<endl;
obj.swap(obj.ch1,obj.ch2);
cout<<"after swapping \n";
cout<<"ch1= "<<obj.ch1<<" , ch2= "<<obj.ch2<<endl;
break;
case's':
cout<<"enter two values\n";
cin.ignore();
obj.str1= cin.get();
cin.ignore();
obj.str2=cin.get();
cout<<"before swapping \n";
cout<<"str1= "<<obj.str1<<" , str2= "<<obj.str2<<endl;
obj.swap(obj.str1,obj.str2);
cout<<"after swapping \n";
cout<<"str1= "<<obj.str1<<" , str2= "<<obj.str2<<endl;
break;
case'm':
obj.menu();
break;
}
}
return 0;
}
TEMPLATE NAMESPACE USING A KEYWORD:-
#include<iostream>
using namespace std;
namespace X
{
int a=20;
}
namespace Y
{
int *ptr;
}
int main()
{
Y::ptr=&(++X::a);
cout<<"++ptr= "<<*(Y::ptr)<<'\n';
return 0;
}
TEMPLATES IN C++
Reviewed by Marketing Thrills
on
September 14, 2015
Rating:
No comments:
Post a Comment