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