Computer Practical File: Name: Belwin Benni Class: XII-A
Computer Practical File: Name: Belwin Benni Class: XII-A
PRACTICAL
FILE
Name: Belwin Benni
Class: XII-A
Page | - 0 -
CERTIFICATE
This is to certify that Roll No.___________, student of class XII-A, has successfully
completed computer science practical under the guidance of Ms. Payal Sahgal during the
Signature of Signature of
External examiner computer science teacher
Page | - 1 -
INDEX
2. Constructors
3. Function Overloading
4. Inheritance
6. Pointers
7. Arrays
9. SQL
Page | - 2 -
CLASSES AND
OBJECTS
Page | - 3 -
Define a class RESORT with the following description:
Private members:
Rno
name
charges
days
compute () a function to calculate and return amount as days*charges
and if this amount is more than 11000 then it is calculated
as 1.02*days*charges
Public members:
getinfo () a function to enter Rno, name, charges and days
dispinfo () a function to display Rno, name, charges, days, and amount
(amount to be displayed by calling the function compute ()
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class Resort
{
int Rno;
char name[25];
float charges;
int days;
float compute()
{
float amount=days*charges;
if(amount>11000)
amount=1.02*days*charges;
return amount;
}
public:
void getinfo()
{
cout<<"Enter the 'Rno'";
cin>>Rno;
cout<<"Enter the name:";
gets(name);
cout<<"Enter the charges: ";
cin>>charges;
cout<<"Enter the no. of days: ";
cin>>days;
}
Page | - 4 -
void dispinfo()
{
cout<<"Rno: "<<Rno;
cout<<"\nName: "<<name;
cout<<"\nCharges: "<<charges;
cout<<"\nDays: "<<days;
float amount=compute();
cout<<"\nAmount: "<<amount;
}
};
void main()
{
clrscr();
Resort R1;
R1.getinfo();
R1.dispinfo();
getch();
}
Page | - 5 -
Green vegetarian
Yellow contains
egg
Red non-
vegetarian
Public members:
foodin() to allow user to enter values for code, foodname, sticker and call
the function gettype to assign respective foodtype
foodout() to allow the user to view the contents of all data members
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class Supply
{
int code;
char foodname[10], sticker[10], foodtype[10];
void gettype()
{
if(strcmpi(sticker,"green")==0)
strcpy(foodtype,"vegetarian");
else if(strcmpi(sticker,"yellow")==0)
strcpy(foodtype,"contains egg");
else if(strcmpi(sticker,"red")==0)
strcpy(foodtype,"non-vegetarian");
}
public:
void foodin()
{
cout<<"Enter the food code: ";
cin>>code;
cout<<"Enter the food name: ";
gets(foodname);
cout<<"Enter the sticker colour: ";
gets(sticker);
gettype();
}
void foodout()
{
Page | - 6 -
cout<<"\nFood code: "<<code;
cout<<"\nFood name: "<<foodname;
cout<<"\nSticker colour: "<<sticker;
cout<<"\nFood type: "<<foodtype;
}
};
void main()
{
clrscr();
Supply s1;
s1.foodin();
s1.foodout();
getch();
}
Page | - 7 -
CONSTRUCTORS
Page | - 8 -
Define a class CLOTHING with the following description:
Private members:
code string
type string
size int
material string
price float
calc_price() to calculate and assign the value of price as follows
for clothing with cotton as material:
TYPE PRICE
Trouser Rs.1500
Shirt Rs.1200
for material other than cotton the above price gets reduced by 25%
Public members:
A constructor to assign initial values of code, type and material and invoke
the calc_price()
show() to display the contents of all the data members
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class Clothing
{
char code[10];
char type[10];
int size;
char material[10];
float price;
void calc_price()
{
if(strcmpi(material,"cotton")==0)
{
if (strcmpi(type,"trouser")==0)
price=1500;
else if(strcmpi(type,"shirt")==0)
price=1200;
}
else
{
if(strcmpi(type,"trouser")==0)
Page | - 9 -
price=1500-(1500*0.25);
else if(strcmpi(type,"shirt")==0)
price=1200-(1200*0.25);
}
}
public:
Clothing()
{
strcpy(code,"not assigned");
strcpy(type,"not assigned");
strcpy(material,"not assigned");
size=0;
price=0.0;
}
void enter()
{
cout<<"Enter the cloth code: ";gets(code);
cout<<"Enter the dress type: ";gets(type);
cout<<"Enter the cloth material: ";gets(material);
cout<<"Enter the size: ";cin>>size;
calc_price();
}
void show()
{
cout<<"\n\tCLOTH SUMMARY";
cout<<"\nCode: "<<code;
cout<<"\nType: "<<type;
cout<<"\nSize: "<<size;
cout<<"\nMaterial: "<<material;
cout<<"\nPrice: "<<price;
}
};
void main()
{
clrscr();
Clothing C1;
C1.enter();
C1.show();
getch();
}
Page | - 10 -
Define a class TOUR with the descriptions given below:
Private members:
tcode string
no_of_adults int
no_of_kids int
kilometres int
totalfare float
Public members:
A constructor to assign initial values – tcode with a value “null” and other
members 0.
A function assignfare() to calculate and assign total fares as follows:
FARE KM
500 >=1000
300 <1000 and
>=500
200 <500
For each kid the fare will be 50% of the fare
mentioned in the table.
entertour() to input values for tcode, no_of_adults, no_of_kids and
kilometres. Also to invoke the assignfare().
showtour() to display contents of all the data.
Page | - 11 -
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class Tour
{
char tcode[20];
int no_of_adults, no_of_kids, kilometers;
float totalfare;
public:
Tour()
{
strcpy(tcode,"null");
no_of_adults=0;
no_of_kids=0;
kilometers=0;
totalfare=0.0;
}
void assignfare()
{
float fare;
if(kilometers>=1000)
fare=500;
else if(kilometers>=500&&kilometers<1000)
fare=300;
else if(kilometers<500)
fare=200;
totalfare=fare*(no_of_adults+0.5*no_of_kids);
}
void entertour()
{
cout<<"Enter the tour code: ";gets(tcode);
cout<<"Enter the no. of adults: ";cin>>no_of_adults;
cout<<"Enter the no. of kids: ";cin>>no_of_kids;
cout<<"Enter the no. of kilometers: ";cin>>kilometers;
assignfare();
}
void showtour()
{
cout<<"\n\tTOUR SUMMARY";
cout<<"\nTour code: "<<tcode;
cout<<"\nNo. of adults: "<<no_of_adults;
Page | - 12 -
cout<<"\nNo. of kids: "<<no_of_kids;
cout<<"\nNo. of kilometers: "<<kilometers;
cout<<"\nTotal Fare: "<<totalfare;
}
};
void main()
{
clrscr();
Tour t1;
t1.entertour();
t1.showtour();
getch();
}
Page | - 13 -
FUNCTION
OVERLOADING
Page | - 14 -
Write a C++ program that uses an area function for calculation of area of a
triangle or a rectangle or a square. Number of sides (three for triangle, two
for rectangle and one for square) suggest
about the shape for which area is to be calculated.
#include<iostream.h>
#include<math.h>
#include<conio.h>
float area(float a, float b, float c)
{
float s, ar;
s=(a+b+c)/2;
ar=sqrt(s*(s-a)*(s-b)*(s-c));
return ar;
}
float area(float a, float b)
{
return a*b;
}
float area(float a)
{
return a*a;
}
void main()
{
clrscr();
int choice, s1, s2, s3, ar;
do
{
cout<<"\n\nArea Menu\n";
cout<<"1.Triangle\n";
cout<<"2.Rectangle\n";
cout<<"3.Square\n";
cout<<"4.Exit\n";
cout<<"Enter your choice";
cin>>choice;
switch(choice)
{
case 1: cout<<"Enter three sides: ";
cin>>s1>>s2>>s3;
ar=area(s1,s2,s3);
cout<<"The area is: "<<ar;
Page | - 15 -
break;
case 2: cout<<"Enter length and breadth: ";
cin>>s1>>s2;
ar=area(s1,s2);
cout<<"The area is: "<<ar;
break;
case 3: cout<<"Enter a side: ";
cin>>s1;
ar=area(s1);
cout<<"The area is: "<<ar;
break;
case 4: break;
default: cout<<"wrong choice";
}
}while(choice>0&&choice<4);
getch();
}
Page | - 16 -
INHERITANCE
Page | - 17 -
Create two derived classes called rectangle and isosceles triangle that
inherits from the class area_c1. Have each class include a function called
area that displays the area of a rectangle or isosceles triangle as
appropriate, Use parameterized constructor to initialise height and width.
#include<iostream.h>
#include<conio.h>
class Area_c1
{
public:
double h,w;
};
class Rectangle: public Area_c1
{
public:
Rectangle(double x,double y)
{
h=x;
w=y;
}
void Area()
{
clrscr();
cout<<"\n\tArea of Rectangle = "<<h*w<<" sq units";
}
};
class Isosceles: public Area_c1
{
public:
Isosceles(double x,double y)
{
h=x;
w=y;
}
void Area()
{
cout<<"\n\n\tArea of triangle = "<<h*w/2<<" sq units";
}
};
void main()
{
clrscr();
Page | - 18 -
double a,b,c,d;
cout<<"\n\tEnter length of the rectangle: ";
cin>>a;
cout<<"\tEnter breadth of the rectangle: ";
cin>>b;
cout<<"\tEnter height of the triangle: ";
cin>>c;
cout<<"\tEnter base of the triangle: ";
cin>>d;
Rectangle r1(a,b);
Isosceles s1(c,d);
r1.Area();
s1.Area();
getch();
}
Page | - 19 -
DATA FILE
HANDLING
Page | - 20 -
Write a program to read the contents of the file story.txt and count the
number of lower case alphabets present in the file.
#include<fstream.h>
#include<ctype.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
int count=0;
ifstream f;
f.open("story.txt", ios::in);
while(!f.eof())
{
f>>ch;
if(islower(ch))
count++;
}
f.close();
cout<<"\n\tNo. of lower case alphabets: ";
cout<<count;
getch();
}
Write a program to count the number of words present in a text file named
para.txt.
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
int count=0;
ifstream f;
f.open("para.txt",ios::in);
while(!f.eof())
{
Page | - 21 -
f.get(ch);
if(ch==' ')
count++;
}
f.close();
cout<<"\n\tNo. of words are: ";
cout<<count+1;
getch();
}
Write a program to add, read, search, modify and delete data from a binary
file student.dat.
#include<process.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
class student
{
int Rno;
char name[20];
float marks;
public:
void getdata()
{
cout<<"\n\tEnter roll no.: ";
cin>>Rno;
cout<<"\tEnter name: ";
gets(name);
cout<<"\tEnter marks: ";
cin>>marks;
}
void putdata()
{
cout<<"\n\tRollno: "<<Rno<<"\n\tName: "<<name<<"\n\tMarks:
"<<marks<<"\n";
}
int getrollno()
{
return Rno;
}
Page | - 22 -
}s1;
void write()
{
clrscr();
ofstream f;
f.open("student.dat",ios::app|ios::binary);
char ans='y';
cout<<"\n\t\t===WRITE WINDOW===\n";
while(ans=='y'||ans=='Y')
{
s1.getdata();
f.write((char*)&s1, sizeof(s1));
cout<<"\n\t!!Do you want to continue writing?(y/n): ";
cin>>ans;
}
f.close();
}
void read()
{
clrscr();
ifstream f;
f.open("student.dat",ios::binary);
cout<<"\n\t\t===READ WINDOW===\n";
while(!f.eof())
{
f.read((char*)&s1, sizeof(s1));
if(f.eof())
break;
s1.putdata();
}
f.close();
}
void search()
{
clrscr();
int rno;
char ans='y';
cout<<"\n\t\t===SEARCH WINDOW===\n";
while(ans=='y'||ans=='Y')
{
char found='n';
ifstream f;
Page | - 23 -
f.open("student.dat");
cout<<"\n\t*Enter roll no: ";
cin>>rno;
while(!f.eof())
{
f.read((char*)&s1, sizeof(s1));
if(s1.getrollno()==rno)
{
s1.putdata();
found='y';
break;
}
}
f.close();
if(found=='n')
cout<<"\t*NOT FOUND\n";
cout<<"\n\t!!Do you want to continue searching?(y/n): ";
cin>>ans;
}
}
void modify()
{
clrscr();
fstream fio;
char ans='y';
long pos;
int rn;
cout<<"\n\t\t===MODIFY WINDOW===\n";
while(ans=='y'||ans=='Y')
{
char found='n';
fio.open("student.dat", ios::in|ios::out);
cout<<"\n\t*Enter roll no. to be modified: ";
cin>>rn;
while(!fio.eof())
{
pos=fio.tellg();
fio.read((char*)&s1,sizeof(s1));
if(fio.eof())
break;
if(s1.getrollno()==rn)
{
Page | - 24 -
found='y';
s1.getdata();
fio.seekg(pos);
fio.write((char*)&s1, sizeof(s1));
}
}
if(found=='n')
cout<<"\t*Not Found\n";
fio.close();
fio.open("student.dat",ios::in|ios::binary);
cout<<"\n\t!!DATA IN THE FILE\n";
while(!fio.eof())
{
fio.read((char*)&s1,sizeof(s1));
if(fio.eof())
break;
s1.putdata();
}
fio.close();
cout<<"\n\t!!Do you want to continue modifying?(y/n): ";
cin>>ans;
}
}
void delete()
{
clrscr();
ifstream fin;
ofstream fout;
int rn;
char ans='y';
cout<<"\n\t\t===DELETE WINDOW===\n";
while(ans=='y'||ans=='Y')
{
char found='n';
fin.open("student.dat", ios::in);
fout.open("temp.dat", ios::out);
cout<<"\n\t*Enter roll no. to be deleted: ";
cin>>rn;
while(!fin.eof())
{
fin.read((char*)&s1,sizeof(s1));
if(fin.eof())
Page | - 25 -
break;
if(s1.getrollno()==rn)
found='y';
else
fout.write((char*)&s1,sizeof(s1));
}
if(found=='n')
cout<<"\t!!NOT FOUND\n";
fin.close();
fout.close();
remove("student.dat");
rename("temp.dat","student.dat");
fin.open("student.dat",ios::in|ios::binary);
cout<<"\n\t!!DATA IN THE FILE\n";
while(!fin.eof())
{
fin.read((char*)&s1,sizeof(s1));
if(fin.eof())
break;
s1.putdata();
}
fin.close();
cout<<"\n\t!!Do you wish to continue deleting?(y/n): ";
cin>>ans;
}
}
void main()
{
clrscr();
int choice;
char ans='y';
while(ans=='y'||ans=='Y')
{
clrscr();
cout<<"\n\t\t\t\t*STUDENT MENU*\n";
cout<<"\n\t\t\t\t 1.Write\n";
cout<<"\t\t\t\t 2.Read\n";
cout<<"\t\t\t\t 3.Search\n";
cout<<"\t\t\t\t 4.Modify\n";
cout<<"\t\t\t\t 5.Delete\n";
cout<<"\t\t\t\t 6.Exit\n";
cout<<"\n\t\t\t***Enter your choice(1/2/3/4/5/6): ";
Page | - 26 -
cin>>choice;
if(choice==1)
write();
else if(choice==2)
read();
else if(choice==3)
search();
else if(choice==4)
modify();
else if(choice==5)
delete();
else if(choice==6)
exit(0);
else
cout<<"\n\t!!Enter a Valid Choice!!";
cout<<"\n\t!!!!!Do you wish to re-run the program?(y/n): ";
cin>>ans;
}}
Page | - 27 -
Page | - 28 -
POINTERS
Page | - 29 -
Write a program to store numbers in a dynamic array where number of
elements are known at runtime.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size, *num;
cout<<"\n\tHow many numbers do you want to enter?: ";
cin>>size;
num=new int[size];;
for(int i=0; i<size; i++)
{
cout<<"\n\tEnter the number: ";
cin>>num[i];
}
cout<<"\n\tNumbers in the array: ";
for(i=0; i<size; i++)
cout<<"\t"<<num[i];
delete []num;
getch();
}
Page | - 30 -
ARRAYS
Page | - 31 -
Write a program to search an item in a sorted array using BINARY SEARCH.
#include<iostream.h>
#include<conio.h>
int BSearch(int arr[],int size,int item)
{
int beg, mid, last;
beg=0;
last=size-1;
while(beg<=last)
{
mid=(beg+last)/2;
if(item==arr[mid])
return mid;
else if(item>arr[mid])
beg=mid+1;
else
last=mid-1;
}
return-1;
}
void main()
{
clrscr();
int size, arr[50], item;
cout<<"\n\tEnter the length of the array: ";
cin>>size;
cout<<"\n\tEnter "<<size<<" numbers: \n";
for(int i=0;i<size;i++)
{
cout<<"\t";
cin>>arr[i];
}
cout<<"\n\tEnter the no. to be searched: ";
cin>>item;
int val=BSearch(arr,size,item);
if(val==-1)
cout<<"\n\tNot Found";
else
cout<<"\n\tThe value was found at position: "<<val+1;
getch();}
Page | - 32 -
Write a program to sort an integer array using SELECTION SORT.
#include<iostream.h>
#include<conio.h>
void SelSort(int num[], int size)
{
int small, pos=0, temp;
for(int i=0; i<size-1; i++)
{
small=num[i];
pos=i;
for(int j=i+1; j<size; j++)
{
if(num[j]<small)
{
small=num[j];
pos=j;
}
}
temp=num[i];
num[i]=num[pos];
num[pos]=temp;
}
}
void main()
{
clrscr();
int size=0, num[50];
cout<<"\n\tEnter the length of the array: ";
cin>>size;
cout<<"\n\tEnter "<<size<<" numbers: \n";
for(int i=0;i<size;i++)
Page | - 33 -
{
cout<<"\t";
cin>>num[i];
}
SelSort(num,size);
cout<<"\n\tSORTED ARRAY:\n\n\t";
for(int j=0;j<size;j++)
{
cout<<num[j]<<"\t";}
getch();
}
#include<iostream.h>
#include<conio.h>
void BubbleSort(int num[], int size)
{
int temp;
for(int i=0;i<size;i++)
{
for(int j=0;j<size-1; j++)
{
if(num[j]>num[j+1])
{
temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
}
Page | - 34 -
void main()
{
clrscr();
int size, num[50];
cout<<"\n\tEnter the length of the array: ";
cin>>size;
cout<<"\n\tEnter "<<size<<" numbers: \n";
for(int i=0;i<size;i++)
{
cout<<"\t";
cin>>num[i];
}
BubbleSort(num,size);
cout<<"\n\tSORTED ARRAY:\n\n\t";
for(i=0;i<size;i++)
cout<<num[i]<<"\t";
getch();
}
Page | - 35 -
STACK AND QUEUE
Page | - 36 -
Linked list implementation of STACK using a class.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
struct node
{char name[20];
int age;
node*link;
}*pr;
class stack
{node *top;
public:
stack()
{top=NULL;}
void stackpush();
void stackpop();
void display();
};
void stack::stackpush()
{char ans='y';
while(ans=='y'||ans=='Y')
{ptr=new node;
ptr->link=NULL;
cout<<"\n\tEnter name: ";
gets(ptr->name);
cout<<"\tEnter age: ";
cin>>ptr->age;
if(top==NULL)
top=ptr;
else
{ptr->link=top;
top=ptr;
}
cout<<"\n\tcontinue y/n? ";
cin>>ans;
}
}
void stack::stackpop()
{if(top==NULL)
cout<<"\tunderflow";
else
Page | - 37 -
{cout<<"\tElement being popped is: ";
cout<<top->name<<":"<<top->age;
ptr=top;
top=top->link;
delete ptr;
}
}
void stack::display()
{clrscr();
cout<<"\tData in the list is:\n";
ptr=top;
while(ptr!=NULL)
{cout<<"\t"<<ptr->name<<"->"<<ptr->age<<"\n";
ptr=ptr->link;
}
}
void main()
{clrscr();
stack s1;
int ch=0;
while(ch!=4)
{cout<<"\n\t\tSTACK MENU";
cout<<"\n\t1.Add";
cout<<"\n\t2.Delete";
cout<<"\n\t3.Display";
cout<<"\n\t4.Exit";
cout<<"\n\tEnter Your Choice: ";
cin>>ch;
if(ch==1)
s1.stackpush();
else if(ch==2)
s1.stackpop();
else if(ch==3)
s1.display();
getch();
}
}
Page | - 38 -
Page | - 39 -
Linked list implementation of QUEUE using a class.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
struct node
{char name[20];
int age;
node*link;
}*ptr;
class queue
{node *rear, *front;
public:
queue()
{rear=NULL;
front=NULL;
}
void queins();
void quedel();
void display();
};
void queue::queins()
{char ans='y';
while(ans=='y'||ans=='Y')
{ptr=new node;
ptr->link=NULL;
cout<<"\n\tEnter name: ";
gets(ptr->name);
cout<<"\tEnter age: ";
cin>>ptr->age;
if(rear==NULL)
front=rear=ptr;
else
{rear->link=ptr;
rear=ptr;
}
cout<<"\n\tcontinue y/n? ";
cin>>ans;
}
}
void queue::quedel()
Page | - 40 -
{if(front==NULL)
cout<<"\tunderflow";
else
{cout<<"\tElement being popped is: ";
cout<<front->name<<":"<<front->age;
ptr=front;
if(front==rear)
front=rear=NULL;
else
front=front->link;
delete ptr;
}
}
void queue::display()
{clrscr();
cout<<"\tData in the list is:\n";
ptr=front;
while(ptr!=NULL)
{cout<<"\t"<<ptr->name<<"->"<<ptr->age<<"\n";
ptr=ptr->link;
}
}
void main()
{clrscr();
queue q1;
int ch=0;
while(ch!=5)
{cout<<"\n\t\tQUEUE MENU";
cout<<"\n\t1.Add";
cout<<"\n\t2.Delete";
cout<<"\n\t3.Display";
cout<<"\n\t4.Exit";
cout<<"\n\tEnter Your Choice: ";
cin>>ch;
if(ch==1)
q1.queins();
else if(ch==2)
q1.quedel();
else if(ch==3)
q1.display();
else if(ch==4)
break; getch();}
Page | - 41 -
Page | - 42 -
SQL
Page | - 43 -
Create the tables vehicle and travel and write SQL commands for the
questions given below:
1. Display names of those travellers whose travel date is after 20th April 2016.
Page | - 44 -
3. Display name, travel date and no. of passengers for those who are not
travelling by vehicle code 102.
4. Display no. and name of travellers for vehicle code 103, 104 and 105.
5. Display name and km travelled for travellers whose name ends with ‘n’.
7. Display how many travellers are having km travelled greater than 100.
Page | - 45 -
9. Display details of travellers who have travelled between 1st April and 31st
December 2015.
10. Display traveller name along with type of vehicle and per km chargers for all
travellers.
11. Display no. , name and travel date for those travellers whose per km
charges are more than 50.
12. Display traveller name along with their total charges (km travelled*per km)
in ascending order of name.
13. Display the name and per km charges for those travellers whose no. is
greater than 105.
Page | - 46 -
14. Change the vehicle code of Ahmad Khan to 103.
15. Increase the no. of passengers by 2 for travellers having vehicle code 103.
Page | - 47 -