Program On Templates
"Nice One Program"
About:Template are used to make Any function,class to capable of Handling Any Data Type
Like here in this Example the compiler would Create Different function for Every type With same body.And can handle different Data Types.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
template<class T>
T abs(T n)
{
return (n<0)?-n:n;
}
void main()
{ clrscr();
int a=10;
int b=-6;
float f=-4.5;
long int la=-444444;
double fd=-0.20001;
cout<<"Abs val: of a:"<<abs(a)<<endl;
cout<<"Abs val: of b:"<<abs(b)<<endl;
cout<<"Abs val: of f:"<<abs(f)<<endl;
cout<<"Abs val: of la:"<<abs(la)<<endl;
cout<<"Abs val: of fd:"<<abs(fd)<<endl;
getch();
}
Reading from File Char by Char
"Nice One Program"
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{ clrscr();
const int MAX=80;
char ln[MAX];
ifstream obj("rvnow.txt");
while(!obj.eof())
{
obj.getline(ln,MAX);
cout<<ln<<endl;
}
obj.close();
getch();
}
"Writing into File CHAR by CHAR"
"Nice One Program"
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
void main()
{ clrscr();
char ch[]="hello this is the Demo For writing char by char into file";
int l=strlen(ch);
ofstream obj("charbychar.txt");
for(int i=0;i<l;i++)
{
obj.put(ch[i]);
}
cout<<"File Written Char by char:";
obj.close();
getch();
}
"Reading Line By Line From FILE"
"Nice One Program"
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{ clrscr(); const int MAX=80;
char ln[MAX];
ifstream obj("rvnow.txt");
while(!obj.eof())
{
obj.getline(ln,MAX);
cout<<ln<<endl;
}
obj.close();
getch();
}
"Program To Write Multiple Lines To FILE"
"Nice One Program"
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{ clrscr();
ofstream obj("rvnow.txt");
obj<<"Hello its First line\n";
obj<<"Second line is here\n";
obj<<"Third Line is Here\n";
cout<<"File Written";
obj.close();
getch();
}
Virtual Destructor
"Nice One Program"
//RULE
//RUlE:Virtual Destructor are used if want to delete the base class pointer to object of derived class
//if virtual dest not used normal destructor will be called and delte the base class part not the derived part
#include<iostream>
using namespace std;
class base
{
public:
base(){ }
virtual ~base(){
cout<<"Base destructor";
}
};
class dervd:public base
{
public:
dervd(){ }
~dervd(){
cout<<"Derived Destructor\n";
}
};
int main()
{
base *ptr=new dervd();
delete ptr;
return 0;
}
PART2:Virtual Member Function With Pointers
"Nice One Program"
//RULE
//RUlE2:pointer to object of derived class are compatible with pointer to object of base class
//RULE1:Compiler will choose membr func according to content of ponter not the type
#include<iostream>
using namespace std;
class base
{
public:
virtual void show()
{cout<<"Base";
}
};
class derv1:public base
{
void show()
{
cout<<"Derived1";
}
};
class derv2:public base
{
void show()
{
cout<<"Derived2";
}
};
int main()
{
derv1 obj1;
derv2 obj2;
base *ptr;
ptr=&obj1;
ptr->show();
ptr=&obj2;
ptr->show();
return 0;
}