By using function template swaping of two integer,float or string.Also using constructor and destructor.
#include<iostream>
using namespace std;
class swaping
{
public:
int a,b;
float aa,bb;
string aaa,bbb;
template<class T>void swap(T &a,T &b);
void integervalue();
void floatvalue();
void stringvalue();
swaping();
~swaping();
};
void swaping::integervalue()
{
cout<<"Enter two integers:";
cin>>a>>b;
}
void swaping::floatvalue()
{
cout<<"Enter two float value:";
cin>>aa>>bb;
}
void swaping::stringvalue()
{
cout<<Enter two string value:";
cin>>aaa>>bbb;
}
template<class T>
void swaping::swap(T &a,T &b)
{
T temp=a;
a=b;
b=temp;
}
swaping::swaping()
{
cout<<"Welcome\n";
}
swaping::~swaping()
{
cout<<"\nIt's over.";
}
int main()
{
swaping aaa;
char ch;
cout<<"Please select:\nswap integer:-a\nswap float:-b\nswap string:-c\n";
cin>>ch;
if(ch=='a')
{
aaa.integervalue();
aaa.swap(aaa.a,aaa.b);
cout<<"\nAfter swaping\n"<<aaa.a<<" "<<aaa.b<<"\n";
}
if(ch=='b')
{
aaa.floatvalue();
aaa.swap(aaa.aa,aaa.bb);
cout<<"\nAfter swaping\n"aaa.aa<<" "<<aaa.bb;<<"\n";
}
if(ch=='c')
{
aaa.stringvalue();
aaa.swap(aaa.aaa,aaa.bbb);
cout<<"\nAfter swaping\n"<<aaa.aaa<<" "<<aaa.bbb<<"\n";
}
return 0;
}
Output:
Welcome
Please select:
swap integer:-a
swap float:-b
swap string:-c
c
Enter two string value:Welcome Hai
After swaping
Hai Welcome
It's over.
………………………………………………
………………………………………………
By using function template, a program to find the cube of a number either the number is integer,float or double.
#include<iostream>
using namespace std;
class cube
{
public:
int a;
float b;
double c;
void integerno();
void floatno();
void doubleno();
template<class T>void cal(T &t);
};
void cube::integerno()
{
cout<<"Enter an integer no.:";
cin>>a;
}
void cube::floatno()
{
cout<<"Enter a float no.:";
cin>>b;
}
void cube::doubleno()
{
cout<<"Enter a double no.:";
cin>>c;
}
template<class T>
void cube::cal(T &t)
{
T cube_value=t*t*t;
t=cube_value;
}
int main()
{
cout<<"Select:\ninteger:-a\nfloat:-b\ndouble:-c\nquit:-q\n";
int i=0;
cube aaa;
char ch;
while((ch=cin.get()) != 'q')
{
switch(ch)
{
case 'a':
{
aaa.integerno();
aaa.cal(aaa.a);
cout<<"Cube of the number:"<<aaa.a<<endl;
}
break;
case 'b':
{
aaa.floatno();
aaa.cal(aaa.b);
cout<<"Cube of the number:"<<aaa.b<<endl;
}
break;
case 'c':
{
aaa.doubleno();
aaa.cal(aaa.c);
cout<<"Cube of the number:"<<aaa.c<<endl;
}
break;
}
i++;
}
return 0;
}
Output:
Select:
integer:-a
float:-b
double:-c
quit:-q
a
Enter an integer no.:6
Cube of the number:216
b
Enter a flaot no.:6.4
Cube of the number:262.144
q
------------------------
Using function template
turn the c++ definition
int sum(int a,int b,int c)
{return (a+b+c); }
into a function template.
#include<iostream>
using namespace std;
class test
{
public:
int a1,a2,a3;
float b1,b2,b3;
double c1,c2,c3;
template<class T>T sum(T &a,T &b,T &c)
{
return(a+b+c);
}
};
int main()
{
test obj;
int no;
cout<<"Integer-1\nfloat-2\ndouble-3\n";
cin>>no;
if(no==1)
{
cout<<"Enter three integer nos.";
cin>>obj.a1>>obj.a2>>obj.a3;
cout<<"Sum"<<obj.sum(obj.a1,obj.a2,obj.a3);
}
if(no==2)
{
cout<<"Enter three float nos.";
cin>>onj.b1>>obj.b2>>obj.b3;
cout<<"Sum"<<obj.sum(obj.b1,obj.b2,obj.b3);
}
if(no==3)
{
cout<<"Enter three double nos.";
cin>>obj.c1>>obj.c2>>obj.c3;
cout<<"Sum"<<obj.sum(obj.c1,obj.c2,obj.c3);
}
return 0;
}
Integer-1
float-2
double-3
1
Enter three integer nos.3 6 9
Sum18
-----------------
Just write a comment into comment box.
#include<iostream>
using namespace std;
class swaping
{
public:
int a,b;
float aa,bb;
string aaa,bbb;
template<class T>void swap(T &a,T &b);
void integervalue();
void floatvalue();
void stringvalue();
swaping();
~swaping();
};
void swaping::integervalue()
{
cout<<"Enter two integers:";
cin>>a>>b;
}
void swaping::floatvalue()
{
cout<<"Enter two float value:";
cin>>aa>>bb;
}
void swaping::stringvalue()
{
cout<<Enter two string value:";
cin>>aaa>>bbb;
}
template<class T>
void swaping::swap(T &a,T &b)
{
T temp=a;
a=b;
b=temp;
}
swaping::swaping()
{
cout<<"Welcome\n";
}
swaping::~swaping()
{
cout<<"\nIt's over.";
}
int main()
{
swaping aaa;
char ch;
cout<<"Please select:\nswap integer:-a\nswap float:-b\nswap string:-c\n";
cin>>ch;
if(ch=='a')
{
aaa.integervalue();
aaa.swap(aaa.a,aaa.b);
cout<<"\nAfter swaping\n"<<aaa.a<<" "<<aaa.b<<"\n";
}
if(ch=='b')
{
aaa.floatvalue();
aaa.swap(aaa.aa,aaa.bb);
cout<<"\nAfter swaping\n"aaa.aa<<" "<<aaa.bb;<<"\n";
}
if(ch=='c')
{
aaa.stringvalue();
aaa.swap(aaa.aaa,aaa.bbb);
cout<<"\nAfter swaping\n"<<aaa.aaa<<" "<<aaa.bbb<<"\n";
}
return 0;
}
Output:
Welcome
Please select:
swap integer:-a
swap float:-b
swap string:-c
c
Enter two string value:Welcome Hai
After swaping
Hai Welcome
It's over.
………………………………………………
………………………………………………
By using function template, a program to find the cube of a number either the number is integer,float or double.
#include<iostream>
using namespace std;
class cube
{
public:
int a;
float b;
double c;
void integerno();
void floatno();
void doubleno();
template<class T>void cal(T &t);
};
void cube::integerno()
{
cout<<"Enter an integer no.:";
cin>>a;
}
void cube::floatno()
{
cout<<"Enter a float no.:";
cin>>b;
}
void cube::doubleno()
{
cout<<"Enter a double no.:";
cin>>c;
}
template<class T>
void cube::cal(T &t)
{
T cube_value=t*t*t;
t=cube_value;
}
int main()
{
cout<<"Select:\ninteger:-a\nfloat:-b\ndouble:-c\nquit:-q\n";
int i=0;
cube aaa;
char ch;
while((ch=cin.get()) != 'q')
{
switch(ch)
{
case 'a':
{
aaa.integerno();
aaa.cal(aaa.a);
cout<<"Cube of the number:"<<aaa.a<<endl;
}
break;
case 'b':
{
aaa.floatno();
aaa.cal(aaa.b);
cout<<"Cube of the number:"<<aaa.b<<endl;
}
break;
case 'c':
{
aaa.doubleno();
aaa.cal(aaa.c);
cout<<"Cube of the number:"<<aaa.c<<endl;
}
break;
}
i++;
}
return 0;
}
Output:
Select:
integer:-a
float:-b
double:-c
quit:-q
a
Enter an integer no.:6
Cube of the number:216
b
Enter a flaot no.:6.4
Cube of the number:262.144
q
------------------------
Using function template
turn the c++ definition
int sum(int a,int b,int c)
{return (a+b+c); }
into a function template.
#include<iostream>
using namespace std;
class test
{
public:
int a1,a2,a3;
float b1,b2,b3;
double c1,c2,c3;
template<class T>T sum(T &a,T &b,T &c)
{
return(a+b+c);
}
};
int main()
{
test obj;
int no;
cout<<"Integer-1\nfloat-2\ndouble-3\n";
cin>>no;
if(no==1)
{
cout<<"Enter three integer nos.";
cin>>obj.a1>>obj.a2>>obj.a3;
cout<<"Sum"<<obj.sum(obj.a1,obj.a2,obj.a3);
}
if(no==2)
{
cout<<"Enter three float nos.";
cin>>onj.b1>>obj.b2>>obj.b3;
cout<<"Sum"<<obj.sum(obj.b1,obj.b2,obj.b3);
}
if(no==3)
{
cout<<"Enter three double nos.";
cin>>obj.c1>>obj.c2>>obj.c3;
cout<<"Sum"<<obj.sum(obj.c1,obj.c2,obj.c3);
}
return 0;
}
Integer-1
float-2
double-3
1
Enter three integer nos.3 6 9
Sum18
-----------------
Just write a comment into comment box.
Most Useful for Students...
ReplyDeletewritten topicwise in systematic manner with University Questions...
Thnxx sir..