7/31/2019 B.sc Computer Science 1st Sem c Lab Record
1/132
Design @ Crazy
UNIVERSITY INSTITUTE OF TECHNOLOGYUNIVERSITY OF KERALA
REGIONAL CENTRE
YEROOR
DEPARTMENT OF COMPUTER SCIENCE
LABORATORY RECORD
NAME :.
CLASS :.
REG. NO. :.
SUBJECT :.
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
2/132
Design @ Crazy
UNIVERSITY INSTITUTE OF TECHNOLOGY
UNIVERSITY OF KERALA
REGIONAL CENTRE
YEROOR
Certified that this is a bonafide record of the practical work done
by....Reg.No:....in the Computer
Laboratory forProgramming Lab-II during the academic year 2011-14 and submitted
for theII Semester B.Sc Computer Science Examination held at UIT Centre, Yeroor
on . . .. . . . . . . . . .
LECTURER IN CHARGE : PRINCIPAL
EXAMINERS:
1)
2)
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
3/132
Design @ Crazy
INDEX
Sl.No Program Name
Page
No Date
1Largest and smallest number
2Square of a number
3Factorial of a number
4Sum of digits of a number
5Reverse of a number
6Fibonacci series
7Prime or not
8Factorial of a number(class concept)
9Prime numbers (class concept)
10Bank transactions (class concept)
11Mark list (class concept)
12Copy constructor concept
13 Function overloading concept
14 Inline function program
15 Default constructor program
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
4/132
Design @ Crazy
16 Parameterized constructor program
17 Single inheritance concept
18 Multilevel inheritance concept
19 Multiple inheritance concept20 Hierarchical inheritance concept
21 Hybrid inheritance concept
22 Operator overloading concept
23 Friend function concept
24 Pure virtual function concept
25 Function template concept
26 Class template program
27 Exception handling program
28 File(Write program using file)
29 File Accessing(Write program using file)
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
5/132
PageNo..
Design@Crazy
Algorithm
1.Start
2.Print the number of terms
3.Read n
4.print the elements
5.for i=0 to n
6.read a[i]
7.for i=0 to n
8.if a[i]>l
l=a[i]
9.if a[i]
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
6/132
PageNo..
Design@Crazy
Title: Largest and smallest number
Aim: Program to find the largest and smallest number
Program
#include
#include
void main()
{
int a[50],i,n,l=0,s=100;
clrscr();
coutn;
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
7/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
8/132
PageNo..
Design@Crazy
Output
Enter the limit:10
Enter the elements:20
35
12
4
0
85
77
6
28
100
The largest element is:100
The smallest elemet is:0
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
9/132
PageNo..
Design@Crazy
Algorithm
1: start
2: read n
3: calculate square
4: print square
5: end
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
10/132
PageNo..
Design@Crazy
Title : Square of a number
Aim : Write a program to find square of a number
Program
#include
#include
Void main()
{
int n,s;
clrscr();
coutn;
s=n*n;
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
11/132
PageNo..
Design@Crazy
Algorithm
1.start
2.declare variables n,i,f
3. initialize f=1
4. read n
5. initialize i=1
6. if i
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
12/132
PageNo..
Design@Crazy
Title : Factorial of a number
Aim : Write a program to find factorial of a number
Program
#include
#include
void main()
{
int n,i,f;
f=1;
clrscr();
coutn;
for(i=1;i
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
13/132
PageNo..
Design@Crazy
Algorithm
1. Start2. Read n3. Initialize I,s=04. if(n>0)go to next step else go to step75. d=n%10,s=s+r,n=n/106.print s7. stop
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
14/132
PageNo..
Design@Crazy
Title : Sum of digits of a number
Aim : Program to find sum of digits of a number
Program
#include
#include
void main()
{
int n,s=0,d;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
d=n%10;
s=s+d;
n=n/10;}
printf("Sum=%d",s);
getch();
}
Result:The program is tested and verified
Output
Enter a number: 562
Sum=13
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
15/132
PageNo..
Design@Crazy
Algorithm
1:start
2:initialize r=0
3:read n
4:if n>0 go to step 5 otherwise go to step 8
5.d=n%10
6.r=(r*10)+d
7.n=n/10 Go to step 4
8:display r
9:stop
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
16/132
PageNo..
Design@Crazy
Title : Reverse of a number
Aim : Program to findreverse of a number
Program
#include
#include
void main()
{
int n,r=0,d;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
d=n%10;
r=(r*10)+d;
n=n/10;
}printf("Reverse=%d",r);
getch();
}
Result: The program is tested and verified
Output
Enter the number:
254
Reverse=452
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
17/132
PageNo..
Design@Crazy
Algorithm
1:start
2:initialize s=0,f0=0,f1=1
3:read n
4:display f0 and f1
5:initialize i=0
6:if i
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
18/132
PageNo..
Design@Crazy
Title : Fibonacci series
Aim : Program to generate Fibonacci series
Program
#include
#include
void main()
{
int f0,f1,s=0,n,i;
clrscr();
f0=0;
f1=1;
printf("Enter the limit:");
scanf("%d",&n);
printf("%d\n%d",f0,f1);
for(i=0;i
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
19/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
20/132
PageNo..
Design@Crazy
Output
Enter the limit:7
0
1
1
2
3
5
8
13
21
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
21/132
PageNo..
Design@Crazy
Algorithm
1:start
2:declare variables n,i,z
3:read n
4:initialize i=2
5:if i
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
22/132
PageNo..
Design@Crazy
Title : Prime or not
Aim : Program to input a number and check it is prime or not.
Program
#include
#include
void main()
{
int n,i,z;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
if(n==1)
exit(0);
i=2;
while(i
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
23/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
24/132
PageNo..
Design@Crazy
Result:
The program is tested and verified
Output
Enter the number:5
The number is......a prime number
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
25/132
PageNo..
Design@Crazy
Algorithm
1.Class defenition
1.1: declare f,i,n
1.2: public member function void getdata()
1.3: public member function void show()
1.4: end of class definition
2: void getdata() function
2.1:accept n
2.2: initialize f=1 and calculate f=f*i
3: void show() function
3.1: display f
4: main() function
4.1: create the object of class
4.2:call void getdata() function
4.3: call void show() function
4.4: end
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
26/132
PageNo..
Design@Crazy
Title : Factorial of a number
Aim : Program to find factorial of a number using class concept.
Program
#include
#include
class fact
{
public:
int f,i,n;
void getdata();
void show();
};
void fact::getdata()
{
coutn;
f=1;
for(i=1;i
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
27/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
28/132
PageNo..
Design@Crazy
Result: The program is tested and verified
Output
Enter the number:4
Factorial=24
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
29/132
PageNo..
Design@Crazy
Algorithm
1: class definition
1.1: declare n,p,I,j,f,c;
1.2: public member function void generate()
1.3: end of class definition
2: void getdata() function
2.1: initialize c=0
2.2: accept n and initialize i=2
2.3: : if i
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
30/132
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
31/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
32/132
PageNo..
Design@Crazy
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
33/132
PageNo..
Design@Crazy
Algorithm
1:class definition
1.1: declare variables dpt, wdrw, bal, temp,name[20],type[10]
1.2: public member function void initial()
1.3: public member function void deposit()
1.4: public member function void withdraw()
1.5: public member function void display()
1.6: end of class definition
2: void initial() function
2.1: accept name
2.2: accept account type
2.3: set balance=fxd
3: void deposit () function
3.1: accept deposit amount
3.2: set bal=fxd+dpt
3.3: display current balance
4: void withdraw() function
4.1: set temp=04.2: accept withdraw amount
4.3: if ((wdrw500)) go to step 4.4
Otherwise go to step 4.5
4.4: set temp=bal-wdrw;
set bal=temp;
4.5: display current balance
5: void display() function
5.1: display name and balance
6: main() function
6.1: if choice=1,accept account number and call initial() function
6.2: if choice=1,accept account number and call deposit() function
6.3: if choice=1,accept account number and call withdraw() function
6.4: if choice=1,accept account number and call display() function
6.5: if choice=5, end
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
34/132
PageNo..
Design@Crazy
Title : Bank transactions
Aim : Write a program for bank transactions.
Program
//Banking
#include
#include
int fxd=500;
class bank
{
private:
char name[20],type[10];
int dpt,wdrw,bal,temp;
public:
void initial();
void deposit();
void withdraw();
void display();
}m[10];
void bank::initial()
{
coutname;
couttype;
bal=fxd;
}
void bank::deposit()
{
coutdpt;
bal=fxd+dpt;
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
35/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
36/132
PageNo..
Design@Crazy
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
37/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
38/132
PageNo..
Design@Crazy
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
39/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
40/132
PageNo..
Design@Crazy
}
}while(n
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
41/132
PageNo..
Design@Crazy
Algorithm
1: class definition
1.1: private data members name,grade,reg no
m1,m2 ,m3,m4,m5,tot,per
1.2: public member function void getdata()
1.3: public member function void calc()
1.4: public member function void display()
2: void getdata() function
2.1:accept name
2.2: accept regno
2.3: accept m1,m2,m3,m4,m5
3: void calc() function
3.1: calculate total
3.2: calculate percentage
3.3: if percentage>=80 ,otherwise go to step 3.4
Set grade=A
3.4:if per>=60 and per=50 and per=40 and per
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
42/132
PageNo..
Design@Crazy
Title : Mark list
Aim : Write a program to display mark list of students
Program
#include
#include
class students
{
private:
char name[20],grade;
int regno,m1,m2,m3,m4,m5,tot;
float per;
public:
void getdata();
void calc();
void display();
}s[40];
void students::getdata()
{coutname;
coutregno;
coutm1>>m2>>m3>>m4>>m5;
}
void students::calc()
{
tot=m1+m2+m3+m4+m5;
per=tot/5;
if(per>=80)
{
grade='A';
}
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
43/132
PageNo..
Design@Crazy
5.6: if i
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
44/132
PageNo..
Design@Crazy
else if((per>=60)&&(per=50)&&(per=40)&&(per
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
45/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
46/132
PageNo..
Design@Crazy
cin>>n;
for (i=0;i
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
47/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
48/132
PageNo..
Design@Crazy
Enter the register number :17379
Enter the mark of 5 subjects :80
52
63
54
90
..........STUDENTS DETAILS...........
REG.NO: NAME SUB1 SUB2 SUB3 SUB4 SUB5 TOTAL PER:(%)
GRADE
............................................................................................................................
17524 ANJU 98 75 88 92 88 441 88 A
17379 ANU 80 52 63 54 90 339 67 B
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
49/132
PageNo..
Design@Crazy
Algorithm
1:class definition
1.1: private data member id
1.2: constructor member functions declaration and definition
1.3: copy constructor member function definition
1.4: public member function void display(void)
1.5: end of class definition
2:main function()
2.1:object A is created and initialized
2.2: copy constructor called
2.3: copy constructor called again
2.4: D is created ,not initialized
2.5: D=A
2.6: call the functions
A.display()
B.display()
C.display()
D.display()
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
50/132
PageNo..
Design@Crazy
Title : Copy constructor
Aim : Write a program to implement copy constructor
Program
#include
#include
class code
{
int id;
public:
code(){ } //constructor
code(int a) { id=a;} //constructor again
code(code &x) { id=x.id; } //copy constructor
void display(void)
{ cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
51/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
52/132
PageNo..
Design@Crazy
Result:
The program is tested and verified
Output
id of A:100
id of B:100
id of C:100
id of D:100
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
53/132
PageNo..
Design@Crazy
Algorithm
1:start
main() function
2: pass volume (10) to int volume(int s) function and display volume
3: pass volume (2.5,8) to double volume(double r,int h) function and display
volume
4: pass volume(100l,75,15) to long volume(long l,int b,int h) function and
display volume
5: int volume(int s) function
return(s*s*s)
6:double volume(double r,int h)
return(3.14519*r*r*h)
7: long volume(long l,int b,int h)
return(l*b*h)
8: stop
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
54/132
PageNo..
Design@Crazy
Title : Function overloading
Aim : Write a program to implement function overloading.
Program
#include
#include
class area
{
public:
int l,b,a,r,m,n;
float pi,result;
void findarea(float,int);
void findarea(int,int);
void findarea(int);
};
void area::findarea(float p,int x)
{
x=p;
r=x;a=x*r*r;
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
55/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
56/132
PageNo..
Design@Crazy
}
void main()
{
area g;
clrscr();
g.findarea(3.14,5);
g.findarea(5,4);
g.findarea(6);
getch();
}
Result:
The program is tested and verified
Output
Area of cube:1000
Area of cylinder:157.2595Area of rectangular box:112500
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
57/132
PageNo..
Design@Crazy
Algorithm
1: class definition
1.1: private data member r
1.2: public member function void getdata( int a)
1.3: public member function float area()
1.4: public member function float vol()
1.5: public member function void disp()
1.6: end of class definition
2: definition of getdata( int a) function as inline
3: definition of area() function as inline
4: definition of vol() function as inline
5: definition of disp()function as inline
6: main() function
6.1: create object of the class
6.2: call the function getdata(a)
6.3: call the function disp()
6.4: end
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
58/132
PageNo..
Design@Crazy
Title : Inline function
Aim : Write a program to implement inline functions.
Program
#include
#include
class circle
{
int r;
public:
void getdata( int a);
float area();
float vol();
void disp();
};
inline void circle::getdata(int a)
{
r=a;
}inline float circle::area()
{
return 3.14*r*r;
}
inline float circle ::vol()
{
return area()*r;
}
inline void circle::disp()
{
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
59/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
60/132
PageNo..
Design@Crazy
void main()
{
clrscr();
int a;
circle d;
couta;
d.getdata(a);
d.disp();
getch();
}
Result: The program is tested and verified
Output
The radius of circle is.........
5
The area is............
78.5
The volume is............
392.5
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
61/132
PageNo..
Design@Crazy
Algorithm for class
1.Start
2.declare the class sum
3.Defining constructor sum() as s=0
4.Defining the function findsum()
5.display value of a and b
6.Display the sum c
7.Stop
Algorithm for main
1.Start
2.Create the object of class sum
3.Call the function findsum() with object a
4.Stop
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
62/132
PageNo..
Design@Crazy
Title :Default constructor
Aim : Write program using default constructor
Program
//Program of default constructor
#include
#include
class sum
{
public:
int a,b,s;
sum()
{
s=0;
}
void findsum()
{couta>>b;
s=a+b;
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
63/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
64/132
PageNo..
Design@Crazy
Result:
The program is tested and verified
OUTPUT
Enter two number
2
3
Sum=5
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
65/132
PageNo..
Design@Crazy
Algorithm for class
1.Start
2.declare the class factorial with member n,I,f
and function findfact()
3.Defining constructor with 1 argument and assign it
to x
4.Defing function findfact() and fin the factorial
with for loop
5.Stop
Algorithm for main
1.Start
2.Calling constructor with 1 argument
3.display the value
4.Stop
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
66/132
PageNo..
Design@Crazy
Title :Parameterized constructor
Aim : Write program using paramerterized constructor
Program
#include
#include
class factorial
{
public:
int n,i,f;
factorial(int);
void findfact();
};
factorial::factorial(int x)
{
n=x;
}
void factorial::findfact()
{f=1;
for(i=n;i>0;i--)
{
f=f*i;
}
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
67/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
68/132
PageNo..
Design@Crazy
Result:
The program is tested and verified
OUTPUT
Factorial=120
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
69/132
PageNo..
Design@Crazy
Algorithm for class
1.Start
2.Create class staff with data members as
no,bp,ta,da,hra,pf,name,designand function calculation()
3.Create class employe as derived class of staff with function
getdata()for reading information and display()for printing data
4.stop
Algorithm for main function
1.Start
2.Create object of class employe
3.Call function getdata()with object e
4.Call function display() with object e
5.stop
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
70/132
PageNo..
Design@Crazy
Title : Single inheritance
Aim : Write program using single inheritance
Program
/*Single inheritance*/
#include
#include
class staff
{
public:
int bp,ta,da,hra,pf,no;
char name[20],desig[20];
void getdata()
{
coutno;
coutname;coutdesig;
coutbp;
coutta;
coutda;
couthra;
coutpf;
}
};
class employe:public staff
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
71/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
72/132
PageNo..
Design@Crazy
{
public:
int total,net;
void calculation()
{
total=bp+ta+da+hra;
net=total-pf;
}
void dispaly()
{
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
73/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
74/132
PageNo..
Design@Crazy
OUTPUT
Enter the roll number of employe:1
Enter the name of the employe:Suresh
Enter the designation of the employe:Kollam
Enter basic pay:5000
Enter the ta :0
Enter the da:510
Enter the hra:310
Enter the pf:1500
no name desi bp ta da hra pf total net
------------------------------------------------------------
1 Suresh Kola 5000 0 510 310 1500
total net
------------
5820 4320
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
75/132
PageNo..
Design@Crazy
Algorithm for class
1.Start
2.create class first with data member id,name and
function getstudent()
3.Create class second with data member s,cname and
function getcourse()
4.Create class third with data member tm s1,s2,s3
and function getresult()and getdetails()
5.Stop
Algorithm for main function
1.Start
2.create object r for class third
3.Call function getstudent()with object r
4.Call function getcourse()with object r5.call function getresult and getdetails with object
r
6.Stop
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
76/132
PageNo..
Design@Crazy
Title :Multilevel inheritance
Aim : Write program using multilevel inheritance
Program
#include
#include
class student
{
public:
int id,total;
char name[10];
void getstudent()
{
coutid;
coutname;}
};
class course:public student
{
public:
int s;
char cname[20];
void getcourse()
{
couts;
coutcname;
}
};
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
77/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
78/132
PageNo..
Design@Crazy
class result:public course
{
public:
int tm,s1,s2,s3;
void getresult()
{
couts1;
couts2;
couts3;
tm=s1+s2+s3;
}
void getdetails()
{
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
79/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
80/132
PageNo..
Design@Crazy
Result:
The program is tested and verified
Output
Enter the student id:1
Enter the name of the student:Asha
Enter the semester:1
Enter the name of the course:Bsccomputerscience
Enter the mark of s1:85
Enter the mark of s2:95
Enter the mark of s3:99
STUDENT DETAILS
----------------------
ID name semester course name total------------------------------------------------------
1 Asha 1 Bsc 279
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
81/132
PageNo..
Design@Crazy
Algorithm for class
1.start
2.Create class A with data member a and function
geta()
3.Create class B with data member b and function
getb()
4.Create class C as derived class of A and B with
member c and function getresult()
5.Stop
Algorithm for main
1.Start
2.Create object of C class
3.Call function geta() and getb() with object d
4.Stop
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
82/132
PageNo..
Design@Crazy
Title :Multiple inheritance
Aim : Write program using multiple inheritance
Program
#include
#include
class A
{
public:
int a;
void geta()
{
couta;
}
};
class B
{public:
int b;
void getb()
{
coutb;
}
};
class C:public A,public B
{
public:
int c;
void getresult()
{
geta();
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
83/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
84/132
PageNo..
Design@Crazy
getb();
c=a+b;
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
85/132
PageNo..
Design@Crazy
Algorithm for class
1.Start
2.create class A with member a and function
geta()
3.Create class B as derived class of A with
member b and function getb()
4.Create class C as derived class of A with
member c and function getc()
5.Stop
Algorithm for main
1.Start
2.Create object for class A
3.Create object for class B
4.Call function geta(),getb()and getsum() with object
p5.Call function geta(), getb() and getmul() with
object q
6.Stop
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
86/132
PageNo..
Design@Crazy
Title :Heirarchial inheritance
Aim : Write program using Herirarchial inheritance
Program
#include
#include
class A
{
public:
int a;
void geta()
{
couta;
}
};
class B:public A
{
public:int b,c;
void getb()
{
coutb;
}
void getsum()
{
c=a+b;
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
87/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
88/132
PageNo..
Design@Crazy
int d,b,c;
void getd()
{
coutd;
}
void getmul()
{
c=a*d;
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
89/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
90/132
PageNo..
Design@Crazy
Output
Enter the value of a:2
Enter the value of b:5
Sum=7Enter the value of a:6
Enter the value of d:7
Product=42
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
91/132
PageNo..
Design@Crazy
Algorithm for class
1.Start
2.Create class player with member name,gender,age
3.Create class test as derived class of player
with member height,weight
4.Create class sports with member sname and function
getitem()
5.Create class result as derived class of test and
player with function display()
6.Stop
Algorithm for main
1.Start
2.Create object of class result
3.Call function getitem() and getitem()and
display() with object r4.stop
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
92/132
PageNo..
Design@Crazy
Title :hybrid inheritance
Aim : Write program using hybrid inheritance
Program
#include
#include
class student
{
public:
int no;
char name[10],desig[10];
void getstudent()
{
coutno;
coutname;
coutdesig;
}
};
class test:public student
{
public:
int m1,m2,m3,total;
void gettest()
{
coutm1;
coutm2;
coutm3;
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
93/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
94/132
PageNo..
Design@Crazy
total=m1+m2+m3;
}
};
class sports
{
public:
char sname[20];
void getitem()
{
coutsname;
}
};
class result:public test,public sports
{
public:
void display()
{cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
95/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
96/132
PageNo..
Design@Crazy
clrscr();
r.getstudent();
r.gettest();
r.getitem();
r.display();
getch();
}
Result:
The program is tested and verified
Output
Enter the roll number of the student:1
Enter the name of the student:Manu
Enter the designation of the student:AyoorEnter the mark of english:85
Enter the mark of mathematics:60
Enter the mark of science:73
Enter your favouraite sports item:Cricket
student details
-----------------------
Number:1
Name:Manu
Designation:Ayoor
Mark of English:85
Mark of Mathematics:60
Mark of science:73
total=218
favouraite sports item:Cricket
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
97/132
PageNo..
Design@Crazy
Algorithm for class
1.start
2.Create class demo with member x,y,z
3.Define function getdata()with 3 argument
4.define function operator() and display()
5.stop
Algorithm for main
1.Start
2.Create object for class demo
3.Call Function with object d
4.Stop
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
98/132
PageNo..
Design@Crazy
Title :Operator overloading
Aim : Write program using operator overloading
Program
#include
#include
class demo
{
public:
int x,y,z;
void getdata(int a,int b,int c);
void operator -();
void display();
};
void demo::getdata(int a,int b,int c)
{
x=a;
y=b;z=c;
}
void demo::operator -()
{
x=-x;
y=-y;
z=-z;
}
void demo::display()
{
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
99/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
100/132
PageNo..
Design@Crazy
{
demo d;
clrscr();
d.getdata(10,-20,30);
-d;
d.display();
getch();
}
Result:
The program is tested and verified
Output
x=-10
y=20z=-30
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
101/132
PageNo..
Design@Crazy
Algorithm for Class
1.Start
2.Create class multiply with data members a,b and
function getvalue()
3.Declare a friend function product
4.define friend function and print the product
5.Stop
Algorithm for main
1.Start
2.Create object d
3.Call function getvalue() with object d
4.Call friend function product
5.Stop
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
102/132
PageNo..
Design@Crazy
Title :Friend function
Aim : Write program using friend function
Program
#include
#include
class multiply
{
int a,b;
public:
void getvalues()
{
couta>>b;
}
friend int product(multiply m)
};
int product(multiply m){
int c;
c=m.a*m.b;
return(c);
}
void main()
{
multiply d;
clrscr();
d.getvalues();
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
103/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
104/132
PageNo..
Design@Crazy
Result:
The program is tested and verified
Output
Enter two number:5 2
Product of two numbers=10
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
105/132
PageNo..
Design@Crazy
Algorithm for class
1.Start
2.Create class first with member b
3.Define the function first()as b=10
4.Define the class display as virtual and print b
5.Create class second as derived class of first
with data member d and function second()
5.Define the function display()print the value of
d
6.Stop
Algorithm for main
1.Start
2.Create object s of second
3.create object *p of first
4.call function display()with p5.Stop
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
106/132
PageNo..
Design@Crazy
Title :Pure virtual function
Aim : Write program using pure virtual function
Program
#include
#include
class first
{
public:
int b;
first()
{
b=10;
}
virtual void dispaly()
{
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
107/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
108/132
PageNo..
Design@Crazy
{
first *p,f;
p=&f;
p->display();
second s;
p=&s;
s->display();
getch();
}
Result:
The program is tested and verified
Output
Value of b=10
Value of d=20
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
109/132
PageNo..
Design@Crazy
Algorithm for template function
1.Start
2.Define function template sum to add two elements
3.Stop
Algorithm for main function
1.Start
2.set x,y and hx,fy
3.z=call function sum with argument as x and y
4.fz=call function sum with argument as fx and fy
5.print z and fz
6.Stop
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
110/132
PageNo..
Design@Crazy
Title :Function template
Aim : Write program using function template
Program
#include
#include
template
T addition(T a, T b)
{
T s;
s=a+b;
return(s);
}
void main()
{
int x,y,z;
float fx,fy,fz;
clrscr();
coutx>>y;
z=addition(x,y);
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
111/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
112/132
PageNo..
Design@Crazy
Output
Enter two integer number:2 3
Sum=5
Enter two real number:3.5 2.5
Sum=6
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
113/132
PageNo..
Design@Crazy
Algorithm for class
1.Start
2.Create class template with data members pointer variable v
3.Create constructor function with no arguments
4.Create constructor function with *a as argument
5.Oerload operator*
6.Stop
Algorithm for main function
1.Start
2.Create two arrays x and y with three element
3.Create template variables
4.v1=x and v2=y
5.Do operation between v1 and v2 with overload operator * and
assign the result to r
6.Print r7.Stop
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
114/132
PageNo..
Design@Crazy
Title : Class template
Aim : Write program using class template
Program
#include
#include
const size=3;
template
class vector
{
T *v;
public:
vector()
{
v=new T[size];
for(int i=0;i
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
115/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
116/132
PageNo..
Design@Crazy
return(sum);
}
};
int main()
{
int x[3]={1,2,3};
int y[3]={1,2,3};
vectorv1;
vectorv2;
v1=x;
v2=y;
int r=v1*v2;
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
117/132
PageNo..
Design@Crazy
Algorithm for function
1.Start
2.Define function test()
3.Try block
if x==1
Throw x
if x=0
Throw x
if x=-1
Throw 1.0
4.Catch block
if x is integer
printCaught integer
If x is character
print Caught character
If x is doubleprint Caught double
5.Stop
Algorithm for main function
1.Start
2.call function test()with argument as 1
3.Call function test ()with argument as 0
4.Call function test()with argument as -1
5.Call function test()with argument as 2
6.Stop
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
118/132
PageNo..
Design@Crazy
Title: Exception handling
Aim : Write program using Exception hanling
Program
#include
#include
void test(int x)
{
try
{
if(x==1)
throw x;
else if(x==0)
throw'x';
else if(x==1)
throw 1.0;
}
catch(char c)
{cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
119/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
120/132
PageNo..
Design@Crazy
test(10;
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
121/132
PageNo..
Design@Crazy
ALGORITHM FOR CLASS:
1.Start
2.Create class tele withdata members fnm[23],dno[20],stm[10],city[20]and
pno and with member function get()for reading and showwall() for printing
data
3.Stop
Algorithm for main function:
1.Start
2.Create object t for class tele
3.Create object f for fstream
4.Assign character ch==y
5.open file address.dat using object f in input mode
6.If ch==y
Call function get()using t
Write fetched data to file
Read value of chRepeat step 6
7.Seek file pointer to begning of file
8.call function showwall()using t
9.Close file address.dat
10.Stop
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
122/132
PageNo..
Design@Crazy
Title: File
Aim :Write program using file
Program
/*Write and read operation using file*/
#include
#include
#include
#include
#include
class tele
{
char fnm[20];
char dno[10],stnm[20];
char road[20],city[10];
long pno;
public:
void get();
void showwall();};
void tele::get()
{
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
123/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
124/132
PageNo..
Design@Crazy
cin>>pno;
}
void tele::showwall()
{
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
125/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
126/132
PageNo..
Design@Crazy
Result:
The program is tested and varified
Output
Enter full name:Asha suresh
Enter door No:20
Enter street name:SPG complex
Enter road name:Dwarka
Enter city:Delhi
Enter phone No:065897231
Do you want to continue?(y/n)n
Information
Full Name:Asha suresh
Dorr No:20
Street Name:SPG complex
Road Name:Dwarka
City:Delhi
Phone No:53
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
127/132
PageNo..
Design@Crazy
Algorithm for class
1.Start
2.Create class book with member bnm[20],author[30],peges,price and
member function as get()for reading data and list() for printing data
3.Stop
Algorithm for main
1.Start
2.Create array of object b[3] of books
3.Create object k of book
4.Create object f1 for fstream
5.Open file book.data in input mode
6.for i=0 to 2
7.Call function get()with b[i]
8.Writw data read by b[i]to file
9.End for i10.Seek file pointer f1 to beginning of file
11.for i=0 to 2
12.Read data from file
13.Call function list()with b[i]
14.End for i
15.Stop
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
128/132
PageNo..
Design@Crazy
Title: File Accessing
Aim :Write program using file
Program
#include
#include
#include
#include
class book
{
char bnm[20],author[30];
int pages,price;
public:
void get();
void list();
};
void book::get()
{
cout
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
129/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
130/132
PageNo..
Design@Crazy
void main()
{
int i;
book b[3],k;
fstream f1;
clrscr();
f1.open("book.dat.",ios::in);
for(i=0;i
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
131/132
PageNo..
Design@Crazy
7/31/2019 B.sc Computer Science 1st Sem c Lab Record
132/132
PageNo..
Output
Enter book name:Harrypotter
Enter author name:JKRowling
Enter pages:1000
Enter book name:Computerorganization
Enter author name:Williamstalling
Enter pages:826
Enter book name:Earthmagnetism
Enter author name:WallaceHall
Enter pages:100