0% found this document useful (0 votes)
65 views

C++ Programs XII

The document appears to be an index from a C++ practical file submitted by a student named Harshit Mathur from St. Mary's Christian School. The index lists 30 programs with brief descriptions implemented in C++ covering topics like functions, classes, inheritance, pointers, arrays, sorting algorithms, and SQL commands. It also includes sample code for some of the programs and expected output. The document was submitted to teacher Ms. Achla Agarwal for review and contains the student and teacher's signatures.

Uploaded by

anurag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

C++ Programs XII

The document appears to be an index from a C++ practical file submitted by a student named Harshit Mathur from St. Mary's Christian School. The index lists 30 programs with brief descriptions implemented in C++ covering topics like functions, classes, inheritance, pointers, arrays, sorting algorithms, and SQL commands. It also includes sample code for some of the programs and expected output. The document was submitted to teacher Ms. Achla Agarwal for review and contains the student and teacher's signatures.

Uploaded by

anurag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

St Marys Christian School

C++Practical File

Submitted To:
By:

Submitted

Ms. Achla Agarwal


Harshit Mathur
XII-A
Roll no.-

INDEX
S.No Program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

To calculate the area of triangle, circle, rectangle


using function overloading
To implement the concept of class
To illustrate the concept of constructor and destructor
To implement constructor overloading
To implement multiple inheritance
To illustrate access control of inherited member in
privately derived class
To show the working of virtual base class
To create a single file and then display its content
To implement use of get() and put() function
To illustrate a function returning a pointer
To illustrate pointer and array
To implement binary search
To implement linear search
To implement selection sort
To implement bubble sort
To implement insertion sort
To implement merging
To find sum of two matrices
To find product of two matrices
To find transpose of a matrices
To insert an element in beginning of link list
To implement the concept of push() and pop() function
in array stack
To implement the concept of push() and pop() function
link stack
To show insertion and deletion from a linked queue
To show insertion and deletion in circular queue
To illustrate the use of create, insert and desc
command
To illustrate the use of alter, update and modify
command

Teacher
s
Signatu
re

28
29
30

To illustrate the use of group by and having clause


To illustrate the use of join command
To illustrate the use of drop and delete command

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;
rectangle:";

ar(a,b,x); break;

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


case 3:cout<<"enter radius of circle:";
cin>>a; ar(a); break;

case 2:cout<<"enter 2 side of

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

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.
#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{
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; };
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:
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;
};
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);
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;}

Output:

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++;}
if(line>22){line=0;}
}
return 0;
}
Output:

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:

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:

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;


else if(item>A[mid]) beg=mid+1;
else last=mid-1;
}
return -1;
}
Output:

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;
}
Output:

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

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;
}
}
cout<<"\nArray After Iteration-"<<++ctr<<"-is:";
for(int k=0;k<size;k++)
cout<<A[k]<<" "<endl;
}
}
Output:

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])

{
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:

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;
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:

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++)
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:

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++)
{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:

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];}
}
return 0;
}
Output:

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
{save=start; start=np;
np->next=save;}}
void disp(node*np)
{ while(np!=NULL)
{cout<<np->info<<"->";
np=np->next; }
cout<<"!!!!!!!!!\n"; }
Output:

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.

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


having clause.

Write a SQL program to illustrate the use of join


command.

Write a SQL program to illustrate the use of drop and


delete command.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy