Tuesday, 12 June 2018

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();
}



Monday, 11 June 2018

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();

}

Monday, 21 May 2018

"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();

}







Sunday, 20 May 2018

"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();

}







Thursday, 17 May 2018

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;

}




Part1:Normal 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 igonre the content of pointer and choose the member function of the type of pointer

#include<iostream>
using namespace std;
class base
{
public:
void show()
{cout<<"Base";
}

};

class derv1:public base
{
void show()
{
cout<<"Derived";
}
};

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;

}




Thursday, 10 May 2018

Finding Longest Subsequence

"Nice One Program"
In this program we will have 2 Strings S1,S2 and Will Find The largest subsequence In both of These




#include<iostream>
#include<string.h>

using namespace std;

int lcs(char*,char*,int,int);
int max(int a,int b)
{
return((a>b)?a:b);
}
int main()
{    char s1[50],s2[50];
  int m,n;
cout<<"Enter String 1:";
cin>>s1;
cout<<"Enter String 2:";
cin>>s2;
m=strlen(s1);
n=strlen(s2);


int x=lcs(s1,s2,m,n);
cout<<"Lngest SUBSEQ: "<<x;
return 0;
}


lcs(char* s1,char* s2,int m,int n)
{
if(m==0||n==0)
return 0;
else if(s1[m-1]==s2[n-1])
return 1+lcs(s1,s2,m-1,n-1);
else
return(max(lcs(s1,s2,m,n-1),lcs(s1,s2,m-1,n)));
}











Monday, 16 April 2018

To Read Text From File
"Nice One Program"

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{       clrscr();

int a;
char ch;
double d;
char cobj[5];
ifstream rvobj("rv.txt");
rvobj>>a>>ch>>d>>cobj;
cout<<"Data Read From File\n";
cout<<a<<endl;
cout<<ch<<endl<<d<<endl<<cobj;


getch();

}





Thursday, 29 March 2018

To Create A Text File And Write Into It

"Nice One Program"

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{       clrscr();

int a=10;
char ch='a';
double d=10.5;
char cobj[5]="ravi";
ofstream rvobj("rv.txt");
rvobj<<a;
rvobj<<ch;
rvobj<<d;
rvobj<<cobj;
cout<<"\nFile Written";



getch();

}

//File Created will be look like this






And Output:



Monday, 26 March 2018

To Create Friend Function

"Nice One Program"

#include<iostream.h>
#include<conio.h>
class A;                   //must declare first before use in B class
class B{
int b;
public:
B()
{ b=10;
}
friend void sum(A,B);

};
class A
{
int a;
public:
A()
{
a=20;
}
friend void sum(A,B);
};
void sum(A A1,B B1)
{
cout<<"Sum:"<<(A1.a+B1.b);

}
void main()
{         clrscr();
A obj1;
B obj2;
sum(obj1,obj2);
getch();
}










Saturday, 24 March 2018

To Add Own Header File in C++

"Nice One Program"

#include<iostream.h>
#include<conio.h>    
#include"myfile.h"

void main()                
{         clrscr();
int a=10,b=20;
int c=sum(a,b);                 //no prototype now create new file with extension .h
cout<<"Sum="<<c;      //again error now include file using ""

getch();
}


FILE WITH .H Exntension Will Have

int sum(int x,int y)
{
return (x+y);
}


Tuesday, 6 March 2018

Program For Copy Constructor

"Nice one Program"

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class point  {
public:          int x,y;
point() {x=0,y=0;        }
point(point &p)
{ x=p.x;
  y=p.y;
}

};
void main()
{clrscr();
point p1,p2;
p1.x=10;
p1.y=20;
p2=p1; //copy constructor called here
cout<<"p2.x="<<p2.x<<endl;
cout<<"p2.y="<<p2.y;
getch();

}



Assignment Operator Overloading ( +=)

"Nice One Program"

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class rv{ public:
int inch,feet;
rv(){ inch=0,feet=0;

}
void operator +=(rv dis)
{
feet=feet+dis.feet;
inch=inch+dis.inch;
if(inch>=12)
{ feet+=inch/12;
  inch%=12;

}


}};

void main()
{       clrscr();
rv dis1,dis2;
cout<<"Enter dis1: ";
cin>>dis1.feet;
cin>>dis1.inch;
cout<<"\nEnter dis2: ";
cin>>dis2.feet;
cin>>dis2.inch;
dis1+=dis2;

cout<<"\n Value of feet: "<<dis1.feet;
cout<<"\n value of inch:"<<dis1.inch;
getch();

}


Operator Overloading

Adding Two Objects

"Nice One Program"

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class rv{ public:
int val;
rv(){ val=1;

}
rv operator +(rv ss)
{     cout<<"func called Will returb object\n";
rv temp;
temp.val=val+ss.val;
return temp;
}};

void main()
{       clrscr();
rv obj1,obj2,obj3;

obj3=obj1+obj2;
cout<<"\n Value of obj3: "<<obj3.val;
getch();
}


Operator Overloading:-

Concatination of Two Strings

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<string.h>
class rv{ public:
char str[20];
rv(){ strcpy(str," ");

}
rv(char s[])
{
strcpy(str,s);
}
rv operator +(rv ss) const
{     cout<<"func called\n";
rv temp;
if((strlen(str)+strlen(ss.str))<20)
{   strcpy(temp.str,str);
    strcat(temp.str,ss.str);
}
else
cout<<"Overflow";
return temp;
}
};

void main()
{       clrscr();
rv st1="hello",st2=" ravi",st3;

st3=st1+st2;
cout<<st3.str;
getch();
}