Thursday, 17 May 2018

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

}