Oops

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 61

//STATICDATAMEMBER

#include<iostream>
usingnamespacestd;
classproduct
{
staticintcount;
intnumber;
public:
voidgetdata(inta)
{
number=a;
count++;
}
voidgetcount()
{
cout<<"\nCOUNT:";
cout<<count;
}
};
intproduct::count;
intmain()
{
producta,b,c;
a.getcount();
b.getcount();
c.getcount();
a.getdata(100);
b.getdata(200);
c.getdata(300);
cout<<"\nAFTERREADINGDATA....";
a.getcount();
b.getcount();
c.getcount();
cout<<"\n";
return0;
}

OUTPUT:
COUNT:0
COUNT:0
COUNT:0
AFTERREADINGDATA....
COUNT:3
COUNT:3
COUNT:3

//STATICMEMBERFUNCTION
#include<iostream>
usingnamespacestd;
classtest
{
intcode;
staticintcount;
public:
voidsetcode()
{
code=++count;
}
voidshowcode()
{
cout<<"\nObjectNUmber="<<code<<"\n";
}
staticvoidshowcount()
{
cout<<"count="<<count<<"\n";
}
};
inttest::count;
intmain()
{
testt1,t2;
t1.setcode();
t2.setcode();
test::showcount();
testt3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
return0;
}

OUTPUT:
count=2
count=3
ObjectNumber=1
ObjectNumber=2
ObjectNumber=3

//DEFAULTARGUMENTS
#include<iostream>
usingnamespacestd;
intmain()
{
floatamount;
floatvalue(floatamt,intn,floatr=0.15);
amount=value(5000.00,5);
cout<<amount;
return0;
}
floatvalue(floatamt,intn,floatr)
{
intyear=1;
floatsum=amt;
while(year<=n)
{
sum=sum*(1+r);
year=year+1;
}
return(sum);
}

OUTPUT:
10056.8

//FRIENDFUNCTION
#include<iostream>
usingnamespacestd;
classABC;
classXYZ
{
public:
intx;
voidsetvalue(inti)
{
x=i;
}
friendvoidmax(XYZ,ABC);
};
classABC
{
inta;
public:
voidsetvalue(inti)
{
a=i;
}
friendvoidmax(XYZ,ABC);
};
voidmax(XYZm,ABCn)
{
if(m.x>=n.a)

cout<<m.x;
else
cout<<n.a;
}
intmain()
{
ABCabc;
abc.setvalue(10);
XYZxyz;
xyz.setvalue(20);
max(xyz,abc);
cout<<"\n";
return0;
}

OUTPUT:
20

//OPERATOROVERLOADING
#include<iostream>
usingnamespacestd;
classcomplex
{
intreal;
intimag;
public:
voidsetdata(intval,intval1)
{
real=val;
imag=val1;
}
voidshow()
{
if(imag>=0)
cout<<"complexnumberis"<<real<<"+"<<imag<<"i"<<endl;
else
cout<<"complexnumberis"<<real<<imag<<"i"<<endl;
}
complexoperator+(complexop2);
complexoperator(complexop2);
complexoperator++();

complexoperator++(intx);
};
complexcomplex::operator+(complexop2)
{
complextemp;
temp.real=op2.real+real;
temp.imag=op2.imag+imag;
returntemp;
};
complexcomplex::operator(complexop2)
{
complextemp;
temp.real=op2.realreal;
temp.imag=op2.imagimag;
returntemp;
}
complexcomplex::operator++(intx)
{
++real;
++imag;
return*this;
}
complexcomplex::operator++()
{
real++;
imag++;
return*this;
}
intmain()
{
complexob1,ob2,ob3;
ob1.setdata(5,10);
ob2.setdata(10,5);
cout<<"beforeaddition\n";
ob1.show();
ob2.show();
ob3=ob1+ob2;
cout<<"afteraddition\n";
ob3.show();
cout<<"beforepreincrement\n";
ob1.show();
++ob1;
cout<<"afterpreincrement\n";
ob1.show();
cout<<"beforepostincrement\n";
ob2.show();
ob2++;
cout<<"afterpostincrement";
ob2.show();
cout<<"beforesubtraction";

ob1.show();
ob2.show();
ob3=ob1ob2;
cout<<"aftersubtraction";
ob3.show();
return0;
}

OUTPUT:
beforeaddition
complexnumberis5+10i
complexnumberis10+5i
afteraddition
complexnumberis15+15i
beforepreincrement
complexnumberis5+10i
afterpreincrement
complexnumberis6+11i
beforepostincrement
complexnumberis10+5i
afterpostincrementcomplexnumberis11+6i
beforesubtractioncomplexnumberis6+11i
complexnumberis11+6i
aftersubtractioncomplexnumberis55i

//MARTIXIMPLEMENTATION
#include<iostream>
usingnamespacestd;
classmatrix
{
inti,j;
intelement[3][3];
public:
matrix(){};
matrix(inttmat[3][3])
{
for(i=0;i<3;i++)
for(j=0;j<3;j++)
element[i][j]=tmat[i][j];
}
matrix(matrix&m)

{
for(i=0;i<3;i++)
for(j=0;j<3;j++)
element[i][j]=m.element[i][j];
}
matrixoperator=(matrix&m1)
{
for(i=0;i<3;i++)
for(j=0;j<3;j++)
element[i][j]=m1.element[i][j];
}
~matrix()
{
cout<<"Destructoriscalled\n";
}
voiddisplay()
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<element[i][j]<<"\t";
}
cout<<"\n";
}
cout<<"\n";
}
};
intmain()
{
inta[3][3]={1,2,3,4,5,6,7,8,9};
intb[3][3]={4,5,18,7,6,2,3,9,1};
matrixm1(a);
matrixm2(b);
matrixm3(m1);
matrixm4;
m4=m2;
m1.display();
m2.display();
m3.display();
m4.display();
return0;
}

OUTPUT:
Destructoriscalled
1
2
3

4
5
6

7
8
9

4
7
3

5
6
9

18
2
1

1
4
7

2
5
8

3
6
9

4
7
3

5
6
9

18
2
1

Destructoriscalled
Destructoriscalled
Destructoriscalled

//OVERLOADINGNEWANDDELETEOPERATOR
#include<iostream>
#include<cstdlib>
usingnamespacestd;
#defineTABLESPACE_SIZE32
classtest
{
inti;
public:
staticchar*tablespace;

staticchar*currentptr;
staticchar*oldptr;
staticboolfirsttime;
test():i(0)
{
cout<<"\n\t\tConstructoriscalled";
}
~test()
{
cout<<"\n\t\tDestructoriscalled";
};
void*operatornew(size_tsize);
voidoperatordelete(void*p,size_tsize);
};
char*test::currentptr;
char*test::oldptr;
char*test::tablespace;
booltest::firsttime=true;
void*test::operatornew(size_tsize)
{
if(test::firsttime)
{
test::tablespace=(char*)::newchar[TABLESPACE_SIZE];
test::currentptr=test::tablespace;
printf("\nTest::tablespacepointeris%u\n",test::tablespace);
printf("\nCurrentpointeris%u\n",test::currentptr);
printf("\noldpointeris%u\n",test::oldptr);
test::firsttime=false;
}
if((test::currentptr+size)>(test::tablespace+TABLESPACE_SIZE))
{
cout<<"\nMemoryallocationfailure\n";
getchar();
exit(0);
}
test::oldptr=test::currentptr;
test::currentptr=test::currentptr+size;
cout<<"\nMemoryallocated\n";
printf("\nCurrentpointeris%u\n",test::currentptr);
printf("\nOldpointeris%u\n",test::oldptr);
returntest::oldptr;
}
voidtest::operatordelete(void*p,size_tsize)
{
::deletep;
}
intmain()
{
while(true)
newtest;

return0;
}

OUTPUT:
Test::tablespacepointeris136036360

Currentpointeris136036360
oldpointeris0
Memoryallocated
Currentpointeris136036364
Oldpointeris136036360
Constructoriscalled
Memoryallocated
Currentpointeris136036368
Oldpointeris136036364
Constructoriscalled
Memoryallocated
Currentpointeris136036372
Oldpointeris136036368
Constructoriscalled
Memoryallocated
Currentpointeris136036376
Oldpointeris136036372
Constructoriscalled
Memoryallocated
Currentpointeris136036380
Oldpointeris136036376
Constructoriscalled
Memoryallocated
Currentpointeris136036384
Oldpointeris136036380

Constructoriscalled
Memoryallocated
Currentpointeris136036388

Oldpointeris136036384
Constructoriscalled
Memoryallocated
Currentpointeris136036392
Oldpointeris136036388
Constructoriscalled
Memoryallocationfailure

//TEMPLATEOFLINKEDLIST
#include<iostream>
template<typenameT>
classdouble_linked
{
structnode
{
Tdata;
node*prev;
node*next;
node(Tt,node*p,node*n):data(t),prev(p),next(n){}
};
node*head;
node*tail;
public:
double_linked():head(NULL),tail(NULL){}
template<intN>
double_linked(T(&arr)[N]):head(NULL),tail(NULL)
{
for(inti(0);i!=N;++i)
push_back(arr[i]);
}
boolempty()const{return(!head||!tail);}
operatorbool()const{return!empty();}
voidpush_back(T);
voidpush_front(T);
Tpop_back();
Tpop_front();
~double_linked()
{
while(head)
{
node*temp(head);
head=head>next;
deletetemp;
}
}
};
template<typenameT>
voiddouble_linked<T>::push_back(Tdata)
{
tail=newnode(data,tail,NULL);
if(tail>prev)
tail>prev>next=tail;
if(empty())
head=tail;
}
template<typenameT>
voiddouble_linked<T>::push_front(Tdata)

{
head=newnode(data,NULL,head);
if(head>next)
head>next>prev=head;
if(empty())
tail=head;
}
template<typenameT>
Tdouble_linked<T>::pop_back()
{
if(empty())
throw("double_linked:listempty");
node*temp(tail);
Tdata(tail>data);
tail=tail>prev;
if(tail)
tail>next=NULL;
else
head=NULL;
deletetemp;
returndata;
}
template<typenameT>
Tdouble_linked<T>::pop_front()
{
if(empty())
throw("double_linked:listempty");
node*temp(head);
Tdata(head>data);
head=head>next;
if(head)
head>prev=NULL;
else
tail=NULL;
deletetemp;
returndata;
}
intmain()
{
intarr[]={4,6,8,32,19};
double_linked<int>dlist(arr);
dlist.push_back(11);
dlist.push_front(100);
while(dlist)
std::cout<<dlist.pop_back()<<""<<"\n";

doublearray[]={41.2,58.2,56.1,23.4};
double_linked<double>d1list(array);
d1list.push_back(81.2);
d1list.push_front(12.0);
while(d1list)
std::cout<<d1list.pop_back()<<""<<"\n";}

OUTPUT:
11
19
32
8
6
4
100
81.2
23.4
56.1
58.2
41.2
12

//BUBBLESORT
#include<iostream>
usingnamespacestd;
template<classX>
voidbubble(X*items,intcount)
{
Xt;
for(inta=1;a<count;a++)
for(intb=count1;b>=a;b)
if(items[b1]>items[b])
{
t=items[b1];
items[b1]=items[b];
items[b]=t;
}
}
intmain()
{
intiarray[7]={7,5,4,3,9,8,6};
doubledarray[5]={4.3,2.5,0.9,10.2,3.0};
cout<<"Hereisunsortedintegerarray:\n";
for(inti=0;i<7;i++)
cout<<iarray[i]<<'';
cout<<endl;
bubble(iarray,7);
cout<<"Hereissortedintegerarray:\n";
for(inti=0;i<7;i++)
cout<<iarray[i]<<'';
cout<<endl;
cout<<"Hereisunsorteddoublearray:\n";
for(inti=0;i<5;i++)
cout<<darray[i]<<'';
cout<<endl;
bubble(darray,5);
cout<<"Hereissorteddoublearray:\n";
for(inti=0;i<5;i++)
cout<<darray[i]<<'';
cout<<endl;
return0;
}

OUTPUT:
Hereisunsortedintegerarray:
7543986
Hereissortedintegerarray:
3456789
Hereisunsorteddoublearray:
4.32.50.910.23
Hereissorteddoublearray:
0.92.534.310.2

//MERGESORT
#include<iostream>
#include<iomanip>
usingnamespacestd;
template<classt>
classsort
{
ta[10];
public:
voidget(int);
voidmerge(int,int);
voidmergesort(int,int,int);
voiddisplay(int);
};
template<classt>
voidsort<t>::get(intn)
{
inti;
cout<<"\n\nEnterthearrayelements:";
for(i=1;i<=n;i++)
cin>>a[i];
}
template<classt>
voidsort<t>::display(intn)
{
inti;
cout<<"\nThesortedarrayis\n";
for(i=1;i<=n;i++)
cout<<a[i]<<setw(5);
}
template<classt>
voidsort<t>::merge(intlow,inthigh)
{
intmid;
if(low<high)
{
mid=(low+high)/2;

merge(low,mid);
merge(mid+1,high);
mergesort(low,mid,high);
}
}
template<classt>
voidsort<t>::mergesort(intlow,intmid,inthigh)
{
tb[10];
inth,i,j,k;
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h=h+1;
}
else
{
b[i]=a[j];
j=j+1;
}
i=i+1;
}
if(h>mid)
{
for(k=j;k<=high;k++)
{
b[i]=a[k];
i=i+1;
}
}
else
{
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i=i+1;
}
}
for(k=low;k<=high;k++)
a[k]=b[k];
}
voidmain()
{
intn;
cout<<"\n\t\tMERGESORTUSINGTEMPLATES";

cout<<"\n\t\t~~~~~~~~~~~~~~~~~~~~~~~";
sort<int>n1;
sort<float>n2;
cout<<"\nEnterthearraysize:";
cin>>n;
n1.get(n);
n1.merge(1,n);
n1.display(n);
n2.get(n);
n2.merge(1,n);
n2.display(n);}

OUTPUT:
Enterthearrayelements:4
5
2
1
3
Thesortedarrayis
12345

//QUICKSORT
#include<iostream>
#include<iomanip>
usingnamespacestd;
template<classw>
classquick
{
wa[50];
intn;
public:
voidget();
voidsort(int,int);
intpartition(int,int);
voidput();
};
template<classw>
voidquick<w>::get()
{
inti;
cout<<"\nEnterthenoofterms:";
cin>>n;
cout<<"\nEnterthevalues:\n";
for(i=1;i<=n;i++)
cin>>a[i];
sort(1,n);
}

template<classw>
voidquick<w>::sort(intp,intq)
{
intj;
if(p<q)
{
j=partition(p,q+1);
sort(p,j1);
sort(j+1,q);
}
}
template<classw>
intquick<w>::partition(intm,intp)
{
inti,j,t;
wv;
v=a[m];
i=m;j=p;
do
{
do
i++;
while(a[i]<v);
do
j;
while(a[j]>v);
if(i<j)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
while(i<j);
a[m]=a[j];
a[j]=v;
returnj;
}
template<classw>
voidquick<w>::put()
{
inti;
for(i=1;i<=n;i++)
cout<<a[i]<<"\n";
}
intmain()
{
quick<int>q1;
quick<float>q2;
cout<<"\n\t\tQUICKSORTUSINGTEMPLATES";

cout<<"\n\t\t~~~~~~~~~~~~~~~~~~~~~~~";
q1.get();
cout<<"\n\nSortedarrayofintegervalues:\n";
q1.put();
q2.get();
cout<<"\n\nSortedarrayoffloatingvalues:\n";
q2.put();
return0;
}

OUTPUT:
Enterthenoofterms:5
Enterthevalues:
2
5
3
4
1
Sortedarrayoffloatingvalues:
1
2
3
4
5

//STACKCLASS
#include<iostream>
usingnamespacestd;
classstack
{
intstackpointer;
intstackarray[10];
public:
stack()
{
stackpointer=0;
}
voidpush(intvalue)
{
try{

if(stackpointer==9)throw9;
}
catch(int){
cout<<"stackoverflow\n";
return;
}
stackarray[stackpointer]=value;
stackpointer++;
}
intpop()
{try{
if(stackpointer==0)throw0;}
catch(int){
cout<<"stackunderflow\n";
return0;
}
stackpointer;
returnstackarray[stackpointer];
}};
intmain()
{
stacks1;
s1.push(10);
s1.push(20);
cout<<s1.pop()<<endl;
cout<<s1.pop()<<endl;
}

OUTPUT:
20
10

//minimumspanningtreeusingkruskalalgorithm
#include<iostream>
usingnamespacestd;
classkruskal
{
private:
intn;//noofnodes
intnoe;//noedgesinthegraph
intgraph_edge[100][4];
inttree[10][10];
intsets[100][10];
inttop[100];
public:
voidread_graph();
voidinitialize_span_t();
voidsort_edges();
voidalgorithm();
intfind_node(int);
voidprint_min_span_t();
};
voidkruskal::read_graph()
{
cout<<"*************************************************\n"
<<"Thisprogramimplementsthekruskalalgorithm\n"
<<"*************************************************\n";
cout<<"Entertheno.ofnodesintheundirectedweightedgraph::";
cin>>n;
noe=0;
cout<<"Entertheweightsforthefollowingedges::\n";
for(inti=1;i<=n;i++)
{
for(intj=i+1;j<=n;j++)
{
cout<<"<"<<i<<","<<j<<">::";
intw;
cin>>w;
if(w!=0)
{
noe++;
graph_edge[noe][1]=i;
graph_edge[noe][2]=j;
graph_edge[noe][3]=w;
}
}
}

//printthegraphedges
cout<<"\n\nTheedgesinthegivengraphare::\n";
for(inti=1;i<=noe;i++)
cout<<"<"<<graph_edge[i][1]
<<","<<graph_edge[i][2]
<<">::"<<graph_edge[i][3]<<endl;
}
voidkruskal::sort_edges()
{
/****Sorttheedgesusingbubblesortinincreasingorder**************/
for(inti=1;i<=noe1;i++)
{
for(intj=1;j<=noei;j++)
{
if(graph_edge[j][3]>graph_edge[j+1][3])
{
intt=graph_edge[j][1];
graph_edge[j][1]=graph_edge[j+1][1];
graph_edge[j+1][1]=t;
t=graph_edge[j][2];
graph_edge[j][2]=graph_edge[j+1][2];
graph_edge[j+1][2]=t;
t=graph_edge[j][3];
graph_edge[j][3]=graph_edge[j+1][3];
graph_edge[j+1][3]=t;
}
}
}
//printthegraphedges
cout<<"\n\nAftersortingtheedgesinthegivengraphare::\n";
for(inti=1;i<=noe;i++)
cout<<"<"<<graph_edge[i][1]
<<","<<graph_edge[i][2]
<<">::"<<graph_edge[i][3]<<endl;
}
voidkruskal::algorithm()
{
//>makeasetforeachnode
for(inti=1;i<=n;i++)
{
sets[i][1]=i;
top[i]=1;
}
cout<<"\nThealgorithmstarts::\n\n";
for(inti=1;i<=noe;i++)
{
intp1=find_node(graph_edge[i][1]);

intp2=find_node(graph_edge[i][2]);
if(p1!=p2)
{
cout<<"Theedgeincludedinthetreeis::"
<<"<"<<graph_edge[i][1]<<","
<<graph_edge[i][2]<<">"<<endl<<endl;
tree[graph_edge[i][1]][graph_edge[i][2]]=graph_edge[i][3];
tree[graph_edge[i][2]][graph_edge[i][1]]=graph_edge[i][3];
//Mixthetwosets
for(intj=1;j<=top[p2];j++)
{
top[p1]++;
sets[p1][top[p1]]=sets[p2][j];
}
top[p2]=0;
}
else
{
cout<<"Inclusionoftheedge"
<<"<"<<graph_edge[i][1]<<","
<<graph_edge[i][2]<<">"<<"formsacyclesoitisremoved\n\n";
}
}
}
intkruskal::find_node(intn)
{
for(inti=1;i<=noe;i++)
{
for(intj=1;j<=top[i];j++)
{
if(n==sets[i][j])
returni;
}
}
return1;
}
intmain()
{
kruskalobj;
obj.read_graph();
obj.sort_edges();
obj.algorithm();
return0;
}

//output
*************************************************
Thisprogramimplementsthekruskalalgorithm
*************************************************
Entertheno.ofnodesintheundirectedweightedgraph::4
Entertheweightsforthefollowingedges::
<1,2>::3
<1,3>::4
<1,4>::2
<2,3>::5
<2,4>::1
<3,4>::3
Theedgesinthegivengraphare::
<1,2>::3
<1,3>::4
<1,4>::2
<2,3>::5
<2,4>::1
<3,4>::3
Aftersortingtheedgesinthegivengraphare::
<2,4>::1
<1,4>::2
<1,2>::3
<3,4>::3

<1,3>::4
<2,3>::5
Thealgorithmstarts::
Theedgeincludedinthetreeis::<2,4>
Theedgeincludedinthetreeis::<1,4>
Inclusionoftheedge<1,2>formsacyclesoitisremoved
Theedgeincludedinthetreeis::<3,4>
Inclusionoftheedge<1,3>formsacyclesoitisremoved
Inclusionoftheedge<2,3>formsacyclesoitisremoved

//Typeconversioncomplextodouble,intandinttodouble
#include<complex>
#include<iostream>
usingnamespacestd;
intmain()
{
inta=7;
cout<<((double)a/2)<<endl;//typecasting
complex<double>c_double(2,3);
complex<int>c_int(2,3);
c_double*=2;
cout<<c_double<<endl;
c_double=c_int;
cout<<c_double<<endl;
complex<double>c1(0.0,1.0),c2;//complexnumberswithdoublecomponents
c2=pow(c1,2.0);
cout<<"Thesquareof"<<c1<<"is"<<c2<<endl;
}

//output

3.5
(4,6)
(2,3)
Thesquareof(0,1)is(1,1.22461e16)

//insertionsort
#include<iostream>
#include<string>
#include<cstdlib>
usingnamespacestd;
constintSIZE=5;
template<classt>
voidprintarray(tarray[],intsize);
template<classt>
voidinsertionsort(tarray[],intsize);
intmain(void)
{
stringstringarray[SIZE]={"beta","gamma","alpha","epsilon","delta"};
cout<<"unsortedarray:"<<endl;
printarray(stringarray,SIZE);
insertionsort(stringarray,SIZE);
cout<<"sortedarray:"<<endl;
printarray(stringarray,SIZE);
intintarray[SIZE]={10,9,3,54,90};
cout<<"unsortedarray:"<<endl;
printarray(intarray,SIZE);
insertionsort(intarray,SIZE);
cout<<"sortedarray:"<<endl;
printarray(intarray,SIZE);
cin.get();
returnEXIT_SUCCESS;
}
template<classt>
voidprintarray(tarray[],intsize)
{
for(inti=0;i<size;i++)
{
cout<<array[i]<<""<<endl;
}
}
template<classt>
voidinsertionsort(tarray[],intsize)
{
for(inti=1;i<size;i++)
{
for(intj=i;j>0;j)
if(array[j]<array[j1])
swap(array[j],array[j1]);
elsebreak;
}
}

//insertionsort
unsortedarray:
beta
gamma
alpha
epsilon
delta
sortedarray:
alpha
beta
delta
epsilon
gamma
unsortedarray:
10
9
3
54
90
sortedarray:
3
9
10
54
90

//providingrandomaccessusingseek
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
usingnamespacestd;
classcustomer
{
intcustid;
stringname;
public:
customer()
{
custid=0;
name="dummy";
}
customer(inttempcustid,stringtempname)
{
custid=tempcustid;
name=tempname;
}
friendostream&operator<<(ostream&tempout,customertempcustomer)
{
tempout<<tempcustomer.custid<<""<<tempcustomer.name<<endl;
}
};
intmain()
{
fstreamcustomerfile("cust.dat",ios::in|ios::out|ios::trunc|ios::binary);
customerlara(1,"Briancharleslara");
customerbeckham(2,"Davidbeckham");
customersteffi(3,"Steffigraf");
customeranswer1,answer2;
customerfile.write((char*)&lara,sizeof(lara));
customerfile.write((char*)&beckham,sizeof(beckham));
customerfile.write((char*)&steffi,sizeof(customer));
cout<<"demoofswappingrecordsofafileusinggetandputpointers\n";
cout<<"recordno1isasfollows\n";
customerfile.seekg(0,ios::beg);
if(!customerfile.read((char*)&answer1,sizeof(customer)))
cout<<"errorinreadingfile";
cout<<answer1;
cout<<"recordno3isasfollows\n";
customerfile.seekg(2*sizeof(customer),ios::beg);
customerfile.read((char*)&answer2,sizeof(customer));
cout<<answer2;
cout<<"nowletusswaprecord1withrecord3\n";

customerfile.seekp(0,ios::beg);
customerfile.write((char*)&answer2,sizeof(customer));
customerfile.seekp(2*sizeof(customer),ios::beg);
customerfile.write((char*)&answer1,sizeof(customer));
cout<<"swappedrecordno1isasfollows\n";
customerfile.seekg(0,ios::beg);
customerfile.read((char*)&answer1,sizeof(customer));
cout<<answer1;
cout<<"swappedrecordno3isasfollows\n";
customerfile.seekg(2*sizeof(customer),ios::beg);
customerfile.read((char*)&answer2,sizeof(customer));
cout<<answer2;
customerfile.close();
return0;
}

//output
demoofswappingrecordsofafileusinggetandputpointers
recordno1isasfollows
1Briancharleslara
recordno3isasfollows
3Steffigraf
nowletusswaprecord1withrecord3
swappedrecordno1isasfollows
3Steffigraf
swappedrecordno3isasfollows
1Briancharleslara

//Dealingwithbinaryfiles
#include<iostream>
#include<fstream>
usingnamespacestd;
structstudent
{
introllno;
charname[30];
charaddress[40];
};
voidreadstudent(student&tempstud)
{
cout<<"\nentertherollno:";
cin>>tempstud.rollno;
cout<<"\nentername:";
cin>>tempstud.name;
cout<<"\nenteraddress:";
cin>>tempstud.address;
cout<<"\n";
}
intmain()
{
structstudentMCA_student_out;
ofstreamMCA_studfile_out;
MCA_studfile_out.open("MCA.dat",ios::out|ios::binary|ios::trunc);
if(!MCA_studfile_out.is_open())
cout<<"filecannotbeopened\n";
charcontinue1='y';
do
{
readstudent(MCA_student_out);
MCA_studfile_out.write((char*)&MCA_student_out,sizeof(structstudent));
if(MCA_studfile_out.fail())
cout<<"doyouwanttocontinuey/n:";
cin>>continue1;
}
while(continue1!='n');
MCA_studfile_out.close();
return0;
}

//output
entertherollno:1
entername:lara
enteraddress:srilanka

//DynamiccastTestapplicationofRTTI

#include<iostream>
usingnamespacestd;
classshape
{
intlinestyle;
intfillcolor;
public:
virtualvoiddraw()
{
cout<<"shapeisdraw\n";
}
};
classcircle:publicshape
{
intradius;
intcenterpointX;
intcenterpointY;
public:
voiddraw()
{
cout<<"circleisdrawn\n";
}
};
classdot:publicshape
{
intcX;
intcY;
public:
voiddraw()
{
cout<<"dotisdrawn\n";
}
};
classrectangle:publicshape
{
intcL;
intcB;
public:
voiddraw()
{
cout<<"rectangleisdrawn\n";
}

};
classsquare:publicshape
{
intcS;
public:
voiddraw()
{
cout<<"squareisdrawn\n";
}
};
classellipse:publicshape
{
intradius;
intcenterpointX;
intcenterpointY;
public:
voiddraw()
{
cout<<"ellipseisdrawn\n";
}
};
classpolygon:publicshape
{
intcSIDE;
intcANGLE;
public:
voiddraw()
{
cout<<"polygonisdrawn\n";
}
};
intmain()
{
shapesomeshape,*ptrshape;
circlering,*ptrcircle;
ptrshape=&ring;
ptrcircle=dynamic_cast<circle*>(ptrshape);
if(ptrcircle)cout<<"castissuccessful\n";
elsecout<<"testisnotsuccessful\n";
ptrshape=&someshape;
ptrcircle=dynamic_cast<circle*>(ptrshape);
if(ptrcircle)cout<<"castissuccessful\n";
elsecout<<"testisnotsuccessful\n";
dotrod,*ptrdot;
ptrshape=&rod;

ptrdot=dynamic_cast<dot*>(ptrshape);
if(ptrdot)cout<<"castissuccessful\n";
elsecout<<"testisnotsuccessful\n";
ptrshape=&someshape;
ptrdot=dynamic_cast<dot*>(ptrshape);
if(ptrdot)cout<<"castissuccessful\n";
elsecout<<"testisnotsuccessful\n";
rectanglebox,*ptrrectangle;
ptrshape=&box;
ptrrectangle=dynamic_cast<rectangle*>(ptrshape);
if(ptrrectangle)cout<<"castissuccessful\n";
elsecout<<"testisnotsuccessful\n";
ptrshape=&someshape;
ptrrectangle=dynamic_cast<rectangle*>(ptrshape);
if(ptrrectangle)cout<<"castissuccessful\n";
elsecout<<"testisnotsuccessful\n";
squaresq,*ptrsquare;
ptrshape=&sq;
ptrsquare=dynamic_cast<square*>(ptrshape);
if(ptrsquare)cout<<"castissuccessful\n";
elsecout<<"testisnotsuccessful\n";
ptrshape=&someshape;
ptrsquare=dynamic_cast<square*>(ptrshape);
if(ptrsquare)cout<<"castissuccessful\n";
elsecout<<"testisnotsuccessful\n";
ellipseel,*ptrellipse;
ptrshape=&el;
ptrellipse=dynamic_cast<ellipse*>(ptrshape);
if(ptrellipse)cout<<"castissuccessful\n";
elsecout<<"testisnotsuccessful\n";
ptrshape=&someshape;
ptrellipse=dynamic_cast<ellipse*>(ptrshape);
if(ptrellipse)cout<<"castissuccessful\n";
elsecout<<"testisnotsuccessful\n";
polygonpo,*ptrpolygon;
ptrshape=&po;
ptrpolygon=dynamic_cast<polygon*>(ptrshape);
if(ptrpolygon)cout<<"castissuccessful\n";
elsecout<<"testisnotsuccessful\n";
ptrshape=&someshape;
ptrpolygon=dynamic_cast<polygon*>(ptrshape);
if(ptrpolygon)cout<<"castissuccessful\n";
elsecout<<"testisnotsuccessful\n";
}

//output
castissuccessful
testisnotsuccessful
castissuccessful
testisnotsuccessful
castissuccessful
testisnotsuccessful
castissuccessful
testisnotsuccessful
castissuccessful
testisnotsuccessful
castissuccessful
testisnotsuccessful

//Queueimplementation
#include<iostream>
usingnamespacestd;
classqueue
{
intelement;
queue*next;
public:
queue*enqueue(queue*,int);
queue*dequeue(queue*);
voidqueue_display(queue*);
}*head,*tail,object;queue*queue::enqueue(queue*head,intkey)
{
queue*temp;
temp=newqueue;
temp>element=key;
temp>next=NULL;
if(head==NULL)
head=temp;
else
tail>next=temp;
tail=temp;
returnhead;
}

queue*queue::dequeue(queue*head)
{
queue*temp;
if(head==NULL)
{
cout<<"\nitisimpossibletodequeueanelementas";
returnNULL;
}
elseif(head>next==NULL)
{
cout<<"\ntheelementdequeuedfromthequeueis:"<<head>element<<endl;
returnNULL;
}
else
{
cout<<"\ntheelementdequeuedfromthequeueis"<<head>element<<endl;
temp=head>next;
head=temp;
cout<<"\ntheelementsofqueueafterdequeueingare\n";
returnhead;
}
}

voidqueue::queue_display(queue*head)
{

if(head!=NULL)
{
while(head>next!=NULL)
{
cout<<head>element<<">";
head=head>next;
}
cout<<head>element;
cout<<endl;
}
else
cout<<"thequeueisempty\n";
}
voidchoice()
{
intkey,ch;
head=tail=NULL;
cout<<"\nchoosetheoperation\n";
cout<<"\n1.enqueue\t2.dequeue\t3.exit\n\n";
cin>>ch;
while(ch!=3)
{
switch(ch)
{
case1:

cout<<"\nenterthekeytobeinserted\n";
cin>>key;
head=object.enqueue(head,key);
cout<<"\ntheelementsofqueueafterinserting"<<key<<"are\n";
object.queue_display(head);
break;
case2:
head=object.dequeue(head);
object.queue_display(head);
break;
case3:
break;
default:
cout<<"\nentercorrectchoice\n";
break;
}
cout<<"\nchoosetheoperation\n";
cout<<"\n1.enqueue\t2.dequeue\t3.exit\n\n";
cin>>ch;
}
}
intmain()
{
choice();
return0;
}

//output
choosetheoperation
1.enqueue

2.dequeue

3.exit

1
enterthekeytobeinserted
10
theelementsofqueueafterinserting10are
10

choosetheoperation
1.enqueue

2.dequeue

3.exit

1
enterthekeytobeinserted
20
theelementsofqueueafterinserting20are
10>20
choosetheoperation
1.enqueue
2

2.dequeue

3.exit

theelementdequeuedfromthequeueis10
theelementsofqueueafterdequeueingare
20

//Matrixvectormultiplication
#include<iostream>
usingnamespacestd;
classvector
{
inta[10][10],b[10][10],c[100][100],m,n,p,q;
public:
voidinput(void);
voiddisplay(void);
friendvoidmul(vector&v);
};
voidvector::input(void)
{
inti,j;
cout<<"Enterm&nvalues:";
cin>>m>>n;
cout<<"EntertheAmatrixvalues"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cin>>a[i][j];
}
cout<<"Enterp&qvalues"<<endl;
cin>>p>>q;
cout<<"EntertheBmatrixvalue";
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
cin>>b[i][j];
}
}
voidvector::display(void)
{
inti,j;
cout<<"Theresultantmatrixis\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<c[i][j]<<"\t";
cout<<"\n";
}
}
voidmul(vector&v)
{
intk,i,j;

for(i=0;i<v.m;i++)
{
for(j=0;j<v.n;j++)
{
v.c[i][j]=0;
for(k=0;k<v.n;k++)
v.c[i][j]=v.c[i][j]+v.a[i][k]*v.b[k][j];
}
}
}
intmain()
{
vectorv1;
v1.input();
mul(v1);
v1.display();
return0;
}

//Output
Enterm&nvalues:33
EntertheAmatrixvalues
123
456
789
Enterp&qvalues:31
EntertheBmatrixvalue
1
2
3
Theresultantmatrixis
14
0
0

32
0
0

50
0
0

You might also like