Upload
kishorjacob
View
72
Download
4
Embed Size (px)
DESCRIPTION
Mahathmagandhi University MSc Computer Science 1st Semester Lab record in Object oriented Programming with C++
Citation preview
2014
MSc Computer Science LAB Record
OBJECT ORIENTED PROGRAMMING WITH C++
COLLEGE OF APPLIED SCIENCE, KATTAPPANA
(Managed by IHRD, Affiliated to MG University)
Name : MSc Computer Science, 1
st Semester (M1)
Class No : Reg.No:..
Object Oriented Programming with C++
Page 1
External Examiner
Certificate
This is the bonafied record of practical work done in the computer laboratory of
College of applied science Kattappana, in the First semester of MSc Computer
Science during the year 2013-4 by . ith Reg.No:.
Lecturer in Charge
Submitted for the first semester practical examination held at college of applied
science Kattappana on ....
Internal Examiner
COLLEGE OF APPLIED SCIENCE, KATTAPPANA
(Managed by IHRD, Affiliated to MG University)
Object Oriented Programming with C++
Page 2
Index
# Title Date Page No
1 Program to print address 3
2 Program to print Armstrong numbers below a given number 5
3 Program to check a given number is palindrome or not 8
4 Program to print prime numbers between 1 and 100 11
5 Program to sort names 14
6 Program to print Fibonacci series using recursion 17
7 Program to count words, lines & characters in a paragraph 20
8 Sort integer, float & character arrays with function overloading 23
9 Search and delete all occurrences of a number in an array 28
10 Program to find volume and area of a cylinder using class 32
11 Write a menu-driven program to perform matrix addition &
multiplication 35
12 Write a menu driven program to display student details using
array of objects 41
13 Program to add two times using object as arguments 45
14 Program to overload + and operator in distance class 48 15 Perform complex number operations using operator
overloading with friend function 52
16 Perform matrix operations using operator overloading 56
17 Program to manipulate employee details using multiple
inheritance 60
18 Program to implement abstract class 64
19 Program to display account details using pointers to array of
objects 69
20 Program for multilevel inheritance 74
21 Program to sort names using command line arguments 78
22 Program to sort numbers using command line arguments 80
23 Program to print prime and non-prime numbers using file 83
24 Program to print odd and even numbers with file 86
25 Program using function template for sorting 89
26 Program of stack using class template 93
Object Oriented Programming with C++
Page 3
Program No: 01
// Program to print the address
#include
#include
void main()
{
clrscr();
char name[20], place[20],dist[20],state[20];
long int pin;
coutname;
coutplace;
coutdist;
coutstate;
coutpin;
cout
Object Oriented Programming with C++
Page 4
OUTPUT
Enter name:CAS
Enter place:Kattappana
Enter district:Idukki
Enter state:Kerala
Enter PIN code:685508
ADDRESS-----------------------
CAS
Kattappana
Idukki
Kerala
PIN : 685602
Object Oriented Programming with C++
Page 5
Program No: 02
// Program to print armstrong numbers below a given number
#include
#include
class armstrong
{
int i,n,a,s;
public:
void get(int );
};
void armstrong::get(int m)
{
for(i=1;i
Object Oriented Programming with C++
Page 6
}
void main()
{
clrscr();
int h;
couth;
cout
Object Oriented Programming with C++
Page 7
OUTPUT
Enter the limit:
1000
Armstrong numbers below 1000 are:
1
153
370
371
407
Object Oriented Programming with C++
Page 8
Program No: 03
// Program to check whether a given number is palindrome or not.
#include
#include
class palindrome
{
int a,d,s;
public:
void check(int);
}ob;
void palindrome::check(int c)
{
int m=c;
s=0;
while(c!=0)
{
a=c%10;
s=(s*10)+a;
c=c/10;
}
if(m==s)
cout
Object Oriented Programming with C++
Page 9
clrscr();
int b;
coutb;
ob.check(b);
getch();
}
Object Oriented Programming with C++
Page 10
OUTPUT
Enter a number:31513
31513 is palindrome
Object Oriented Programming with C++
Page 11
Program No: 04
// Program to print prime numbers between 1 & 100.
#include
#include
class prime
{
int i,f;
public:
void check(int);
}ob;
void prime::check(int a)
{
f=0;
for(i=1;i
Object Oriented Programming with C++
Page 12
for(int n=1;n
Object Oriented Programming with C++
Page 13
OUTPUT
PRIME NUMBERS BETWEEN 1 AND 100
2 3 5 7 11
13 17 19 23 29
31 37 41 43 47
53 59 61 67 71
73 79 83 89 97
Object Oriented Programming with C++
Page 14
Program No: 05
// Program to sort names.
#include
#include
#include
class sort
{
public:
char a[25][25];
int n,i,j;
void getdata();
void compare();
};
void sort::getdata()
{
coutn;
cout
Object Oriented Programming with C++
Page 15
for(i=1;i
Object Oriented Programming with C++
Page 16
OUTPUT
Enter the number of names:
6
Enter the names:
arjtech
dotkey
adoscube
microsoft
yahoo
Sorted list is:
adoscube
arjtech
dotkey
microsoft
yahoo
Object Oriented Programming with C++
Page 17
Program No: 06
// Program to print Fibonacci series using recursive function.
#include
#include
class fibo
{
public:
int fib(int n)
{
if(n==0)
return 0;
if(n==1)
return 1;
return(fib(n-2)+fib(n-1));
}
}obj;
void main()
{
clrscr();
int o,i;
couto;
for(int j=0;j
Object Oriented Programming with C++
Page 18
}
getch();
}
Object Oriented Programming with C++
Page 19
OUTPUT
Enter limit: 100
0
1
1
2
3
5
8
13
21
34
Object Oriented Programming with C++
Page 20
Program No: 07
// Program to count number of words, lines and characters in a paragraph
#include
#include
#include
class count
{
char s[200];
long int w,ch,l;
public:
void get()
{
cout
Object Oriented Programming with C++
Page 21
l=0;
ch=0;
for(int i=0;s[i]!='/0';i++)
{
ch++;
if(s[i]==' '||s[i]=='\n')
{
w++;
}
if(s[i+1]=='\0')
{
l++;
}
}
}
void main()
{
clrscr();
count c;
c.get();
c.put();
c.display();
getch();
}
Object Oriented Programming with C++
Page 22
OUTPUT
Enter paragraph:
If opportuity doest knock, build a door. If you dot build your drea, soeoe ill hire you to help build theirs. Words:21
Lines:2
Characters:117
Object Oriented Programming with C++
Page 23
Program No: 08
// Sort integer array, float array, character array using function overloading?
#include
#include
#include
class sort1
{
public:
int m;
void nos(int n);
void nos();
void nos(char s);
};
void sort1::nos(int n)
{
int a[10],t,i,j;
cout
Object Oriented Programming with C++
Page 24
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
cout
Object Oriented Programming with C++
Page 25
for(j=i+1;ja[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
cout
Object Oriented Programming with C++
Page 26
{
for(j=i+1;j
Object Oriented Programming with C++
Page 27
OUTPUT
Enter numbers:1
2
3
Integer numbers in ascending order:
1
2
3
Enter the limit:2
Enter the numbers:
2
3
the float numbers in descending order:
3
2
enter the limit:2
enter the characters:
r
e
the character in sorted order:
e
r
Object Oriented Programming with C++
Page 28
Program No: 09
/* Program to search and delete all occurrences of a given number in an
array */
#include
#include
class del
{
int n,a[10],b[10],i;
public:
void get();
void search();
};
void del::get()
{
coutn;
cout
Object Oriented Programming with C++
Page 29
int s;
couts;
int j=0;
int f=0;
for(i=0;i
Object Oriented Programming with C++
Page 30
cout
Object Oriented Programming with C++
Page 31
OUTPUT
Enter limit:5
Enter array:
6
5
8
6
9
Enter element to search:6
The new array is:
5
8
9
Object Oriented Programming with C++
Page 32
Program No: 10
// Program to find volume and area of a cylinder using class?
#include
#include
class cylender
{
float r,h;
public:
void get();
float volume();
float area();
}ob;
void cylender::get()
{
coutr>>h;
}
float cylender::volume()
{
float v;
v=(3.14*r*r)+h;
return v;
}
float cylender::area()
{
float a;
a=(2*3.14*r*r)+(h*(2*3.14*r));
Date:
Object Oriented Programming with C++
Page 33
return a;
}
void main()
{
clrscr();
float vo,area1;
ob.get();
vo=ob.volume();
cout
Object Oriented Programming with C++
Page 34
OUTPUT
Enter Radius and height of the cylinder :6
10
Volume= 123.040001
Area= 602.880005
Object Oriented Programming with C++
Page 35
Program No: 11
// Write a menu driven program to perform matrix addition and multiplication?
#include
#include
class matrix
{
int m,n,t,u,i,j,k;
int a[5][5],b[5][5],c[5][5];
public:
void getdata(int p,int q,int r,int s);
void sum();
void pro();
void display();
};
void matrix::getdata(int p, int q,int r,int s)
{
m=p;
n=q;
t=r;
u=s;
cout
Object Oriented Programming with C++
Page 36
}
cout
Object Oriented Programming with C++
Page 37
{
for(k=0;k
Object Oriented Programming with C++
Page 38
cout
Object Oriented Programming with C++
Page 39
cout
Object Oriented Programming with C++
Page 40
OUTPUT
----------------MENU---------------------
1.Matrix Addition
2.Matrix Multiplication
3.Exit
Enter Your Choice: 1
Enter the row and column of matrix 1:2
2
Enter the row and column of matrix 2:2
2
Enter the elements of first matrix:
2
2
2
2
Enter the elements of second matrix:
2
2
2
2
The Sum of the Given Matrices are:
4 4
4 4
Object Oriented Programming with C++
Page 41
Program No: 12
// Write a menu driven program to display student details using array of objects?
#include
#include
class stud
{
protected:
int no,m1,m2;
char name[25];
public:
void getdata();
};
void stud::getdata()
{
cout
Object Oriented Programming with C++
Page 42
int to;
public:
void getdata1()
{
to=m1+m2;
}
};
class result:public test
{
public:
void putdata()
{
cout
Object Oriented Programming with C++
Page 43
for(int j=0;j
Object Oriented Programming with C++
Page 44
OUTPUT
Enter total number of students:2
Enter roll number:1
Enter name: Aswin
Enter first mark :36
Enter second mark:39
Student details
--------------------------------------------
Rollnumber :1
Name:Aswin
Total mark:75
Enter roll number:2
Enter name: Binu
Enter first mark:35
Enter second mark:42
Student details
-----------------------------------------------
Rollnumber :2
Name:Binu
Total mark:77
Object Oriented Programming with C++
Page 45
Program No: 13
// Program to add two times using objects as arguments?
#include
#include
class time
{
public:
int h,m,s;
void get()
{
couth>>m>>s;
}
void showTime()
{
cout
Object Oriented Programming with C++
Page 46
}
if(t.m>=60)
{
t.h=t.h+t.m/60;
t.m=t.m%60;
}
if(t.h>12)
{
t.h=t.h-12;
}
return(t);
}
};
void main()
{
clrscr();
time c1,c2,c3;
c1.get();
c2.get();
c3=c1+c2;
c3. showTime ();
getch();
}
Object Oriented Programming with C++
Page 47
OUTPUT
Enter Time (hh:mm:ss) : 1
59
33
Enter Time (hh:mm:ss) : 1
2
31
Sum is: 3:3:4
Object Oriented Programming with C++
Page 48
Program No: 14
// Program to overload + & - operator in distance class?
#include
#include
class distance
{
int x,y,z,k,l,m;
public:
void getdata(int,int,int);
void getdata1(int,int,int);
void display();
void display1();
void operator-();
void operator+();
};
void distance::getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
void distance::getdata1(int p,int q,int r)
{
k=p;
l=q;
m=r;
}
Date:
Object Oriented Programming with C++
Page 49
void distance::display()
{
cout
Object Oriented Programming with C++
Page 50
ob.getdata1(-40,-50,-80);
+ob;
ob.display1();
getch();
}
Object Oriented Programming with C++
Page 51
OUTPUT
-1020-30-40-50-80
Object Oriented Programming with C++
Page 52
Program No: 15
/* Perform complex no: operations using operator overloading with friend function.*/
#include
#include
class complex
{
int ima,real;
public:
void get();
void put();
friend complex operator +(complex c,complex d);
friend complex operator -(complex c,complex d);
friend complex operator *(complex c,complex d);
friend complex operator /(complex c,complex d);
};
void complex::get()
{
cin>>real>>ima;
}
void complex::put()
{
cout
Object Oriented Programming with C++
Page 53
complex operator +(complex c1,complex c2)
{
complex t1;
t1.real=c1.real+c2.real;
t1.ima=c1.ima+c2.ima;
return(t1);
}
complex operator -(complex c1,complex c2)
{
complex t2;
t2.real=c1.real-c2.real;
t2.ima=c1.ima-c2.ima;
return(t2);
}
complex operator* (complex c1,complex c2)
{
complex t3;
t3.real=(c1.real*c2.real)-(c1.ima*c2.ima);
t3.ima=(c1.real*c2.ima)+(c2.real+c1.ima);
return(t3);
}
complex operator / (complex c1,complex c2)
{
complex t4;
t4.real=((c1.ima*c2.real)+(c1.ima*c2.ima))/((c2.real*c2.ima)-(c2.ima*c2.ima));
t4.ima=((c1.ima*c2.real)-(c1.real*c2.ima))/((c2.real*c2.real)+(c2.ima+c2.ima));
Object Oriented Programming with C++
Page 54
return(t4);
}
void main()
{
complex c1,c2,c3,c4,c5,c6;
clrscr();
cout
Object Oriented Programming with C++
Page 55
OUTPUT
Enter the imaginary part and real part:1
2
Enter the second imaginary part and real part:1
2
Sum is:2+4i
Difference is:0+0i
product is:-3+5i
division:-3+0i
Object Oriented Programming with C++
Page 56
Program No: 16
// Perform matrix operations using operator overloading?
#include
#include
class matrix
{
int m,n;
int a[10][10],b[10][10],c[10][10];
public:
void getdata(int p,int q);
void sum();
void display();
};
void matrix::getdata(int p, int q)
{
m=p;
n=q;
int i,j;
cout
Object Oriented Programming with C++
Page 57
for(i=0;ib[i][j];
}
}
}
void matrix::sum()
{
int i,j;
c[i][j]=0;
for(i=0;i
Object Oriented Programming with C++
Page 58
{
cout
Object Oriented Programming with C++
Page 59
OUTPUT
Enter the row and column of matrix 1:2
2
Enter the row and column of matrix 2:2
2
Enter the elements of first matrix:
2
2
2
2
Enter the elements of second matrix:
3
3
3
3
SUM IS=
5 5
5 5
Object Oriented Programming with C++
Page 60
Program No: 17
// Program for manipulation of employee details using multiple inheritance?
#include
#include
class empl
{
protected:
int age;
char name[20],place[20];
public:
void getdata()
{
cout
Object Oriented Programming with C++
Page 61
void getsalary()
{
coutdept;
coutsalary;
}
};
class emp:public empl,public salary
{
public:
void display()
{
cout
Object Oriented Programming with C++
Page 62
ob1.getsalary();
ob1.display();
getch();
}
Object Oriented Programming with C++
Page 63
OUTPUT
Enter employee details---------------------------------
Enter the name:Tony
Enter the age:25
Enter the place:kottayam
Enter the department:finance
Enter salary:15000
EMPLOYEE DETAILS
________________
Name:Tony
Age:25
Place:kottayam
Department:finance
Salary:15000
Object Oriented Programming with C++
Page 64
Program No: 18
// Program to implement abstract class?
#include
#include
class shape
{
virtual float area()=0;
virtual float perimeter()=0;
};
class circle:public shape
{
protected:
int r;
float a1,p1;
public:
void read()
{
coutr;
}
float area()
{
a1=3.14*r*r;
return a1;
}
float perimeter()
{
Date:
Object Oriented Programming with C++
Page 65
p1=2*(3.14*r);
return p1;
}
};
class rectangle:public shape
{
protected:
int l,b,p,a;
public:
void get()
{
coutl>>b;
}
float area()
{
a=l*b;
return a;
}
float perimeter()
{
p=2*(l+b);
return p;
}
};
void main()
{
Object Oriented Programming with C++
Page 66
clrscr();
rectangle r;
circle c;
float c1,c2,r1,r2;
cout
Object Oriented Programming with C++
Page 67
r2=r.perimeter();
cout
Object Oriented Programming with C++
Page 68
OUTPUT
1:CIRELE
2:RECTANGLE
3:EXIT
Enter your choice:2
Enter length & breadth:3
5
area of rectangle=15
perimeter of rectangle=16
Enter your choice:3
Object Oriented Programming with C++
Page 69
Program No: 19
// Program to display account details using pointers to array of objects
#include
#include
#include
class bank
{
float acno,depno;
char n[20],ad[20];
public:
void create();
void withdraw();
void deposite();
void display();
};
void bank::create()
{
coutdepno;
}
void bank::withdraw()
Date:
Object Oriented Programming with C++
Page 70
{
int i;
float with;
cout
Object Oriented Programming with C++
Page 71
float dep;
cout
Object Oriented Programming with C++
Page 72
ptr->create();
ptr->withdraw();
ptr->deposite();
ptr->display();
}
getch();
}
Object Oriented Programming with C++
Page 73
OUTPUT
for creating a new account enter the details below....
Enter name: Rajeev
Enter address: Cochin
Enter account number and deposite amount?3012568000369
15000
Do you want to withdraw an amount to?
1.yes
2.no2
Do you want to deposit an amount?
1.yes
2.no
2
balance sheet...
---------------------------
Ac_No Name: address Amount
3.012568e+12 Rajeev Cochin 15000
For creating a new account enter the details below....
Enter name
Object Oriented Programming with C++
Page 74
Program No: 20
// Program for multilevel inheritance
#include
#include
class student
{
protected:
int id;
char name[20];
public:
void getdata()
{
cout
Object Oriented Programming with C++
Page 75
cout
Object Oriented Programming with C++
Page 76
result obj1;
obj1.getdata();
obj1.gettest();
obj1.display();
getch();
}
Object Oriented Programming with C++
Page 77
OUTPUT
Enter the student details
Enter the name:Renjini
Enter id: 12
Enter the first mark:59
Enter the second mark:67
STUDENT DETAILS
________________
Name:Renjini
ID:12
Mark 1:59
Mark 2:67
Total:126
Object Oriented Programming with C++
Page 78
Program No: 21
// Program to sort names using command line arguments?
#include
#include
#include
void main(int argc,char* argv[]) {
clrscr();
char t[20];
for(int i=0;i
Object Oriented Programming with C++
Page 79
OUTPUT
sorted names
adone
brighty
grace
Object Oriented Programming with C++
Page 80
Program No: 22
// Progra to sort os using command line arguments #include
#include
#include
#include
class sort1
{
int i,j;
public:
void names(int a[10],int n);
void disp(int a[10],int n);
};
void sort1::names(int a[10],int n)
{
for(i=1;i
Object Oriented Programming with C++
Page 81
}
void sort1::disp(int a[10],int n)
{
cout
Object Oriented Programming with C++
Page 82
OUTPUT
Array before sort is:
5
3
7
10
The sorted array is:
3
5
7
10
Object Oriented Programming with C++
Page 83
Program No: 23
// Program to print prime & non-prime os usig file #include
#include
void main()
{
clrscr();
ofstream obj("prime.txt",ios::out);
ofstream ob("nonprime.txt",ios::out);
int a,c,k,f;
ob
Object Oriented Programming with C++
Page 84
}
if(f==0)
obj
Object Oriented Programming with C++
Page 85
OUTPUT
Prime numbers are :
2 3 4 5 7 11 13 17 19 23
Non-Prime numbers are :
1 6 8 9 10 12 14 15 16 18 20 21 22 24
Object Oriented Programming with C++
Page 86
Program No: 24
//Progra to prit odd & ee os usig file #include
#include
void main()
{
clrscr();
ofstream obj("even.txt",ios::out);
ofstream ob("odd.txt",ios::out);
int a,c,k,f;
ob
Object Oriented Programming with C++
Page 87
cout
Object Oriented Programming with C++
Page 88
OUTPUT
Even Numbers are :
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56
58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98
Odd Numbers are :
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55
57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
Object Oriented Programming with C++
Page 89
Program No: 25
// Program for function template to sort?
#include
#include
#include
template
void sort(t a[20],int n)
{
int i,j;
t temp;
cout
Object Oriented Programming with C++
Page 90
}
void main()
{
int i,n,b[20],ch;
char c[20];
float d[20];
coutn;
cout
Object Oriented Programming with C++
Page 91
cin>>d[i];
}
sort(d,n);
getch();
}
Object Oriented Programming with C++
Page 92
OUTPUT
Enter the Limit for integer sort
2
Enter INTEGER elements
1
3
Sorted Elements
1
3
Enter the Limit for charector sort
2
Enter ther CHARACTER elements
r
t
Sorted Elements
r
t
Enter the Limit for floating point sort
2
Enter ther FLOATINGPOINT elements
3.3
2.6
Sorted Elements
2.6
3.3
Object Oriented Programming with C++
Page 93
Program No: 26
//Program of stack using class template
#include
#include
class
class stack
{
int top;
t a[10];
public:
void push(int);
void pop();
void display();
void ini();
}obj;
void stack::push(int t)
{
int temp;
temp=t;
if(top>10)
{
cout
Object Oriented Programming with C++
Page 94
}
}
void stack::pop()
{
if(top==0)
{
cout
Object Oriented Programming with C++
Page 95
}
void stack::ini()
{
top=0;
}
int main()
{
clrscr();
obj.ini();
int e,c;
coutc;
switch(c)
{
case 1:
coute;
obj.push(e);
break;
case 2:
obj.pop();
break;
case 3:
obj.display();
break;
case 4:
Object Oriented Programming with C++
Page 96
return 0;
default:
cout
Object Oriented Programming with C++
Page 97
OUTPUT
enter 1,2,3 or 4 for push,pop,display and exit
1
enter element for push to stack
2
1
enter element for push to stack
5
3
stack elements are
2 5
2
3
stack elements are
2
2
2
underflow
3
stack is null