Sie sind auf Seite 1von 98

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<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char name[20], place[20],dist[20],state[20];
long int pin;
cout<<"\nEnter name:";
cin>>name;
cout<<"\nEnter place:";
cin>>place;
cout<<"\nEnter district:";
cin>>dist;
cout<<"\nEnter state:";
cin>>state;
cout<<"\nEnter PIN code:";
cin>>pin;
cout<<"\nADDRESS-----------------------\n";
cout<<name<<"\n";
cout<<place<<\;
cout<< dist<<\;
cout<<state<<\;
cout<<PIN : <<pin;
getch();
}
Date:


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<iostream.h>
#include<conio.h>
class armstrong
{
int i,n,a,s;
public:
void get(int );
};
void armstrong::get(int m)
{
for(i=1;i<m;i++)
{
n=i;
s=0;
while(n!=0)
{
a=n%10;
s=s + (a*a*a);
n=n/10;
}
if(s==i)
{
cout<<i<<"\n";
}
}
Date:


Object Oriented Programming with C++
Page 6
}
void main()
{
clrscr();
int h;
cout<<"\nEnter the limit: \n";
cin>>h;
cout<<"\nArmstrong numbers belo <<h<< are: \n";
armstrong ob;
ob.get(h);
getch();
}
















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<iostream.h>
#include<conio.h>
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<<"\<< << is palindrome";
else
cout<<"\<< << is not palindrome";
}
void main()
{
Date:


Object Oriented Programming with C++
Page 9
clrscr();
int b;
cout<<"\nEnter a number:";
cin>>b;
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<iostream.h>
#include<conio.h>
class prime
{
int i,f;
public:
void check(int);
}ob;
void prime::check(int a)
{
f=0;
for(i=1;i<=a;i++)
{
if(a%i==0)
{
f=f+1;
}
}
if(f==2)
cout<<"\t"<<a<<"\t";
}
void main()
{
clrscr();
cout<<"\nPRIME NUMBERS BETWEEN 1 AND 100\n";
Date:


Object Oriented Programming with C++
Page 12
for(int n=1;n<=100;n++)
{
ob.check(n);
}
getch();
}






















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<iostream.h>
#include<conio.h>
#include<string.h>
class sort
{
public:
char a[25][25];
int n,i,j;
void getdata();
void compare();
};
void sort::getdata()
{
cout<<"Enter the number of names:\n";
cin>>n;
cout<<"Enter the names:\n";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
}
void sort::compare()
{
int j;
char x[10];
Date:


Object Oriented Programming with C++
Page 15
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(a[i],a[j])>0)
{
strcpy(x,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],x);
}
}
}
cout<<"Sorted list is:\n";
for(i=1;i<=n;i++)
{
cout<<"\n"<<a[i];
}
}
void main()
{
clrscr();
sort ob;
ob.getdata();
ob.compare();
getch();
}



Object Oriented Programming with C++
Page 16
OUTPUT
Enter the number of names:
6
Enter the names:
arjtech
dotkey
google
adoscube
microsoft
yahoo

Sorted list is:
adoscube
arjtech
dotkey
google
microsoft
yahoo











Object Oriented Programming with C++
Page 17
Program No: 06
// Program to print Fibonacci series using recursive function.
#include<iostream.h>
#include<conio.h>
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;
cout<<"Enter limit:";
cin>>o;
for(int j=0;j<o;j++)
{
i=obj.fib(j);
cout<<i;
cout<<"\n";
Date:


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<iostream.h>
#include<conio.h>
#include<string.h>
class count
{
char s[200];
long int w,ch,l;
public:
void get()
{
cout<<"Enter paragraph:\n";
cin.getline(s,200);
}
void put();

void display()
{
cout<<"Words:"<<w<<"\n";
cout<<"Lines:"<<l<<"\n";
cout<<"Characters:"<<ch<<"\n";
}
};
void count::put()
{
w=1;
Date:


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<iostream.h>
#include<conio.h>
#include<string.h>
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<<"Enter numbers:";
m=n;
for(i=1;i<=m;i++)
{
cin>>a[i];
}
for(i=1;i<=m;i++)
{
for(j=i+1;j<=m;j++)
{
if(a[i]>a[j])
Date:


Object Oriented Programming with C++
Page 24
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}

cout<<"Integer numbers in ascending order:\n";
for(i=1;i<=m;i++)
{
cout<<a[i]<<"\n";
}
}
void sort1::nos()
{
float a[10],t;
int i,j,c;
cout<<"Enter the limit:";
cin>>c;
cout<<"Enter the numbers:";
for(i=1;i<=c;i++)
{
cin>>a[i];
}
for(i=1;i<=c;i++)
{


Object Oriented Programming with C++
Page 25
for(j=i+1;j<=c;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
cout<<"the float numbers in descending order:";
for(i=c;i>=1;i--)
{
cout<<a[i]<<"\n";
}
}
void sort1::nos(char s)
{
char a[10],t;
int i,j,n;
n=s;
cout<<"enter the limit:";
cin>>n;
cout<<"enter the characters:";
for(i=1;i<=n;i++)
cin>>a[i];
for(i=1;i<=n;i++)


Object Oriented Programming with C++
Page 26
{
for(j=i+1;j<=n;j++)
{
if(a[i]<a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
cout<<" character in sorted order:";
for(i=1;i<=n;i++)
{
cout<<a[i]<<"\n";
}
}
void main()
{
char s;
clrscr();
sort1 ob;
ob.nos(3);
ob.nos();
ob.nos(s);
getch();
}


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<iostream.h>
#include<conio.h>
class del
{
int n,a[10],b[10],i;
public:
void get();
void search();
};
void del::get()
{
cout<<"\nEnter limit:";
cin>>n;
cout<<"\nEnter array:";
for(i=0;i<n;i++)
{
cout<<"\n\t";
cin>>a[i];
}
}
void del::search()
{
Date:


Object Oriented Programming with C++
Page 29
int s;
cout<<"\nEnter element to search:";
cin>>s;
int j=0;
int f=0;
for(i=0;i<n;i++)
{
if(a[i]==s)
{
f=1;
}
}
for(i=0;i<n;i++)
{
if(a[i]!=s)
{
b[j]=a[i];
j++;
}
}
if(f==1)
{
cout<<"\nThe new array is :\n";
for(i=0;i<j;i++)
{


Object Oriented Programming with C++
Page 30
cout<<"\t"<<b[i]<<"\n";
}
}
else
cout<<"\n Sorry invalid array!";
}
void main()
{
clrscr();
del ob;
ob.get();
ob.search();
getch();
}











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 <iostream.h>
#include <conio.h>
class cylender
{
float r,h;
public:
void get();
float volume();
float area();
}ob;
void cylender::get()
{
cout<<"\nEnter Radius and height of the cylinder :";
cin>>r>>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<<"\nVolume= "<<vo;
area1=ob.area();
cout<<"\n Area= "<<area1;
getch();
}













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<iostream.h>
#include<conio.h>
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<<"Enter the elements of first matrix:\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
Date:


Object Oriented Programming with C++
Page 36
}
cout<<"Enter the elements of second matrix:\n";
for(i=0;i<t;i++)
{
for(j=0;j<s;j++)
{
cin>>b[i][j];
}
}
}
void matrix::sum()
{
c[i][j]=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}
void matrix::pro()
{
c[i][j]=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)


Object Oriented Programming with C++
Page 37
{
for(k=0;k<n;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
}
void matrix::display()
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<c[i][j]<<"\t";
}
cout<<"\n";
}
}
int main()
{
clrscr();
int a,b,c,d;
matrix m;
int ch;
d:
cout<<"----------------MENU---------------------\n";


Object Oriented Programming with C++
Page 38
cout<<"\n";
cout<<"1.Matrix Addition\n2.Matrix Multiplication\n"<<"3.Exit\n";
cout<<"Enter Your Choice:\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the row and column of matrix 1:";
cin>>a>>b;
cout<<"Enter the row and column of matrix 2:";
cin>>c>>d;
if((a==c)&&(b==d))
{
m.getdata(a,b,c,d);
m.sum();
cout<<"\tThe Sum of the Given Matrices are:\n";
m.display();
}
else
cout<<"Matrix cannot be added";
break;
case 2: cout<<"Enter the row size and column size of matrix 1:";
cin>>a>>b;
cout<<"Enter the row size and column size of matrix 2:";
cin>>c>>d;
m.getdata(a,b,c,d);
m.pro();


Object Oriented Programming with C++
Page 39
cout<<"\tThe Product of the given Matrices are:\n";
m.display();
break;
case 3:
return 0;
}
goto d;
getch();
}




















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<iostream.h>
#include<conio.h>
class stud
{
protected:
int no,m1,m2;
char name[25];
public:
void getdata();
};
void stud::getdata()
{
cout<<"Enter roll number:"<<"\n";
cin>>no;
cout<<"Enter name:"<<"\n";
cin>>name;
cout<<"Enter first mark:" <<"\n";
cin>>m1;
cout<<"Enter second mark:" <<"\n";
cin>>m2;
}

class test :public stud
{
protected:
Date:


Object Oriented Programming with C++
Page 42
int to;
public:
void getdata1()
{
to=m1+m2;
}
};

class result:public test
{
public:
void putdata()
{
cout<<"Student details\n";
cout<<"--------------------------------------------"<<"\n";
cout<<"Rollnumber :"<<no<<"\n";
cout<<"Name:"<<name<<"\n";
cout<<"Total mark:"<<to<<"\n";
}
};
void main()
{
int n;
clrscr();
result ob[50];
cout<<"Enter total number of students:";
cin>>n;


Object Oriented Programming with C++
Page 43
for(int j=0;j<n;j++)
{
ob[j].getdata();
ob[j].getdata1();
ob[j].putdata();
getch();
}
}


















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<iostream.h>
#include<conio.h>
class time
{
public:
int h,m,s;
void get()
{
cout<<" Enter Time (h:m:s) : ";
cin>>h>>m>>s;
}
void showTime()
{
cout<<"Sum is:"<<h<<":"<<m<<":"<<s;
}
time operator+(time t2)
{
time t;
t.h=h+t2.h;
t.m=m+t2.m;
t.s=s+t2.s;
if(t.s>60)
{
t.m=t.m+t.s/30;
t.s=t.s%60;
Date:


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<iostream.h>
#include<conio.h>
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<<x<<y<<z;
}
void distance::display1()
{
cout<<k<<l<<m;
}
void distance::operator-()
{
x=-x;
y=-y;
z=-z;
}
void distance::operator+()
{
k=+k;
l=+l;
m=+m;
}
void main()
{
clrscr();
distance ob;
ob.getdata(10,-20,30);
-ob;
ob.display();


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<iostream.h>
#include<conio.h>
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<<real<<"+"<<ima<<i;
}
Date:


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<<"\nEnter the imaginary part and real part:";
c1.get();
cout<<"\nEnter the second imaginary part and real part:";
c2.get();
c3=c1+c2;
cout<<"\nSum is:";
c3.put();
c4=c1-c2;
cout<<"\n difference is:";
c4.put();
c5=c1*c2;
cout<<"\nproduct is:";
c5.put();
c6=c1/c2;
cout<<"\ndivision:";
c6.put();
getch();
}





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<iostream.h>
#include<conio.h>
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<<"Enter the elements of first matrix:\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"Enter the elements of second matrix:\n";
Date:


Object Oriented Programming with C++
Page 57
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>b[i][j];
}
}
}
void matrix::sum()
{
int i,j;
c[i][j]=0;
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}
void matrix::display()
{
int i,j;
cout<<"SUM IS=\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)


Object Oriented Programming with C++
Page 58
{
cout<<c[i][j]<<"\t";
}
cout<<"\n";
}
}
void main()
{
clrscr();
int a,b,c,d;
cout<<"Enter the row and column of matrix 1:";
cin>>a>>b;
cout<<"Enter the row and column of matrix 2:";
cin>>c>>d;
matrix m;
if((a==c)&&(b==d))
{
m.getdata(a,b);
m.sum();
m.display();
}
else
cout<<"Matrix not added";
getch();
}




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<iostream.h>
#include<conio.h>
class empl
{
protected:
int age;
char name[20],place[20];
public:
void getdata()
{
cout<<"\n Enter employee details-------------------------------------"<<"\n";
cout<<"\nEnter name:";
cin>>name;
cout<<"\nEnter the age:";
cin>>age;
cout<<"\nEnter the place:";
cin>>place;
}
};
class salary
{
protected:
int salary;
char dept[10];
public:
Date:


Object Oriented Programming with C++
Page 61
void getsalary()
{
cout<<"\nEnter department:";
cin>>dept;
cout<<"\nEnter salary:";
cin>>salary;
}
};
class emp:public empl,public salary
{
public:
void display()
{
cout<<"EMPLOYEE DETAILS\n";
cout<<"________________\n";
cout<<"Name:"<<name<<"\n";
cout<<"Age:"<<age<<"\n";
cout<<"Place:"<<place<<"\n";
cout<<"Department:"<<dept<<"\n";
cout<<"Salary:"<<salary<<"\n";
}
};
void main()
{
clrscr();
emp ob1;
ob1.getdata();


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<iostream.h>
#include<conio.h>
class shape
{
virtual float area()=0;
virtual float perimeter()=0;
};
class circle:public shape
{
protected:
int r;
float a1,p1;
public:
void read()
{
cout<<"Enter radius:";
cin>>r;
}
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()
{
cout<<"Enter length & breadth:";
cin>>l>>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<<"1:CIRELE\n";
cout<<"2:RECTANGLE\n";
cout<<"3:EXIT\n";
int ch;
do
{
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
{
c.read();
c1=c.area();
c1=c.perimeter();
cout<<"area of circle="<<c1<<"\n";
cout<<"perimeter of circle="<<c1<<"\n";
break;
}
case 2:
{
r.get();
r1=r.area();


Object Oriented Programming with C++
Page 67
r2=r.perimeter();
cout<<"area of rectangle="<<r1<<"\n";
cout<<"perimeter of rectangle="<<r2<<"\n";
break;
}
default:
if(ch==3)
cout<<"u r going to exit";
else
cout<<"wrong entry\n";
break;
}
}
while(ch!=3);
getch();
}











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<iostream.h>
#include<conio.h>
#include<process.h>
class bank
{
float acno,depno;
char n[20],ad[20];
public:
void create();
void withdraw();
void deposite();
void display();
};
void bank::create()
{
cout<<"for creating a new account enter the details below....";
cout<<"\nEnter name:";
cin>>n;
cout<<"\nEnter address:";
cin>>ad;
cout<<"\nEnter account number and deposite amount:";
cin>>acno>>depno;
}

void bank::withdraw()
Date:


Object Oriented Programming with C++
Page 70
{
int i;
float with;
cout<<"Do you want to withdraw the amount ?"<<"\n 1.yes\n 2.no";
cin>>i;
if(i==1)
{
cout<<"Enter amount:";
cin>>with;
if(with>depno)
cout<<"You have not enough balance and having Rs:"<<depno<<"
only\n";
else if(with==depno)
cout<<"Not permitted ,zero balance\n";
if(with <depno)
{
depno=depno-with;
cout<<"\n\n Withdrawed Successfully";
}
else
{
cout<<"\n no balance";
}
}
}
void bank::deposite()
{
int j;


Object Oriented Programming with C++
Page 71
float dep;
cout<<"Do you want to deposite amount?"<<"\n 1.yes"<<"\n2.no";
cin>>j;
if(j==1)
{
cout<<"Enter amount:";
cin>>dep;
depno=depno+dep;
cout<<"deposited successfully";
}
}
void bank::display()
{
cout<<"\n\nbalance sheet...\n";cout<<"---------------------------\n";
cout<<"Ac_No"<<"\t\t"<<"Name:"<<"\t\t"<<"address\t\t"<<"Amount\t\t\n";
cout<<acno<<"\t\t"<<n<<"\t\t"<<ad<<"\t\t" <<depno<<"\n";
}
void main()
{
clrscr();
int i;
bank a[10];
bank *ptr;
ptr=a;
for(i=0;i<3;i++)
{



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<iostream.h>
#include<conio.h>
class student
{
protected:
int id;
char name[20];
public:
void getdata()
{
cout<<"\n Enter the student details"<<"\n";
cout<<"\n";
cout<<"\n"<<"Enter the name";
cin>>name;
cout<<"\n Enter the id"<<"\n";
cin>>id;
}
};
class test:public student
{
protected:
int mark1,mark2;
public:
void gettest()
{
Date:


Object Oriented Programming with C++
Page 75
cout<<"Enter the first mark"<<"\n";
cin>>mark1;
cout<<"Enter the second mark"<<"\n";
cin>>mark2;
}
};
class result:public test
{
protected:
int total;
public:
void display()
{
total=mark1+mark2;
cout<<"STUDENT DETAILS\n";
cout<<"________________\n";
cout<<"\n";
cout<<"Name:"<<name<<endl;
cout<<"ID:"<<id<<endl;
cout<<"Mark 1:"<<mark1<<endl;
cout<<"Mark 2:"<<mark2<<endl;
cout<<"Total:"<<total<<endl;
}
};
void main()
{
clrscr();


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<iostream.h>
#include<conio.h>
#include<string.h>
void main(int argc,char* argv[]) {
clrscr();
char t[20];
for(int i=0;i<argc;i++)
{
for(int j=i+1;j<argc;j++)
{
if(strcmp(argv[i],argv[j])>0)
{
strcpy(t,argv[i]);
strcpy(argv[i],argv[j]);
strcpy(argv[j],t);
}
}
}
cout<<" \n sorted names\n";
for(i=0;i<argc;i++)
{
cout<<"\t"<<argv[i]<<"\n";
}
getch();
}
Date:


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<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
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<n;i++)
{
for(j=1;j<n;j++)
{
if(a[i]<a[j])
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
Date:


Object Oriented Programming with C++
Page 81
}
void sort1::disp(int a[10],int n)
{
cout<<"The sorted array is:"<<endl;
for(i=1;i<n;i++)
{
cout<<a[i]<<endl;
}
}
void main(int argc,char* argv[])
{
clrscr();
sort1 s;
int n,a[10];
n=argc;
cout<<"Array before sort is:"<<endl;
for(int i=1;i<n;i++)
cout<<argv[i]<< " ";
for(i=1;i<n;i++)
a[i]=atoi(argv[i]);
s.names(a,n);
s.disp(a,n);
getch();
}





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 <fstream.h>
#include <conio.h>
void main()
{
clrscr();
ofstream obj("prime.txt",ios::out);
ofstream ob("nonprime.txt",ios::out);
int a,c,k,f;
ob<<"\t Non-Prime numbers are :\n";
a=1;
ob<<a;
a=2;
obj<<"\t Prime numbers are :\n";
obj<<a;
for(a=3;a<25;a++)
{
f=0;
c=a/2;
for(k=2;k<c;k++)
{
if(a%k==0)
{
f=1;
break;
}
Date:


Object Oriented Programming with C++
Page 84
}
if(f==0)
obj<<" "<<a;
else
ob<<" "<<a;
}
obj.close();
ob.close();
char ch;
ifstream f1("prime.txt",ios::in);
ifstream f2("nonprime.txt",ios::in);
while(f1)
{
f1.get(ch);
cout<<ch;
}
f1.close();
while(f2)
{
f2.get(ch);
cout<<ch;
}
f2.close();
getch();
}




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 <fstream.h>
#include <conio.h>
void main()
{
clrscr();
ofstream obj("even.txt",ios::out);
ofstream ob("odd.txt",ios::out);
int a,c,k,f;
ob<<"\n\t Odd Numbers are :\n";
obj<<"\t Even Numbers are :\n";
for(a=1;a<100;a++)
{
if(a%2==0)
obj<<" "<<a;
else
ob<<" "<<a;
}
obj.close();
ob.close();
char ch;
ifstream f1("even.txt",ios::in);
ifstream f2("odd.txt",ios::in);
while(f1)
{
f1.get(ch);
Date:


Object Oriented Programming with C++
Page 87
cout<<ch;
}
f1.close();
while(f2)
{
f2.get(ch);
cout<<ch;
}
f2.close();
getch();
}
















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 <iostream.h>
#include <conio.h>
#include <stdlib.h>
template <class t>
void sort(t a[20],int n)
{
int i,j;
t temp;
cout<<"\tSorted Elements\n";
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
cout<<"\t"<<a[i]<<"\n";
}
Date:


Object Oriented Programming with C++
Page 90
}
void main()
{
int i,n,b[20],ch;
char c[20];
float d[20];
cout<<"Enter the Limit for integer sort\n\t";
cin>>n;
cout<<"Enter ther INTEGER elements\n";
for(i=0;i<n;i++)
{
cin>>b[i];
}
sort(b,n);
cout<<"\tEnter the Limit for charector sort\n\t";
cin>>n;
cout<<"\tEnter ther CHARACTER elements\n";
for(i=0;i<n;i++)
{
cin>>c[i];
}
sort(c,n);
cout<<"\tEnter the Limit for floating point sort\n";
cin>>n;
cout<<"\tEnter ther FLOATINGPOINT elements\n";
for(i=0;i<n;i++)
{


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<iostream.h>
#include<conio.h>
class <template t>
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<<"overflow\n";
}
else
{
a[top]=item;
top++;
Date:


Object Oriented Programming with C++
Page 94
}
}
void stack::pop()
{
if(top==0)
{
cout<<"underflow\n";
}
else
{
top--;
}
}
void stack::display()
{
if(top==0)
{
cout<<"stack is null\n";
}
else
{
cout<<"stack elements are \n";
for(int i=0;i<=top;i++)
{
cout<<a[i]<<"\t";
}
}


Object Oriented Programming with C++
Page 95
}
void stack::ini()
{
top=0;
}
int main()
{
clrscr();
obj.ini();
int e,c;
cout<<"enter 1,2,3 or 4 for push,pop,display and exit\n";
l:
cin>>c;
switch(c)
{
case 1:
cout<<"enter element for push to stack\n";
cin>>e;
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<<"invalid option\n";
}
goto l;
getch();
}




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