Anuj C++ Practical File
Anuj C++ Practical File
Computer Science
(C++)
Submitted By :- ANUJ
Class - XII A
Roll no. –
INDEX
PROGRAMS:
Binary Search
Linear Search
Copy Constructor
Parameterised Constructor
To calculate area of a triangle using class (Nesting)
Pointer
Linear Queue
Circular Queue
To read content from a Binary File
To write content in the Binary File
To find absolute value of a number
To print area of rectangle and circle
To calculate sum using function overloading
Using same function to print different Data Types
To swap two numbers using class
. Program That Defines a Class String and
Overload Program
Program to Print Numbers From 1 to n Without
Using Loops
Travel plan [using class]
Publication [using class]
Merge two arrays
SQL COMMANDS:
Show database
Create database
To insert data and use of SELECT command
Creating and printing the table in descending
order
Alter the table with ADD command
Alter the table with DROP command
LIKE command
SELECT-ORDER BY command
CREATE VIEW command
PROGRAMS
1. Binary Search:
#include<conio.h>
#include<iostream.h>
int first,last,mid;
int found=0;
first=0;
last=N-1;
mid=int((first+last)/2);
if(a[mid]==data)
found=1;
if(a[mid]<data)
first=mid+1;
if(a[mid]>data)
last=mid-1;
return(found);
int main()
{
clrscr();
int x[10],data, i;
for(i=0;i<10;i++)
cin>>x[i];
cin>>data;
int res=Binary_Search(x,10,data);
if(res==1)
cout<<"\n found";
else
getch();
return 0;
/*
4 7 9 11 14 16 19 20 23
found
*/
2. Linear Search
#include<conio.h>
#include<iostream.h>
int i, found=0;
for(i=0;i<N;i++)
if(x[i]==data)
found=1;
return(found);
int main()
clrscr();
int x[10],data, i;
for(i=0;i<10;i++)
cin>>x[i];
}
cout<<"\n Enter No. to be searched:";
cin>>data;
int res=Linear_Search(x,10,data);
if(res==1)
cout<<"\n found";
else
getch();
return 0;
/* Enter 10 numbers:1
2 3 4 5 6 7 8 9 10
found */
3. Copy Constructor
#include<conio.h>
#include<iostream.h>
#include<string.h>
class student
int roll;
char name[30];
public:
student()
cout<<"\n Constructor:";
roll=10;
strcpy(name,"Rahul");
student(student &s)
roll=s.roll;
strcpy(name,s.name);
void input_void()
cin>>roll;
cin>>name;
void show_data()
cout<<roll;
cout<<"\n Name:";
cout<<name;
} };
int main()
student s;
s.show_data();
cout<<"\n";
student A(s);
A.show_data();
getch();
return 0;
/*
Constructor:
Roll no:10
Name:Rahul
Copy constructor:
Roll no:10
Name:Rahul */
4. Parametrised Constructor
#include<conio.h>
#include<iostream.h>
class read_constructor
int x;
public:
read_constructor(int a)
x=a;
void read_data()
cin>>x;
void show_data()
cout<<"Value of x:"<<x;
};
int main()
read_constructor obj(10);
obj.show_data();
getch();
return 0;
}
/*
Value of x:10
*/
5. Nesting
#include<conio.h>
#include<iostream.h>
class area
int b, h;
double ar;
public:
void input_data()
cin>>b;
cin>>h;
void calculate()
input_data();
ar=0.5*b*h;
}
void output()
calculate();
} };
int main()
clrscr();
area A;
A.output();
getch();
return 0;
} /*
Enter base:10
Enter height:5
Area of triangle:25 */
6. Pointers
#include<conio.h>
#include<iostream.h>
int* read()
int x;
x=45;
return(&x);
int main()
clrscr();
int *res;
res=read();
getch();
return 0;
/*
Value at res:0x8fc7ffee
*/
7. Linear Queue
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
//function prototype
void add(int &front, int &rear, int x[], int N, int value);
void main()
{ clrscr();
int x[100],front,rear,choice,value;
front=-1;
rear=-1;
do
cout<<"\n 1. Addition";
cout<<"\n 2. Deletion";
cout<<"\n 3. Display";
cout<<"\n 4. Exit";
cin>>choice;
switch(choice)
case 1:
cin>>value;
add(front,rear,x,100,value);
break;
case 2:
deletion(front,rear);
break;
case 3:
display(front,rear,x);
getch();
break;
case 4:
break;
default:
getch();
}while(choice!=4);
getch();
return ;
void add(int &front, int &rear, int x[], int N, int value)
if(rear==-1)
front=0;
rear=0;
x[rear]=value;
else
{ if(rear>=(N-1))
cout<<"full";
else
rear=rear+1;
x[rear]=value;
} } }
if(front==-1)
cout<<"Empty";
else
if(front==rear)
front=-1;
rear=-1; }
else
{ front=front+1; }
return;
int i;
if(front==-1)
cout<<"Empty";
else
for(i=front;i<=rear;i++)
cout<<setw(4)<<x[i];
return;
}/*
3. Display
4. Exit
Enter value 8
Queue Menu
1. Addition
2. Deletion
3. Display
4. Exit
2 5 8
Queue Menu
1. Addition
2. Deletion
3. Display
4. Exit
Queue Menu
1. Addition
2. Deletion
3. Display
4. Exit
5 8
Queue Menu
1. Addition
2. Deletion
3. Display
4. Exit
Enter value 9
Queue Menu
1. Addition
2. Deletion
3. Display
4. Exit
Enter your choice 3
5 8 9
Queue Menu
1. Addition
2. Deletion
3. Display
4. Exit
8. Circular Queue
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
//Function prototype
void main()
clrscr();
int x[100],m,data,choice;
m=0;
do
{
cout<<"\n 1. Push";
cout<<"\n 2. Pop";
cout<<"\n 3. Display";
cout<<"\n 4. Exit";
cin>>choice;
switch(choice)
case 1:
cin>>data;
push(x,100,m,data);
break;
case 2:
pop(m);
break;
case 3:
display(x,m);
getch();
break;
case 4:
break;
default:
getch();
}while(choice!=4);
if(M>=N)
cout<<"STACK full";
else
x[M]=data;
M=M+1;
return;
if(m<1)
else
m=m-1;
return;
int i;
if(m<1)
else
for(i=0;i<m;i++)
cout<<setw(6)<<x[i];
return;
} /*
2. Pop
3. Display
4. Exit
Enter choice:1
Enter value:4
STACK MENU
1. Push
2. Pop
3. Display
4. Exit
Enter choice:1
Enter value:6
STACK MENU
1. Push
2. Pop
3. Display
4. Exit
Enter choice:1
Enter value:8
STACK MENU
1. Push
2. Pop
3. Display
4. Exit
Enter choice:3
2 4 6 8
STACK MENU
1. Push
2. Pop
3. Display
4. Exit
Enter choice:2
STACK MENU
1. Push
2. Pop
3. Display
4. Exit
Enter choice:3
2 4 6
STACK MENU
1. Push
2. Pop
3. Display
4. Exit
Enter choice:4
*/
#include<conio.h>
#include<iostream.h>
#include<string.h>
struct student{
int roll;
char name[30];
char address[80];
};
int main()
clrscr();
ifstream obj("student.dat");
student s;
while(obj.read((char*)&s, sizeof(student)))
cout<<"\nRoll no:"<<s.roll;
cout<<"\n Name:"<<s.name;
cout<<"\n Address:"<<s.address;
obj.close();
getch();
return 0;
} /*
Roll no:1
Name:ashish
Address:delhi
*/
#include<conio.h>
#include<iostream.h>
struct student
int roll;
char name[30];
char address[80];
};
int main()
{clrscr();
ifstream fin("student.dat");
ofstream fout("temp.dat");
int troll;
student s;
cin>>troll;
while(fin.read((char*)&s, sizeof(student)))
{ if(troll==s.roll)
{ cout<<"\n Enter New name:";
cin>>s.name;
cin>>s.address; }
fout.write((char*)&s, sizeof(student));
fin.close();
fout.close();
//remove("student.dat");
//rename("temp.dat","student.dat");
getch();
return 0;
}/*
*/
#include <iostream.h>
int absolute(int);
float absolute(float);
int main()
{ int a = -5;
float b = 5.5;
cout << "Absolute value of " << a << " = " << absolute(a) << endl;
cout << "Absolute value of " << b << " = " << absolute(b);
return 0;
if (var < 0)
var = -var;
return var;}
var = -var;
return var;}
#include<iostream.h>
#include<conio.h>
class CalculateArea
{ public:
void Area(int r)
};
void main()
{ CalculateArea C;
C.Area(5);
C.Area(5,3);
C.Area(7,2.1f);
C.Area(4.7f,2); }
#include<iostream.h>
#include<conio.h>
class Addition
{ public:
{ cout<<a+b; }
{ cout<<a+b+c; }
};
void main()
{ clrscr();
Addition obj;
obj.sum(10, 20);
cout<<endl;
#include <iostream>
class printData
void print(double f)
void print(char* c)
};
int main(void) {
printData pd;
pd.print(5);
pd.print(500.263);
pd.print("Hello C++");
return 0; }
15. To swap two numbers using class
#include<iostream.h>
#include<conio.h>
class swap
{
int a,b;
public:
void getdata();
void swapv();
void display();
};
void swap::getdata()
{ cout<<“Enter two numbers:”;
cin>>a>>b; }
void swap::swapv()
{ a=a+b;
b=a-b;
a=a-b; }
void swap::display()
{ cout<<“a=”<<a<<“tb=”<<b; }
main()
{
clrscr();
swap s;
s.getdata();
cout<<“nBefore swap:n”;
s.display();
s.swapv();
cout<<“nnAfter swap:n”;
s.display();
getch();
return 0;
}
16. Program That Defines a Class String and Overload
[Operator to Compare Two Strings]
#include<iostream>
#include<stdio.h> //used
for gets()
#include<string.h> //used for strcmp()
using namespace std;
class String
{
char str[20];
public:
void
getdata() //function to read the string
{
gets(str);
}
//operator
function to overload comparison operator and compare two strings
int operator
==(String s)
{
if(!strcmp(str,s.str))
return
1;
return
0;
}
};
main()
{
String s1,s2;
if(s1==s2) //here
the operator function will be called
{ cout<<“nStrigs are Equaln”; }
else
{ cout<<“nStrings are Not Equaln”; }
return 0;
}
void newplan();
void showplan();
}travel;
void travelplan::travelplan()
{cout<<"enter the plancode"<<endl;
cin>>plancode;
cout<<endl;
cout<<"enter the place"<<endl;
gets(place);
cout<<endl;
cout<<"enter the no of travellers"<<endl;
cin>>noofpeople;
cout<<endl;
}
void travelplan::newplan()
{if(noofpeople<20)
{noofbus=1;}
else if((noofpeople>=20)&&(noofpeople<40))
{noofbus=2;}
else if(noofpeople>=40)
{noofbus=3;}
}
void travelplan::showplan()
{cout<<"Plan code : "<<plancode<<endl;
cout<<"Place : ";
puts(place);
cout<<endl;
cout<<"No of travellers : "<<noofpeople<<endl;
cout<<"No of bus : "<<noofbus<<endl;
}
void main()
{travel.newplan();
travel.showplan();
getch();
}
19. Publication [using class]
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
class publication
{
char title[20];
float price;
public:
void getdata()
{
cout<<"Enter Title"<<endl;
gets(title);
cout<<"Enter Price"<<endl;
cin>>price;
}
void putdata()
{
cout<<"Title"<<endl;
puts(title);
cout<<"Price"<<endl;
cout<<price;
}};
class book : protected publication
{
int pagecount;
public:
void putdata()
{
publication :: putdata();
cout<<endl<<"Pg Count"<<endl;
cout<<pagecount;
}
void getdata()
{
publication::getdata();
cout<<"Pg Count"<<endl;
cin>>pagecount;
}
};
class tape : protected publication
{
float time;
public:
void getdata()
{
publication :: getdata();
cout<<"Enter Time"<<endl;
cin>>time;
}
void putdata()
{
publication :: putdata();
cout<<endl<<"Time "<<endl;
cout<<time;
}
};
void main()
{
clrscr();
book b;
tape t;
int ch;
x:
{
cout<<endl<<"1. BOOK 2:Tape 3:Exit "<<endl;
cin>>ch;
switch(ch)
{
case 1:b.getdata();
b.putdata();
goto x;
case 2:t.getdata();
t.putdata();
goto x;
case 3:exit(0);
}
}
getch();
}
20. Merge two arrays
#include<iostream.h>
#include<conio.h>
int c[100],i,j,k;
class merged
{
int na,nb,a[50],b[50];
public:
void merge();
void display();
void input();
}g;
void merged::merge()
{
int j=0;
int i=na-1;
while((i>0)&&(j<nb))
{
if (b[j]<a[i])
{
c[k]=a[i];
i--;
k++;
}
if (b[j]>a[i])
{
c[k]=b[j];
j++;
k++;
}
if (b[j]==a[i])
{
c[k]=b[j];
j++;
k++;
c[k]=a[i];
i--;
k++;
}
}
if(i==-1)
{
while(j<nb)
{
c[k]=b[j];
j++;
k++;
}
}
if(j==nb)
{
while(i>=0)
{
c[k]=a[i];
i--;
k++;
}}
}
void merged::display()
{
for(i=0;i<na+nb;i++)
cout<<c[i];
}
void merged::input()
{
cout<<"Enter A Lim"<<endl;
cin>>na;
cout<<"Enter B Limit"<<endl;
cin>>nb;
cout<<"Enter array A"<<endl;
for(i=0;i<na;i++)
cin>>a[i];
cout<<"Enter Array B"<<endl;
for(j=0;j<nb;j++)
cin>>b[j];
}
void main()
{
clrscr();
g.input();
g.merge();
g.display(); }
SQL
COMMANDS
Show databases;
Create database;
Create and desc table
Insert & select
Select-Like
Select- order by
Create view