Top Banner
St Mary’s Christian School C++Practical File 2015- 16 Submitted To: Submitted By: Ms. Achla Agarwal Harshit Mathur XII-A
42

C++ programs XII

Jan 27, 2016

Download

Documents

anuragr anuragr

they are c++ project with screenshot of output
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: C++ programs XII

C++Practical File

2015-16

Submitted To: Submitted By:

Ms. Achla Agarwal Harshit Mathur

XII-A

Roll no.-

Page 2: C++ programs XII

INDEX

S.No Program Teacher’sSignature

1 To calculate the area of triangle, circle, rectangle using function overloading

2 To implement the concept of class3 To illustrate the concept of constructor and destructor4 To implement constructor overloading5 To implement multiple inheritance6 To illustrate access control of inherited member in privately

derived class7 To show the working of virtual base class8 To create a single file and then display its content9 To implement use of get() and put() function10 To illustrate a function returning a pointer11 To illustrate pointer and array12 To implement binary search13 To implement linear search14 To implement selection sort15 To implement bubble sort16 To implement insertion sort17 To implement merging18 To find sum of two matrices19 To find product of two matrices20 To find transpose of a matrices21 To insert an element in beginning of link list 22 To implement the concept of push() and pop() function in array

stack23 To implement the concept of push() and pop() function link stack24 To show insertion and deletion from a linked queue25 To show insertion and deletion in circular queue26 To illustrate the use of create, insert and desc command27 To illustrate the use of alter, update and modify command28 To illustrate the use of group by and having clause29 To illustrate the use of join command30 To illustrate the use of drop and delete command

Page 3: C++ programs XII

Write a C++Program that calculate area of circle, rectangle and triangle using function overloading.

#include<iostream.h>

#include<math.h>

void ar(float a,float b,float c)

{float s; s=(a+b+c)/2;

cout<<"area of triangle is:"<<sqrt(s*(s-a)*(s-b)*(s-c)); }

void ar(float a,float b)

{cout<<"area of rectangle is:"<<a*b;}

float ar(float a)

{cout<<"area of circle is:"<<3.14*a*a; }

void main()

{float a,b,x;int c;

cout<<"calculate the area of\n1.triangle\n2.rectangle\n3.circle\nenter your choice:";

cin>>c;

switch(c)

{case 1:cout<<"enter the 3 side of tirangle:";

cin>>a>>b>>x; ar(a,b,x); break; case 2:cout<<"enter 2 side of rectangle:";

cin>>a>>b; ar(a,b); break;

case 3:cout<<"enter radius of circle:";

cin>>a; ar(a); break;

default:cout<<"wrong choice";}}

OUTPUT:

Page 4: C++ programs XII
Page 5: C++ programs XII

Write a C++Program to implement concept of class.

#include<iostream.h>

class harshit{public:

void showdata(int i,float j)

{

cout<<"code is "<<i<<", price is "<<j;

}

};

void main()

{

harshit m;

m.showdata(3,325.75);

}

OUTPUT:

Write a C++Program to illustrate the concept of constructor and destructor.

Page 6: C++ programs XII

#include<iostream.h>

#include<conio.h>

class harshit{

public:

harshit(int a,int b){cout<<constructing<<”\t”<<a<<"\t"<<b;}

~harshit(){cout<<"\ndestructing";}

};

void main(){

harshit s1(2,3);

}

Output:

Write a C++Program to implement constructor overloading.

#include<iostream.h>

class area{

Page 7: C++ programs XII

public:

area(int a){cout<<"area of square is:"<<a*a;}

area(int a,int b){cout<<"\narea of rectangle is:"<<a*b;}

};

void main(){

area s(2);

area r(3,4);

}

OUTPUT:

Write a C++Program to implement multiple inheritance.

#include<iostream.h>

class chair{ public:

int a; };

class direct{ public:

int b; };

Page 8: C++ programs XII

class compy:public chair,public direct

{public:int c;

void an(){cout<<"enter a,b,c respectively;\n";

cin>>a>>b>>c;

cout<<"a="<<a<<endl<<"b="<<b<<endl<<"c="<<c; }

};

void main()

{ compy a2;

a2.an(); }

Output:

Write a C++Program to illustrate access control of inherited member in the privately derived class.

#include<iostream.h>

class an{

public:int a; };

class meh: private an

{int b;

public:

Page 9: C++ programs XII

void har(){cout<<"enter a,b respectively;\n";

cin>>a>>b;

cout<<"a:"<<a<<",b:"<<b;}

};

void main()

{

meh a4;

a4.har();

}

Output:

Write a C++Program to show the working of virtual base class.

#include<iostream.h>

class base{public: int a;

};

class d1:virtual public base

{public: int b;

};

class d2:virtual public base

{public: int c;

Page 10: C++ programs XII

};

class d3:public d1,public d2

{public: int total;

};

void main(){d3 ob;

ob.a=23; ob.b=56; ob.c=79;

ob.total=ob.a+ob.b+ob.c;

cout<<ob.a<<"\t"<<ob.b<<"\t"<<ob.c<<"\t"<<ob.total;

}

Output:

Write a C++Program to create a single file and then display its content.

#include<iostream.h>

#include<fstream.h>

int main()

{ ofstream fout;

fout.open("Student",ios::out);

char name[90],ch; float marks=0.0;

for(int i=0;i<5;i++)

{ cout<<"student"<<i+1<<":\tName:";

cin.get(name,30); cout<<"\t\tmarks:";

cin>>marks; cin.get(ch);

Page 11: C++ programs XII

fout<<name<<'\n'<<marks<<"\n";

}

fout.close();

ifstream fin("Student",ios::in);

fin.seekg(0); cout<<"\n";

for(i=0;i<5;i++)

{ fin.get(name,30);

fin.get(ch); fin>>marks;

fin.get(ch);

cout<<"student name:"<<name<<"\tmarks:"<<marks<<endl;

}

fin.close();

return 0;}

Page 12: C++ programs XII

Output:

Page 13: C++ programs XII

Write a C++Program to implement the used of get() and put() function.

#include<iostream.h>

#include<fstream.h>

int main()

{ofstream fout;

fout.open("aschars",ios::app);

if(!fout)

{cout<<"the file cant be open\n";

return 1;

}

char ch;int line=0;

//write the characters

for(int i=33;i<128;i++)

{fout.put(((char)i));}

fout.close();

//now display the contents of the file

ifstream fin;

fin.open("aschars",ios::in);

fin.seekg(0);

for(i=33;i<128;i++)

{fin.get(ch);

cout<<" "<<i<<"=";

cout.put((char)(i));

if(!(i%8))

{cout<<endl;line++;}

Page 14: C++ programs XII

if(line>22){line=0;}

}

return 0;

}

Output:

Page 15: C++ programs XII

Write a C++Program to illustrate a function returning a pointer.

#include<iostream.h>

int*big(int&,int&);

int main()

{ int a,b,*c;

cout<<"enter two integers \n";

cin>>a>>b;

c=big(a,b);

cout<<"the bigger value is:"<<*c<<endl;

return 0;

}

int*big(int&x,int&y)

{ if(x>y)

return(&x);

else

return(&y);

}

Output:

Page 16: C++ programs XII

Write a C++Program to illustrate pointer and array.

#include<iostream.h>

int main()

{ int *ip[5];

int a=45,b=58,c=85,d=99,e=175;

ip[0]=&a;

ip[1]=&b;

ip[2]=&c;

ip[3]=&d;

ip[4]=&e;

for(int i=0;i<5;i++)

cout<<"the pointer ip["<<i<<"] points to "<<*ip[i]<<endl;

cout<<"the base address of array ip of pointers is"<<ip<<endl;

for(i=0;i<5;i++)

cout<<"the address stored in ip["<<i<<"] is"<<*ip[i]<<endl;

return 0;

}

Output:

Page 17: C++ programs XII

Write a C++Program to implement binary search.

#include<iostream.h>

int Bsearch(int[],int,int);

int main()

{ int A[50],item,n,index;

cout<<"enter desired array size...";

cin>>n;

cout<<"\nenter the array elements \n";

for(int i=0;i<n;i++)

cin>>A[i];

cout<<"enter the elments to b search for.";

cin>>item;

index=Bsearch(A,n,item);

if(index==-1)

cout<<"\nSorry!! given element NOT found";

else

cout<<"\n element found @ index :"<<index<<" & Position:"<<index+1<<endl;

return 0;

}

int Bsearch(int A[],int size,int item)

{ int beg,last,mid;

beg=0; last=size-1;

while(beg<=last)

{mid=(beg+last)/2;

if(item==A[mid]) return mid;

Page 18: C++ programs XII

else if(item>A[mid]) beg=mid+1;

else last=mid-1;

}

return -1;

}

Output:

Page 19: C++ programs XII

Write a C++Program to implement linear search.

#include<iostream.h>

int Lsearch(int[],int,int);

int main()

{ int A[50],item,n,index;

cout<<"enter desired array size...";

cin>>n;

cout<<"\nenter the array elements \n";

for(int i=0;i<n;i++)

cin>>A[i];

cout<<"enter the elments to b search for.";

cin>>item;

index=Lsearch(A,n,item);

if(index==-1)

cout<<"\nSorry!! given element NOT found";

else

cout<<"\n element found @ index :"<<index<<" & Position:"<<index+1<<endl;

return 0;

}

int Lsearch(int A[],int size,int item)

{for(int i=0;i<size;i++)

{if(A[i]==item)

return i;

}

return -1;

Page 20: C++ programs XII

}

Output:

Page 21: C++ programs XII

Write a C++Program to implement selection sort.

#include<iostream.h>

void SelSort(int[],int);

int main()

{int A[50],n;

cout<<"How many elements do u want to create array with.....";

cin>>n;

cout<<"Enter the elements ..\n";

for(int i=0;i<n;i++)

cin>>A[i];

SelSort(A,n);

cout<<"\n\nthe sorted array is as shown below.\n";

for(i=0;i<n;i++)

cout<<A[i]<<" ";

cout<<endl;

return 0;

}

void SelSort(int A[],int size)

{int small,pos,tmp;

for(int i=0;i<size;i++)

{small=A[i];

pos=i;

for(int j=i+1;j<size;j++)

{if(A[j]<small)

{small=A[j];

pos=j;}

Page 22: C++ programs XII

}

tmp=A[i];

A[i]=A[pos];

A[pos]=tmp;

cout<<"\nArray After pass-"<<i+1<<"-is:";

for(j=0;j<size;j++)

cout<<A[j]<<" "<endl;

}

}

Output:

Page 23: C++ programs XII

Write a C++Program to implement bubble sort.

#include<iostream.h>

void BubbleSort(int[],int);

int main()

{int A[50],n;

cout<<"hoW many elements do u want to create array with.....";

cin>>n;

cout<<"Enter the elements ..\n";

for(int i=0;i<n;i++)

cin>>A[i];

BubbleSort(A,n);

cout<<"\n\nthe sorted array is as shown below.\n";

for(i=0;i<n;i++)

cout<<A[i]<<" ";

cout<<endl;

return 0;

}

void BubbleSort(int A[],int size)

{int tmp,ctr=0;

for(int i=0;i<size;i++)

{for(int j=0;j<(size-1);j++)

{if(A[j]>A[j+1])

{

tmp=A[j];

A[j]=A[j+1];

A[j+1]=tmp;

Page 24: C++ programs XII

}

}

cout<<"\nArray After Iteration-"<<++ctr<<"-is:";

for(int k=0;k<size;k++)

cout<<A[k]<<" "<endl;

}

}

Output:

Page 25: C++ programs XII

Write a C++Program to implement insertion sort.

#include<iostream.h>

#include<limits.h>

void InsSort(int[],int);

int main()

{int A[50],n;

cout<<"hoW many elements do u want to create array with.....";

cin>>n;

cout<<"Enter the elements ..\n";

for(int i=1;i<=n;i++)

cin>>A[i];

InsSort(A,n);

cout<<"\n\nthe sorted array is as shown below.\n";

for(i=1;i<=n;i++)

cout<<A[i]<<" ";

cout<<endl;

return 0;

}

void InsSort(int A[],int size)

{int tmp,j;

A[0]=INT_MIN;

for(int i=1;i<=size;i++)

{ tmp=A[i];

j=i-1;

while(tmp<A[j])

{

Page 26: C++ programs XII

A[j+1]=A[j];

j--;

}

A[j+1]=tmp;

cout<<"\nArray After pass-"<<i<<"-is:";

for(int k=1;k<=size;k++)

cout<<A[k]<<" "<endl;

}

}

Output:

Page 27: C++ programs XII

Write a C++Program to implement merging.

#include<iostream.h>

void Merge(int[],int,int[],int,int[]);

int main()

{ int A[50],B[50],C[50],MN=0,M,N;

cout<<"how many elents do u want to create the first array with......{in acs order}\n" ;

cin>>M;

cout<<"enter the elements .\n";

for(int i=0;i<M;i++)

cin>>A[i];

cout<<"how many elents do u want to create the second array with......{in acs order}\n" ;

cin>>N;

cout<<"enter the elements .\n";

for(i=0;i<N;i++)

cin>>B[i];

MN=M+N;

Merge(A,M,B,N,C);

cout<<"\n\nthe merged array is as shown below:\n";

for(i=0;i<MN;i++)

cout<<C[i]<<" ";

cout<<endl;

return 0;

}

void Merge(int A[],int M,int B[],int N,int C[])

{ int a,b,c;

Page 28: C++ programs XII

for(a=0,b=0,c=0;a<M&&b<N;)

{ if(A[a]<B[b]) C[c++]=A[a++];

else C[c++]=B[b++];

}

if(a<M)

{while(a<M)

C[c++]=A[a++];}

if(b<N)

{while(b<N)

C[c++]=B[b++];}

}

Output:

Page 29: C++ programs XII

Write a C++Program to find sum of two matrices.

#include<iostream.h>

#include<process.h>

int main()

{int a[10][10],b[10][10],c[10][10];

int i,j,m,n,p,q;

cout<<"input rows & colomn of matrix A\n";

cin>>m>>n;

cout<<"input rows & colomn of matrix B\n";

cin>>p>>q;

if((m!=p)||(n!=q))

{cout<<"matrx cant b added";

exit(0); }

else

{cout<<"input Matrix A:\n";

for(i=0;i<m;i++)

{for(j=0;j<n;j++)

cin>>a[i][j];

}

cout<<"input Matrix B:\n" ;

for(i=0;i<p;i++)

{for(j=0;j<q;j++)

cin>>b[i][j];

}

for(i=0;i<m;i++)

{for(j=0;j<n;j++)

Page 30: C++ programs XII

c[i][j]+=a[i][j]*b[i][j]; }

cout<<"\nthe sum of 2 matrices is:\n" ;

for(i=0;i<m;i++)

{ cout<<endl;

for(j=0;j<n;j++)

cout<<" "<<c[i][j];

}

return 0;}}

Output:

Page 31: C++ programs XII

Write a C++Program to find product of two matrices.

#include<iostream.h>

#include<process.h>

int main()

{int a[10][10],b[10][10],c[10][10];

int i,j,m,n,p,q,ip;

cout<<"input rows & colomn of matrix A\n";

cin>>m>>n;

cout<<"input rows & colomn of matrix B\n";

cin>>p>>q;

if(n!=p)

{cout<<"matrx cant b added";

exit(0); }

else

{cout<<"input Matrix A:\n";

for(i=0;i<m;i++)

{for(j=0;j<n;j++)

cin>>a[i][j];

}

cout<<"input Matrix B:\n" ;

for(i=0;i<p;i++)

{for(j=0;j<q;j++)

cin>>b[i][j];

}

for(i=0;i<m;i++)

{for(j=0;j<q;j++)

Page 32: C++ programs XII

{c[i][j]=0;

for(ip=0;ip<n;ip++)

c[i][j]+=a[i][ip]*b[ip][j]; } }

cout<<"\nthe product of 2 matrices is:\n" ;

for(i=0;i<m;i++)

{ cout<<endl;

for(j=0;j<q;j++)

cout<<" "<<c[i][j]; }

return 0;}}

Output:

Page 33: C++ programs XII

Write a C++Program to find transpose of a matrix.

#include<iostream.h>

#include<process.h>

int main()

{int a[10][10],c[10][10];

int i,j,m,n;

cout<<"input rows & colomn of matrix \n";

cin>>m>>n;

cout<<"input Matrix A:\n";

for(i=0;i<m;i++)

{for(j=0;j<n;j++)

cin>>a[i][j];

}

cout<<"given matrix is:\n" ;

for(i=0;i<m;i++)

{cout<<endl;

for(j=0;j<n;j++)

cout<<a[i][j]<<" ";

}

cout<<"\nthe transpose of matrix is:\n" ;

for(i=0;i<m;i++)

{ cout<<endl;

for(j=0;j<n;j++)

{c[i][j]=a[j][i];

cout<<" "<<c[i][j];}

Page 34: C++ programs XII

}

return 0;

}

Output:

Page 35: C++ programs XII

Write a C++Program to insert an element in begging of link list.

#include<iostream.h>

#include<process.h>

struct node { int info; node*next; }*start,*newptr,*save,*ptr;

node*create_new_node(int);

void insert_beg(node*);

void disp(node*);

int main()

{start=NULL; int info; char ch='y';

while(ch=='y')

{cout<<"\n enter the info. for the new node........\n";

cin>>info; cout<<"\nCreating new node!!press enter to continues..\n";

newptr=create_new_node(info);

insert_beg(newptr);

cout<<"\n now the list is:\n";

disp(start);

cout<<"\nPress 'y ' to enter more nodes..........\n" ; cin>>ch; }

return 0; }

node*create_new_node(int n)

{ ptr=new node; ptr->info=n; ptr->next=NULL;

return ptr;}

void insert_beg(node*np)

{if(start==NULL)

start=np;

else

Page 36: C++ programs XII

{save=start; start=np;

np->next=save;}}

void disp(node*np)

{ while(np!=NULL)

{cout<<np->info<<"->";

np=np->next; }

cout<<"!!!!!!!!!\n"; }

Output:

Page 37: C++ programs XII

Write a SQL program to illustrate the use of create, insert and desc command.

Write a SQL program to illustrate the use of alter, update and modify command.

Page 38: C++ programs XII
Page 39: C++ programs XII

Write a SQL program to illustrate the use of group by & having clause.

Page 40: C++ programs XII

Write a SQL program to illustrate the use of join command.

Page 41: C++ programs XII

Write a SQL program to illustrate the use of drop and delete command.