Você está na página 1de 114

VALIA KOONAMBAIKULATHAMMA

COLLEGE OF ENGINEERING & TECHNOLOGY


Chavarcode, Parippally P.O, Trivandrum, Kerala.
Ph: no: 0470 - 2665223, Email:vksbkt2008@gmail.com, Website: www.vkcet.com
( Governed by Valiakoonambaikulam Sree Bhadrakali Kshethra Trust
Vadakkevila P O,Kollam-691010, Kerala )

13.507 Object Oriented Programming Lab


LABORATORY RECORD
Name :..
Class No : Semester & Branch....
Reg. No. University Exam :..

CERTIFIED THAT THIS IS A BONAFIDE RECORD OF


THE WORK DONE IN THE LABORATARY OF
VKCET,Chavarcode,Parippally

.....
Staff-in-charge

...................
Head of the Department
Submitted for the Practical Examination in

.. held on ..

.........
Internal Examiner

.........
External Examiner

13.507 Object Oriented Programming Lab

CONTENTS
Sl.
No

Date

Name of the Experiment

Page
No.

Swapping

Area of shapes

12

Power of a number

16

Factorial of a number

18

Student Details

20

Bank account details

24

Static member function

30

Object as arguments

32

Sum and difference of Complex numbers

34

10

Parameterized Constructor

38

11

Copy constructor

40

12

Destructor

42

13

Friend Function

44

14

Overloading unary operator

46

15

Overloading binary operator

48

16

Matrix operation

50

17

Constructors in derived class

56

18

Distance

60

19

Bookshop

64

20

Single Inheritance

72

Signature of
staff-incharge

13.507 Object Oriented Programming Lab

13.507 Object Oriented Programming Lab

CONTENTS
Sl.
No

Date

Name of the Experiment

Page
No.

21

Multiple Inheritance

76

22

Multilevel Inheritance

80

23

Student details using Inheritance

84

24

Bank - Inheritance

88

25

Shape virtual function

100

26

Class template

104

27

Swap using template

108

28

File using class

110

Signature of
staff-incharge

13.507 Object Oriented Programming Lab

13.507 Object Oriented Programming Lab

13.507 Object Oriented Programming Lab

Algorithm
1. Start
2. Create a class Swap with a,b as private data member and function cvalue with
2 integer value x1 & y1 as parameters,function cref with 2 integer pointer
variables *x2 & *y2 as parameters and function cads with 2 integer variables
&x3 & &y3 as parameters as the public member functions.
3. Create s as the object of the class Swap
4. Read two numbers a & b
5. Call function cvalue with a and b as actual parameters by using object s and dot
operator
6. Call function cref with a and b as actual parameters by using object s and dot
operator
7. Call function cads with &a and &b as actual parameters by using object s and
dot operator
8. Stop
Function:cvalue()
1. Start
2. assign x1 to t1,y1 to x1 and t1 to y1
3. Stop
Function:cref()
1. Start
2. assign *x1 to *t1,*y1 to *x1 and *t1 to *y1
3. Stop
Function:cads()
1. Start
2. assign x1 to t1,y1 to x1 and t1 to y1
3. Stop

13.507 Object Oriented Programming Lab

Date :

Experiment No: 1
SWAPPING

Aim
Program to perform swap operation using call by value, call by
reference, call by address.
Program
#include<iostream.h>
#include<conio.h>
class Swap
{
public:
int a,b;
void cvalue(int x1,int y1)
{
int t1;
t1=x1;
x1=y1;
y1=t1;
cout<<"\nAFTER SWAP A:"<<x1<<" B:"<<y1;
}
void cref(int *x2,int *y2)
{
int *t2;
*t2=*x2;
*x2=*y2;
*y2=*t2;
cout<<"\nAFTER SWAP A:"<<*x2<<" B:"<<*y2;
}
void cadd(int &x3,int &y3)
{
int t3;
t3=x3;
x3=y3;
y3=t3;
cout<<"\nAFTER SWAP A:"<<x3<<" B:"<<y3;
}
};

13.507 Object Oriented Programming Lab

Output
ENTER TWO NUMBERS :5

CALL BY VALUE
AFTER SWAP A=7 B=5

ENTER TWO NUMBERS :9

CALL BY REFERENCE
AFTER SWAP A=2 B=9

ENTER TWO NUMBERS :16

CALL BY ADDRESS
AFTER SWAP A=8 B=16

13.507 Object Oriented Programming Lab

int main()
{
clrscr();
int a,b;
Swap s;
cout<<"\nENTER
cin>>a>>b;
cout<<"CALL BY
s.cvalue(a,b);
cout<<"CALL BY
s.cref(&a,&b);
cout<<"CALL BY
s.cadd(a,b);
getch();
return 0;
}

TWO NUMBERS:";
VALUE";
REFERENCE";
ADDRESS";

Result
Successfully compiled and run the program to get the desired output.

13.507 Object Oriented Programming Lab

10

Algorithm
1. Start
2. Create class Shape with public member functions -area with floating value r as
parameter, area with integer values l & b as parameter,area with integer value
br & floating value h as parameter,function area with integer value a as
parameter
3. Create s as the object of class Shape
4. Read the radius of the circle r
5. Read the length and the breadth of rectangle l and b
6. Read the breadth and the height of triangle br and h
7. Read the side of the square a
8. Display area of the circle by calling function s.area(r)
9. Display area of the rectangle by calling function s.area( l , b )
10.Display area of the triangle by calling function s.area( br , h )
11.Display area of the square by calling function s.area(a)
12.Stop
Function:area()
1.
2.
3.
4.
5.

Start
Pass a floating value r as the formal parameter
area<-3.14*r*r
Display area
Stop

Function:area()
1.
2.
3.
4.
5.

Start
Pass 2 integer value l,b as the formal parameter
area<-l*b
Display area
Stop

Function:area()
1.
2.
3.
4.
5.

Start
Pass a integer value br and floating value h as the formal parameter
area<-0.5*br*h
Display area
Stop

11

13.507 Object Oriented Programming Lab

Date :

Experiment No: 2
AREA OF SHAPES

Aim
Program to find the area of circle, rectangle, triangle and square
using function overloading.
Program
#include<iostream.h>
#include<conio.h>
class Shape
{
public:
void area(float r)
{
float ar=3.14*r*r;
cout<<"\nAREA OF THE CIRCLE:"<<ar;
}
void area(int l,int b)
{
int ar=l*b;
cout<<"\nAREA OF THE RECTANGLE:"<<ar;
}
void area(float br,float h)
{
float ar=0.5*br*h;
cout<<"\nAREA OF THE TRIANGLE:"<<ar;
}
void area(int a)
{
int ar=a*a;
cout<<"\nAREA OF THE SQUARE:"<<ar;
}
};
int main()
{
clrscr();
float r,br,h;
int a,l,b;
cout<<"\n ENTER THE RADIUS OF THE CIRCLE:";
cin>>r;
cout<<"\nENTER THE LENGTH AND BREADTH OF RECTANGLE:";
cin>>l>>b;
cout<<"\nENTER THE BREADTH AND HEIGHT OF TRIANGLE:";
13.507 Object Oriented Programming Lab

12

Function:area()
1.
2.
3.
4.
5.

Start
Pass a integer value a as the formal parameter
area<-3.14*a*a
Display area
Stop

Output
ENTER RADIUS OF THE CIRCLE:1.0
ENTER LENGTH AND BREADTH OF THE RECTANGLE:2

ENTER BREADTH AND HEIGHT OF THE TRIANGLE: 3

5.5

ENTER SIDE OF THE SQUARE:6


.......AREA OF THE CIRCLE.........
3.14
......AREA OF THE RECTANGLE......
10
......AREA OF THE TRIANGLE......
7.5
......AREA OF THE SQUARE......
36

13

13.507 Object Oriented Programming Lab

cin>>br>>h;
cout<<"\nENTER THE SIDE OF A SQUARE:";
cin>>a;
Shape s;
s.area(r);
s.area(l,b);
s.area(br,h);
s.area(a);
getch();
return 0;
}

Result
Successfully compiled and run the program to get the desired output.

13.507 Object Oriented Programming Lab

14

Algorithm
1.
2.
3.
4.
5.
6.
7.
8.

Start
Create a class Power with powe() as public member function
Create p as the object of class Power
Call function p.powe() with no arguments
Call function p.powe() with 1 integer value as parameter
Call function p.powe() with 2 integer value as parameters
Stop

Function:powe()
1. Start
2. Pass 2 integer variables x & y as formal parameter
3. Calculate power of number by calling mathematical function pow() by passing
x & y as parameter
4. Display power of number
5. Stop

Output

POWER IS 1

POWER IS 2

POWER IS 9

15

13.507 Object Oriented Programming Lab

Date :

Experiment No: 3
POWER OF A NUMBER

Aim
To implement a program to find the power of a number.
Program
#include<iostream.h>
#include<conio.h>
#include<math.h>
class power
{
public:
void powe(int x=1,int y=1)
{
int pw=pow(x,y);
cout<<"\n the power of the number is"<<pw;
}
};
int main()
{
clrscr();
power p;
p.powe();
p.powe(2);
p.powe(3,3);
getch();
return 0;
}
Result
The program was successfully compiled and run to get the desired output.

13.507 Object Oriented Programming Lab

16

Algorithm
1. Start
2. Create a class factorial with n as the private data member and getdata(),fact()
as the public data member and create fact as the object of the class factorial
3. call getdata() using fact and dot operator
4. call fact() using fact and dot operator
5. stop
Function:getdata()
1. Start
2. read the number and stores it in n
3. stop
Function:fact()
1.
2.
3.
4.
5.
6.
7.

Start
set f as 1
intialize i as n
repeat steps 5-6 till i is greater than zero
f<-f*i
decrement i
Stop

Output

ENTER THE NUMBER :10


FACTORIAL OF 10 IS 24320

17

13.507 Object Oriented Programming Lab

Date :

Experiment No: 4
FACTORIAL OF A NUMBER

Aim
To implement a program to find the factorial of a number.
Program
#include<iostream.h>
#include<conio.h>
class factorial
{
int n;
public:
void getdata()
{
cout<<"\n ENTER THE NUMBER:";
cin>>n;
}
void fact();
};
void factorial::fact()
{
int f=1;
for(int i=n;i>0;i--)
f=f*i;
cout<<"\n FACTORIAL OF"<<n<<" "<<"is"<<f;
}
int main()
{
factorial fact;
clrscr();
fact.getdata();
fact.fact();
getch();
return 0;
}
Result
The program was successfully compiled and run to get the desired output.
13.507 Object Oriented Programming Lab

18

Algorithm
1. Start
2. Create a class Student with rno,name,gr,m1,m2,t,avg,g as private data member
and get(),tot(),grade(),show() as public member functions and create s as the
object of class Student
3. Read the number of students and stores it in n
4. initialise i as zero
5. Repeat steps 6-10 till i less than n
6. Call function s[i].get()
7. Call function s[i].tot()
8. Call function s[i].grade()
9. call function s[i].show()
10.increment i
11.Stop
Function:get()
1. Start
2. Read name,roll number and marks for student
3. Stop
Function:tot()
1.
2.
3.
4.
5.

Start
t<-m1+m2
avg<-t/2
avg/10
Stop

Function:grade()
1.
2.
3.
4.
5.
6.
7.
8.

Start
if g is 9 then gr<-A
if g is 8 then gr<-B
if g is 7 then gr<-C
if g is 6 then gr<-D
if g is 5 then gr<-E
Otherwise gr<-F
Stop

19

13.507 Object Oriented Programming Lab

Date :

Experiment No: 5
STUDENT DETAILS

Aim
To implement a program to display details of students in a class.
Program
#include<iostream.h>
#include<conio.h>
class Student
{
int rno,m1,m2,t,avg,g;
char name[20],gr;
public:
void get()
{
cout<<"\nENTER NAME:";
cin>>name;
cout<<"\nENTER ROLL NUMBER:";
cin>>rno;
cout<<"\nENTER MARKS OBTAINED:";
cin>>m1>>m2;
}
void tot()
{
t=m1+m2;
avg=t/2;
g=avg/10;
}
void grade();
void show()
{
cout<<" "<<rno<<"
"<<m2<<" "<<t<<" "<<gr;
}
};
void Student::grade()
{
switch(g)
{
case 9:gr='A';
case 8:gr='B';
case 7:gr='C';

"<<name<<"

"<<m1<<"

break;
break;
break;
13.507 Object Oriented Programming Lab

20

Function:show()
1. Start
2. Display rollnumber,name,marks,total and grade
3. Stop
Output
ENTER THE NO.OF STUDENTS:2
ENTER ROLL NUMBER:28
ENTER NAME:ANU
ENTER MARKS:88

92

ENTER ROLL NUMBER:14


ENTER NAME:MANU
ENTER MARKS:100
RNO NAME

98

MARK1

MARK2

TOTAL

GRADE

28

ANU

88

92

180

14

MANU

100

98

198

21

13.507 Object Oriented Programming Lab

case 6:gr='D';
break;
case 5:gr='E';
default:
gr='F'; break;
}
}

break;

int main()
{
clrscr();
Student s[10];
int n;
cout<<"\nENTER NO.OF STUDENTS:";
cin>>n;
for(int i=0;i<n;i++)
{
s[i].get();
s[i].tot();
s[i].grade();
}
cout<<"\nROLLNUMBER
NAME
MARK1
GRADE\n";
for(i=0;i<n;i++)
s[i].show();
getch();
return 0;
Result

MARK2

TOTAL

The program was successfully compiled and run to get the desired output.

13.507 Object Oriented Programming Lab

22

Algorithm
1. Start
2. Create a class Bank with name,actype,amount and bal as private datra members
and public member functions get(),deposit(),withdraw(),display()
3. Create b as the object of class bank
4. set ch as 1
5. Read no.of customers and stores it in n
6. Create account for each customer by calling function b[i].get()
7. Read choice from the menu and Stores it in c
8. Check whether ch is 1 then do steps 9-14
9. Read account number and stores it in an
10.Initialize i as zero
11.Repeat steps 12- 14 till i is less than the no.of customers
12.check if the account number is equal to b[i].accno
13.if yes,call function b[i].deposit() otherwise goto step 9
14.increment i
15.Check whether ch is 2 then do steps 16-21
16.Read account number and stores it in ann
17.Initialize i as zero
18.Repeat steps 19- 21 till i is less than the no.of customers
19.check if the account number is equal to b[i].accno
20.if yes,call function b[i].withdraw() otherwise goto step 16
21.increment i
22.Check whether ch is 3 then do steps 23-28
23.Read account number and stores it in an
24.Initialize i as zero
25.Repeat steps 26-28 till i is less than the no.of customers
26.check if the account number is equal to b[i].accno
27.if yes,call function b[i].display() otherwise goto step 23
28.increment i
29.check whether ch is 4 then exit from program
30.Stop
Function:get()
1.
2.
3.
4.
5.

Start
Read the name,account number,amount deposited,account type
set balance as zero
add balance amount with amount deposited and saves it in balance
Stop

23

13.507 Object Oriented Programming Lab

Date :

Experiment No: 6
BANK ACCOUNT DETAILS

Aim
To implement a program for display details of n customers in a Bank.
Program
#include<iostream.h>
#include<conio.h>
class Bank
{
char name[15],actype[25];
double amount,bal;
public:
long accno;
void get()
{
cout<<"\nENTER NAME:";
cin>>name;
cout<<"\nENTER ACCOUNT NUMBER:";
cin>>accno;
cout<<"\nENTER AMOUNT TYPE:";
cin>>actype;
cout<<"\nENTER AMOUNT DEPOSITE:";
cin>>amount;
bal=0;
bal=bal+amount;
}
void deposit()
{
double dep;
cout<<"\nENTER AMOUNT:";
cin>>dep;
bal=bal+dep;
}
void withdraw()
{
double wd;
cout<<"\nENTER AMOUNT:";
cin>>wd;
bal=bal-wd;
}
13.507 Object Oriented Programming Lab

24

Function:deposit()
1.
2.
3.
4.

Start
Read the amount to deposit
add amount to balance
Stop

Function:withdraw()
1.
2.
3.
4.

Start
Read the amount to withdraw
subtract amount from balance
Stop

Function:display()
1. Start
2. Display name,account trype,balance
3. Stop
Output
ENTER NO.OF CUSTOMER:1
ACCOUNT CREATION
ENTER NAME:AKHIL
ENTER ACCOUNTNO:13430028
ENTER AMOUNT DEPOSITED:50000
ENTER ACCOUNT TYPE:SAVINGS
1.DEPOSIT

2.WITHDRAW

3.DISPLAY

ENTER YOUR CHOICE:1


ENTER ACCOUNT NUMBER:134300214
ENTER AMOUNT:10000
PRESS 1 TO CONTINUE ELSE PRINT 0:1
25

13.507 Object Oriented Programming Lab

4.EXIT

void display()
{
cout<<"\nNAME:"<<name;
cout<<"\nACCOUNT TYPE:"<<actype;
cout<<"\nBALANCE:"<<bal;
}
};
int main()
{
clrscr();
int n,ch=1,c;
Bank b[25];
cout<<"\nENTER NUMBER OF CUSTOMERS:";
cin>>n;
cout<<"\nACCOUNT CREATION";
for(int i=0;i<n;i++)
b[i].get();
cout<<"\n1.DEPOSITE\t\t2.WITHDRAW\t\t3.DISPLAY\t\t4.EXIT"
;
while (ch==1)
{
cout<<"\nENTER YOUR CHOICE:";
cin>>c;
switch(c)
{
case 1:long an;
cout<<"\nENTER ACCOUNT NUMBER:";
cin>>an;
for(int i=0;i<n;i++)
{
if(an==b[i].accno)
b[i].deposit();
}
break;
case 2:long ann;
cout<<"\nENTER ACCOUNT NUMBER:";
cin>>ann;
for(i=0;i<n;i++)
{
if(ann==b[i].accno)
b[i].withdraw();
}
break;
case 3:long ano;
cout<<"\nENTER ACCOUNT NUMBER:";
cin>>ano;
13.507 Object Oriented Programming Lab

26

ENTER YOUR CHOICE:3


ENTER ACCOUNT NUMBER:134300214
NAME:AKHIL
ACCOUNTTYPE:SAVINGS
BALANCE:60000
PRESS 1 TO CONTINUE ELSE PRINT 0:1
ENTER YOUR CHOICE:2
ENTER ACCOUNT NUMBER:134300214
ENTER AMOUNT:1000
PRESS 1 TO CONTINUE ELSE PRINT 0:1
ENTER YOUR CHOICE:3
ENTER ACCOUNT NUMBER:134300214
NAME:AKHIL
ACCOUNTTYPE:SAVINGS
BALANCE:59000
PRESS 1 TO CONTINUE ELSE PRINT 0:1
ENTER YOUR CHOICE:4

27

13.507 Object Oriented Programming Lab

for(i=0;i<n;i++)
{
if(ano==b[i].accno)
b[i].display();
}
break;
default:cout<<"WRONG CHOICE";
break;
}
cout<<"\nPRESS 1 TO CONTINUE ELSE PRESS 0:";
cin>>ch;
}
getch();
return 0;
}
Result
The program was successfully compiled and run to get the desired output.

13.507 Object Oriented Programming Lab

28

Algorithm
1. Start
2. Create a class Test with integer variable code and static integer variable count
as private data member & public member functions setcode(),showcode() and a
static function showcount()
3. Create t1,t2 as the object of Test
4. Call function t1.setcode()
5. Call function t2.setcode()
6. Accessing static function showcount()
7. Create t3 as the object of Test
8. Call function t3.setcode()
9. Accessing static function showcount()
10.Call function t1.showcode()
11.Call function t2.showcode()
12.Call function t3.showcode()
13.Stop
Function:setcode()
1. Start
2. increment count and stores it in code
3. Stop
Function:showcode()
1. Start
2. Display code
3. Stop
Function:showcount()
1. Start
2. Display count
3. Stop
Output
COUNT:1
COUNT:2
COUNT:3
OBJECT NUMBER:1
OBJECT NUMBER:2
OBJECT NUMBER:3
29

13.507 Object Oriented Programming Lab

Date :

Experiment No: 7
STATIC MEMBER FUNCTION

Aim
A program to display count of number of objects created using static
member function.
Program
#include<iostream.h>
#include<conio.h>
class test{
int code;
static int count;
public:
void setcode(void){
code=++count;
}
void showcode(void){
cout<<"\n object number :"<<code<<"\n";
}
static void showcount(void){
cout<<"\n count:"<<count<<"\n";
}
};
int test::count=0;
int main()
{
clrscr();
test t1,t2;
t1.setcode();
t2.setcode();
test::showcount();
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
getch();
return 0;
}
Result
The expected output is obtained successfully
13.507 Object Oriented Programming Lab

30

Algorithm
1. Start
2. Create a class Time with 2 integer variables hours and minutes as private data
member & public member functions gettime(),puttime(),sum()
3. Create T1,T2,T3 as the objects of Time
4. Call function T1.gettime() by passing 2 integer values
5. Call function T2.gettime() by passing 2 integer values
6. Call function T3.sum() by passing T1 & T2
7. Call display function T1.puttime()
8. Call display function T2.puttime()
9. Call display function T3.puttime()
10.Stop
Functio:gettime()
1.
2.
3.
4.

Start
Pass 2 integer variables h and m as formal parameter
Assign h to hours and m to minutes
Stop

Function:puttime()
1. Start
2. Display hours and minutes
3. Stop

Function:sum()
1.
2.
3.
4.
5.
6.
7.

Start
Pass objects t1,t2 of Time as formal parameter
minutes<-t1.minutes+t2.minutes
hours<-minutes/60
minutes<-minutes%60
hours<-hours+t1.hours+t2.hours
Stop

Output
T1=2 HOURS & 45 MINUTES
T2=3 HOURS & 30 MINUTES
T3=6 HOURS & 15 MINUTES

31

13.507 Object Oriented Programming Lab

Date :

Experiment No: 8
OBJECT AS ARGUMENTS

Aim
A program to implement the use of Object as function arguments.
Program
#include<iostream.h>
#include<conio.h>
class time
{
int hours,minutes;
public:
void gettime(int h,int m){
hours=h;
minutes=m;
}
void puttime(void){
cout<<hours<<"hours and";
cout<<minutes<<"minutes"<<"\n";
}
void sum(time,time);
};
void time::sum(time t1,time t2){
minutes=t1.minutes+t2.minutes;
hours=minutes/60;
minutes=minutes%60;
hours=hours+t1.hours+t2.hours;
}
int main(){
clrscr();
time T1,T2,T3;
T1.gettime(2,45);
T2.gettime(3,30);
T3.sum(T1,T2);
cout<<"\n DISPLAY TIME T1 T2 AND T3\n";
cout<<"\nT1=";T1.puttime();
cout<<"\nT2=";T2.puttime();
cout<<"\nT3=";T3.puttime();
getch();
return 0;
}
Result
The expected output is obtained successfully.
13.507 Object Oriented Programming Lab

32

Algorithm
1. Start
2. Create class Complex with 4 integer variables x,y,a,b as private data member
& public member functions get(),sum(),sub()
3. Create A,B,C as the objects of class Complex
4. Call function A.get()
5. Call function B.get()
6. Display first complex number by calling A.display()
7. Display second complex number by calling B.display()
8. Call function C.sum() by passing A & B as parameters
9. Call function C.sub() by passing A & B as parameters
10.Stop
Function:get()
1. Start
2. Read real and imaginary part of complex number
3. Stop
Function:sum()
1.
2.
3.
4.
5.
6.
7.

Start
Pass 2 objects A & B of class Complex
Create C as the object of Complex
C.x<-A.x+B.x
C.y<-A.y+B.y
Display sum
Stop

Function:sub()
1.
2.
3.
4.
5.
6.
7.

Start
Pass 2 objects A & B of class Complex
Create C as the object of Complex
C.x<-A.x-B.x
C.y<-A.y-B.y
Display sub
Stop

Function:display()
1. Start
2. Display x & y
3. Stop
33

13.507 Object Oriented Programming Lab

Date :

Experiment No: 9
SUM & DIFFERENCE OF COMPLEX NUMBERS

Aim
A program to find the sum and difference of two complex numbers.
Program
#include<iostream.h>
#include<conio.h>
class complex
{
int x,y,a,b;
public:
void get()
{
cout<<"\n Enter a complex number:";
cout<<"\nReal:";
cin>>x;
cout<<"\nImaginary:";
cin>>y;
}
void sum(complex A,complex B)
{
complex c;
c.x=A.x+B.x;
c.y=A.y+B.y;
cout<<"\nSum:"<<c.x<<"+i"<<c.y;
}
void sub(complex A,complex B)
{
complex c;
c.x=A.x-B.x;
c.y=A.y-B.y;
cout<<"\n Sub:"<<c.x<<"+i"<<c.y;
}
void display()
{
cout<<x<<"+i"<<y;
}
};

13.507 Object Oriented Programming Lab

34

Output
ENTER A COMPLEX NO
REAL:5
IMAGINARY:6
ENTER A COMPLEX NO
REAL:3
IMAGINARY:5
FIRST COMPLEX NO:5+i6
SECOND COMPLEX NO:3+i5
SUM:8+i11
SUB:2+i1

35

13.507 Object Oriented Programming Lab

int main()
{
complex A,B,c;
clrscr();
A.get();
B.get();
cout<<"\n The First Complex No:";
A.display();
cout<<"\n The Second Complex No:";
B.display();
c.sum(A,B);
c.sub(A,B);
getch();
return 0;
}
Result
Successfully compiled and run the program to get desired output.

13.507 Object Oriented Programming Lab

36

Algorithm
1. Start
2. Create a class Point with x,y as private data member,display() as the public
member function
3. Create a constructor Point() for the class Point with 2 integer values a & b as
parameters
4. Assign a to x and b to y
5. Create p1(1,1),p2(5,10) as the object of class Point
6. Call display function p1.display()
7. Call display function p2.display()
8. Stop
Function:display()
1. Start
2. Display x & y
3. Stop
Output
POINT P1=(1,1)
POINT P2=(5,10)

37

13.507 Object Oriented Programming Lab

Date :

Experiment No: 10
PARAMETERIZED CONSTRUCTOR

Aim
To implement a program using parameterized constructor.
Program
#include<iostream.h>
#include<conio.h>
class point
{
int x,y;
public:
point(int a,int b)
{
x=a;
y=b;
}
void display()
{
cout<<"("<<x<<","<<y<<")"<<"\n";
}
};
int main()
{
clrscr();
point p1(1,1);
point p2(5,10);
cout<<"\npoint p1=";
p1.display();
cout<<"\npoint p2=";
p2.display();
getch();
return 0;
}
Result
The program was successfully compiled and run to get the desired output.

13.507 Object Oriented Programming Lab

38

Algorithm
1. Start
2. Create a class Code with id as private data member,display() as the public
member function
3. Create a constructor Code() for the class Code with a integer values a as
parameter
4. Assign a to id
5. Create A(100) as the object of class Code
6. Call copy constructor B(A)
7. Call again copy constructor C<-A
8. Create D as the object of Code
9. Assign A to D
10.Display id of A by calling A.display()
11.Display id of B by calling B.display()
12.Display id of C by calling C.display()
13.Display id of D by calling D.display()
14.Stop
Function:display()
1. Start
2. Display id
3. Stop
Output
ID OF A:100
ID OF B:100
ID OF C:100
ID OF D:100

39

13.507 Object Oriented Programming Lab

Date :

Experiment No: 11
COPY CONSTRUCTOR

Aim
To implement a program using copy constructor.
Program
#include<iostream.h>
#include<conio.h>
class code
{
int id;
public:
code()
{}
code(int a){
id=a;
}
void display(){
cout<<"\n "<<id;
}
};
int main()
{
clrscr();
code A(100);
code B(A);
code C=A;
code D;
D=A;
cout<<"\n id of A:";
A.display();
cout<<"\n id of B:";
B.display();
cout<<"\n id of C:";
C.display();
cout<<"\n id of D:";
D.display();
getch();
return 0;
}
Result
The program was successfully compiled and run to get the desired output.

13.507 Object Oriented Programming Lab

40

Algorithm
1. Start
2. Initialise integer variable count as zero
3. Create class Test
4. Create a constructor Test() for the class Test
5. increment count
6. Display message
7. Create a destructor ~Test() for the class Test
8. Display message
9. decrement count
10.Create T1 as the object of Test by creating first object inside the main block
11.Create T2 and T3 as the object of Test by creating two more objects inside
block
12.leaving from inner block
13.leaving from main block
14.Stop

Output
INSIDE THE MAIN BLOCK..
CREATING FIRST OBJECT T1..
CONSTRUCTOR MSG:OBJECT NUMBER 1 CREATED..
INSIDE BLOCK 1..
CREATING TWO MORE OBJECTS T2 AND T3..
CONSTRUCTOR MSG:OBJECT NUMBER 2 CREATED..
CONSTRUCTOR MSG:OBJECT NUMBER 3 CREATED..
LEAVING BLOCK 1..
DESTRUCTOR MSG:OBJECT NUMBER 3 DESTROYED..
DESTRUCTOR MSG:OBJECT NUMBER 2 DESTROYED..
BACK INSIDE THE MAIN BLOCK..
DESTRUCTOR MSG:OBJECT NUMBER 1 DESTROYED..

41

13.507 Object Oriented Programming Lab

Date :

Experiment No: 12
DESTRUCTOR

Aim
To implement a program using destructor.
Program
#include<iostream.h>
#include<conio.h>
int count=0;
class test
{
public:
test(){
count++;
cout<<"\n\n CONSTRUCTOR MESSAGE:OBJECT
NUMBER"<<count<<"CREATED";
}
~test(){
cout<<"\n\n DESTRUCTOR MESSAGE:OBJECT
NUMBER"<<count<<"DESTROYED";
count--;
}
};
int main(){
clrscr();
cout<<"\n INSIDE THE MAIN BLOCK";
cout<<"\n\n CREATING FIRST OBJECT T1..";
{
test T1;
{
cout<<"\n\n INSIDE BLOCK 1...";
cout<<"\n\n CREATING TWO MORE OBJECTS T2 AND
T3..";
test T2,T3;
cout<<"\n\n LEAVING BLOCK 1..";
}
cout<<"\n\n BACK INSIDE THE MAIN BLOCK..";
}
getch();
return 0;
}
Result
The program was successfully compiled and run to get the desired output.
13.507 Object Oriented Programming Lab

42

Algorithm
1. Start
2. Create a class XYZ with integer variable data as the private member and public
member functions setvalue() and declare friend function add()
3. Create a class ABC with integer variable data as the private member and public
member functions setvalue() and declare friend function add()
4. Create X as the object of class XYZ, A as the object of class ABC
5. Call function X.setvalue()by passing 5 as parameter
6. Call function A.setvalue()by passing 50 as parameter
7. Call add function by passing X & A as parameters
8. Stop

Function:setvalue()
1.
2.
3.
4.

Start
Pass integer value as the formal parameter
Assign value to data
Stop

Function:add()
1.
2.
3.
4.

Start
Pass object obj1 of XYZ & object obj2 of ABC as formal parameter
Display sum of values of two classes by adding obj1.data and obj2.data
Stop

Output
SUM OF DATA VALUES OF XYZ AND ABC OBJECTS USING FRIEND
FUNCTION=55

43

13.507 Object Oriented Programming Lab

Date :

Experiment No: 13
FRIEND FUNCTION

Aim
A program to find the sum of data values used in two classes.
Program
#include<iostream.h>
#include<conio.h>
class ABC;
class XYZ{
int data;
public:
void setvalue(int value){
data=value;
}
friend void add(XYZ,ABC);
};
class ABC{
int data;
public:
void setvalue(int value)
{
data=value;
}
friend void add(XYZ,ABC);
};
void add(XYZ obj1,ABC obj2){
cout<<"SUM OF DATA VALUES OF XYZ AND ABC OBJECTS
USING FRIEND FUNCTION="<<obj1.data+obj2.data;
}
int main(){
clrscr();
XYZ X;
ABC A;
X.setvalue(5);
A.setvalue(50);
add(X,A);
getch();
return 0;
}
Result
The program was successfully compiled and run to get the desired output.

13.507 Object Oriented Programming Lab

44

Algorithm
1. Start
2. Create a class Space with 3 integer variables x,y and z as private data member
& public member functions getdata(),display(),operator-()
3. Create s as the object of class Space
4. Call function s.getdata() by passing 3 values
5. Display S by calling function s.display()
6. Activates operator-() function
7. Display -S by calling function s.display()
8. Stop
Function:getdata()
1.
2.
3.
4.
5.
6.

Start
Pass 3 integer variables a,b and c as formal parameter
Assign a to x
Assign b to y
Assign c to z
Stop

Function:display()
1.
2.
3.
4.
5.

Start
Display x
Display y
Display z
Stop

Function:operator-()
1.
2.
3.
4.
5.

Start
Assign -x to x
Assign -y to y
Assign -z to z
Stop

Output
S: X=10 Y=-20 Z=30
-S: X=-10 Y= 20 Z=-30

45

13.507 Object Oriented Programming Lab

Date :

Experiment No: 14
OVERLOADING UNARY OPERATOR

Aim
A program to overload a unary operator.
Program
#include<iostream.h>
#include<conio.h>
class space{
int x,y,z;
public:
void getdata(int a,int b,int c);
void display();
void operator-();
};
void space::getdata(int a,int b,int c){
x=a;
y=b;
z=c;
}
void space::display()
{
cout<<"x="<<x<<" ";
cout<<"y="<<y<<" ";
cout<<"z="<<z<<" ";
}
void space::operator-(){
x=-x;
y=-y;
z=-z;
}
int main(){
clrscr();
space S;
S.getdata(10,-20,30);
cout<<"S:";
S.display();
-S;
cout<<"\n-S:";
S.display();
getch();
return 0;
}
Result
The program was successfully compiled and run to get the desired output.
13.507 Object Oriented Programming Lab

46

Algorithm
1. Start
2. Create a class complex with 2 integer variables real and img as private data
member & public member functions get(),display(),operator+()
3. Create c1,c2,c3 as the objects of class complex
4. Call functions c1.get() ,c2.get()
5. Call function operator+() and the result is stored in c3
6. Display c1 by calling function c1.display()
7. Display c2 by calling function c2.display()
8. Display c3 by calling function c2.display()
9. Stop
Function:get()
1. Start
2. Read the value for real and img
3. Stop
Function:display()
1. Start
2. Display the values of real and img
3. Stop
Function:operator+()
1. Start
2. Pass the object c as formal parameter
3. Assign temp.real as the sum of real and c.real
4. Assign temp.img as the sum of img and c.img
5. return the value of temp
6. Stop
Output
ENTER FIRST COMPLEX NUMBER
ENTER THE REAL AND IMAGINARY PART: 2 5
ENTER SECOND COMPLEX NUMBER
ENTER THE REAL AND IMAGINARY PART: 3 2
FIRST COMPLEX NUMBER : 2 + i 5
SECOND COMPLEX NUMBER : 3 + i 2
SUM= 5 + i 7
47

13.507 Object Oriented Programming Lab

Date :

Experiment No: 15
OVERLOADING BINARY OPERATOR

Aim
A program to overload a binary operator.
Program
#include<iostream.h>
#include<conio.h>
class complex{
float x,y;
public:
complex(){}
complex(float real,float imag){
x=real;
y=imag;
}
complex operator+(complex);
void display();
};
complex complex::operator+(complex c){
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void complex::display(){
cout<<x<<"+j"<<y<<"\n";
}
int main(){
clrscr();
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
cout<<"c3=";
c3.display();
getch();
return 0;
}
Result
The program was successfully compiled and run to get the desired output.
13.507 Object Oriented Programming Lab

48

Algorithm
1. Start
2. Create a class with a[][] as its private member
and,n,i,j,get(),show()operator+(matrix), operator-(matrix),operator*(matrix) as
its public members
3. Call get() to read two matrices
4. Call show() to display two matrices
5. Check row1 equal row2 and column1 equal to row2
6. Then add two matrices
7. Then subtract two matrices
8. Check rows equal to column2 and column1 equal to row2
9. Check row1 equals to column2 and column1 rqual to row2
10.Then multiply two matrices
11.Stop
Function:get()
1.
2.
3.
4.
5.
6.
7.
8.

Start
Read m,n
for(i<-0;i<m;i++)
for(j<-0;j<n;j++)
Read a[i][j]
End for
End for
Stop

Function:show()
1.
2.
3.
4.
5.
6.
7.

Start
for(i<-0;i<m;i++)
for(j<-0;j<n;j++)
Display a[i][j]
End for
End for
Stop

Function:operator+(matrix)
1.
2.
3.
4.

Start
t.m<-m
t.n<-n
for(i<-0;i<m;i++)

49

13.507 Object Oriented Programming Lab

Date :

Experiment No: 16
MATRIX OPERATION

Aim
To perform matrix operations using operator overloading.
Program
#include<iostream.h>
#include<conio.h>
class matrix
{
int a[10][10];
public:
int m,n,i,j;
void get();
void show();
matrix operator+(matrix);
matrix operator-(matrix);
matrix operator*(matrix);
};
void matrix::get()
{
cout<<"Enter the row and column\n";
cin>>m>>n;
cout<<"Enter the elements\n";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
}
void matrix::show()
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j];
cout<<"\t";
}
cout<<"\n";
}
}
13.507 Object Oriented Programming Lab

50

5.
6.
7.
8.

for(j<-0;j<n;j++)
End for
End for
Stop

Function:operator-(matrix)
1.
2.
3.
4.
5.
6.
7.
8.
9.

Start
t.m<-m
t.n<-n
for(i<-0;i<m;i++)
for(j<-0;j<n;j++)
t.a[i][j]<-a[i][j]-m1.a[i][j]
End for
End for
Stop

Function:operator*(matrix)
1. Start
2. t.m<-m
3. t.n<-n
4. for(j<-0;j<n;j++)
5. t.a[i][j]=0
6. for(j<-0;j<n;j++)
7. for(k<-0;k<-m;k++)
8. t.a[i][j]<-a[i][j]+m1.a[i][k]*a[k][j]
9. End for
10.End for
11.End for
12.Stop

Output
1st matrix
Enter the row and column
2

Enter the elements

51

13.507 Object Oriented Programming Lab

matrix matrix::operator+(matrix m1)


{
matrix t;
t.m=m;
t.n=n;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
t.a[i][j]=a[i][j]+m1.a[i][j];
}
return t;
}
matrix matrix::operator-(matrix m1)
{
matrix s;
s.m=m;
s.n=n;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
s.a[i][j]=a[i][j]-m1.a[i][j];
return s;
}
matrix matrix::operator*(matrix m1)
{
matrix t;
t.m=m;
t.n=n;
for(i=0;i<m;i++)
{
t.a[i][j]=0;
for(j=0;j<n;j++)
for(int k=0;k<m;k++)
t.a[i][j]=a[i][j]+m1.a[i][k]*a[k][j];
}
return t;
}
int main()
{
clrscr();
matrix m1,m2,m3;
cout<<"1st matrix\n";
m1.get();
m1.show();
cout<<"2nd matrix\n";
m2.get();
m2.show();
13.507 Object Oriented Programming Lab

52

2nd matrix
Enter the row and column
2

Enter the elements


1

sum:
2

sub:
0

mul:
2

53

13.507 Object Oriented Programming Lab

if(m1.m==m2.m&&m1.n==m2.n)
{
m3=m1+m2;
cout<<"Sum:\n";
m3.show();
}
else
cout<<"Addition not possible\n";
if(m1.m==m2.m&&m1.n==m2.n)
{
m3=m1-m2;
cout<<"\nSub:\n";
m3.show();
}
else
cout<<"Subtraction is not possible\n";
if(m1.m==m2.m&&m1.n==m2.n)
{
cout<<"\nMul:\n";
m3=m1*m2;
m3.show();
}
else
cout<<"Multiplication not possible\n";
getch();
return 0;
}

Result
The program was successfully compiled and run to get the desired output.

13.507 Object Oriented Programming Lab

54

Algorithm
1.
2.
3.
4.
5.
6.
7.
8.
9.

Start
Create a class alpha with a constructor and a function
Create a class beta with a constructors and a function
Create a class gamma that inherites beta and alpha .It has a constructor and a
function.
Creating object of class gamma.
Call function g.show_x()
Call function show_y()
Call function show_mn()
Stop.

Function: alpha()
1. Start
2. x<-i
3. Stop
Function :show_x()
1. Start
2. Display x.
3. Stop
Function :beta()
1. Start
2. y<-j
3. Stop
Function:show_y()
1. Start
2. Display y
3. Stop
Function: gamma()
1.
2.
3.
4.

Start
m<-c
n<-d
Stop

55

13.507 Object Oriented Programming Lab

Date :

Experiment No: 17
CONSTRUCTORS IN DERIVED CLASS

Aim
A program to implement constructors in derived class.
Program
##include<iostream.h>
#include<conio.h>
class alpha
{
int x;
public:
alpha(int i)
{
x=i;
cout<<"\n....ALPHA INITIALIZED.....\n";
}
void showx()
{
cout<<"X="<<x<<"\n";
}
};
class beta
{
float y;
public:
beta(float j)
{
y=j;
cout<<"\n....BETA INITIALIZED.....\n";
}
void showy()
{
cout<<"Y="<<y<<"\n";
}
};
class gamma:public beta,public alpha
{
int m,n;
public:
gamma(int a,float b,int c,int d):alpha(a),beta(b)
{
m=c;
13.507 Object Oriented Programming Lab

56

Function:show_mn()
1.
2.
3.
4.

Start
Display m
Display n
stop.

OUTPUT

BETA INITIALIZED
ALPHA INITIALIZED
GAMMA INITIALIZED
X=5
Y=10.75
M=20
N=30

57

13.507 Object Oriented Programming Lab

n=d;
cout<<"\n.....GAMMA INITIALIZED.....\n";
}
void showmn()
{
cout<<"M="<<m<<"\n"<<"N="<<n<<"\n";
}
};
int main()
{
clrscr();
gamma g(5,10.7,20,30);
cout<<"\n";
g.showx();
g.showy();
g.showmn();
getch();
return 0;
}
Result
The program was successfully compiled and run to get the desired output.

13.507 Object Oriented Programming Lab

58

Algorithm
1. Start
2. Create a class metres with m and cm as its private members and get(), show()
as its public members and function sum() as friend to it.
3. Create a class inches with f and in as its private members and gets(), shows()
as its public members and function sum() as friend to it.
4. Create m1 as the object of class metres
5. Create m2 as the object of class inches
6. Call function m1.get()
7. Call function m2.gets()
8. Call function sum()
9. Stop
Function:get()
1. Start
2. Read m,cm
3. Stop
Function:show()
1. Start
2. Display m,cm
3. Stop
Function:gets()
1. Start
2. Read f,in
3. Stop
Function:shows()
1. Start
2. Display f,in
3. Stop
Function:sum()
1. Start
2. Pass the objects m1 and m2 as formal parameter
3. Read ch
4. if ch is 1 then,
5. Assign fm as product of m2.f and 0.3048
6. Assign incm as product of m2.in and 2.54
7. Assign s1 as the sum of m1.m and fm
8. Assign s2 as the sum of m1.cm and incm
9. Display s1,s2
10.End if
11.if ch is 2 then,
59

13.507 Object Oriented Programming Lab

Date :

Experiment No: 18
DISTANCE

Aim
Create a class DM and Db which stores the value of distances.DM
stores distance in m & cm and DB in feet & inch. Write a program
that can read values for the class objects & add one object of DM
with another object of DB. Use a friend function to carry out the
addition operation. The display should be in the format of feet &
inches or m &cm.
Program
# include<iostream.h>
#include<conio.h>
class DB;
class DM
{
double m,cm;
public:
void get()
{
cout<<"Enter distance m & cm";
cin>>m>>cm;
}
void show()
{
cout<<"Distance in m &cm:<<m<<"m"<<cm<<"cm";
}
friend void sum(DM,DB);
};
class DB
{
double f,in;
public:
void gets()
{
cout<<"Enter distance in feet & in\n";
cin>>f>>in;
}
void shows()
{
cout<<"\nDistance in feet & inch:"<<f<<"feet
"<<in<<"in" ;
}
13.507 Object Oriented Programming Lab

60

Assign mf as quotient of m1.m and 0.3048


12.Assign cmin as quotient of m1.cm and 2.54
13.Assign s3 as the sum of m2.f and mf
14.Assign s4 as the sum of mf.in and cmin
15.Display s3,s4
16.End if
17.Stop
Output

Enter Distance in m&cm


1 1
Enter distance in feet & in
1 1
Distance in m&cm : 1m 1cm
Distance in feet & inch : 1feet 1in
1. Distance in m&cm
2. Distance in feet & inch
Enter your choice 1
Total distance : 1.3048m 3.54cm

61

13.507 Object Oriented Programming Lab

friend void sum(DM,DB);


};
void sum(DM m1,DB m2 )
{
cout<<"\n\n1:Distance in m& cm";
cout<<"\n\n2:Distance in feet & inch";
cout<<"\nEnter your choice";
int ch;
cin>>ch;
if(ch==1)
{
double fm=m2.f * 0.3048;
double incm=m2.in*2.54;
double s1=m1.m+fm;
double s2=m1.cm+incm;
cout<<"\n Toatl distance:"<<s1<<"m"<<" and
"<<s2<<"cm"<<"\n";
}
if(ch==2)
{
double mf=m1.m/0.3048;
double cmin=m1.cm/2.54;
double s3=mf+m2.f;
double s4=cmin+m2.in;
cout<<"\nTotal Distance:"<<s3<<"feet"<<"
"<<s4<<"in"<<"\n";
}
}

and

int main()
{
DM m1;
DB m2;
clrscr();
m1.get();
m2.gets();
m1.show();
m2.shows();
sum(m1,m2);
getch();
return 0;
}Result
The program was successfully compiled and run to get the desired output.

13.507 Object Oriented Programming Lab

62

Algorithm
1. Start
2. Create a class book with aname,title,pub,sp,price,Read(),search(),uprice() as its
members for an array of objects.
3. Read n and set ts->0
4. for(i<-0;i<n;i++)
5. Call Read()
6. End for
7. From the menu,Read any choice
8. if ch<-1 then Read nam,tit
9. for(i<-0;i<n;i++)
10.Compare aname,nam& tit,title
11.if it is same call search()
12.End for
13.if ch<-2 then Read m
14.for(i<-0;i,m;i++)
15.Read aname,tit
16.for(j<-0;j<n;j++)
17.Compare aname,name & tit ,title
18.if yes,check sp>0
19.amount <-amount+price
20.sp<-sp-1
21.ts<-ts+1
22.End for
23.End for
24.if ch<-3,then Read na & ti
25.for(i<-0;i,n;i++)
26.Compare Nmae ,na &ti,title
27.if yes Call uprice()
28.End for
29.if ch<-4 display ts
30.stop
Function:Read()
1. Start
2. Read aname,title,pub,price,sp
3. stop
Function:search()
1. Start
2. display aname,title,pub,price,sp
3. stop

63

13.507 Object Oriented Programming Lab

Date :

Experiment No: 19
BOOKSHOP

Aim
Design a class bookshop with data members author, title, publishers
and stock positions. Write a program to perform the following
operations. 1)Serach , 2) Display the total amount, 3) Upadate the
price ,4) Record the transactions.
Program
# include<iostream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
class book
{
public:
char name[20],title[20],pub[20];
int sp,price;
void read()
{
cin>>name;
cin>>title;
cin>>pub;
cin>>price;
cin>>sp;
}
void search()
{
cout<<"Author \t Title \t Publisher \t Price \t Stock
\n";
cout<<name<<"\t"<<title<<"\t"<<pub<<"\t"<<price<<"\t"<<sp
<<"\n";
}
void uprice()
{
cout<<"\nCurrent Price"<<price;
cout<<"\nEnter new price";
int p;
cin>>p;
price=p;
cout<<"\nPrice of"<<title<<":"<<price;
}
};
int main()
13.507 Object Oriented Programming Lab

64

Function:price()
1. Start
2. display price
3. Read p
4. price<-p
5. display price
6. Stop
Output
enter the total no of books :1
enter book details :

Author

Title

Publisher

Price

Morris

CHD

Pearson

300

Stock Position

10

1.search
2.purchase
3.update price
4.transactions
5.exit
enter your choice :1
enter author name :Morris
enter title of book : CHD
Author

Title

Publisher

Price

Morris

CHD

Pearson

300

65

13.507 Object Oriented Programming Lab

Stock Position

10

{
book b[50];
clrscr();
cout<<"\nEnter the total no of books";
int n,ts=0;
cin>>n;
cout<<"\nEnter book details";
cout<<"\n\nAuthor\tTitle\tPublisher\tPrice\tStockPosition
\n";
for(int i=0;i<n;i++)
b[i].read();
cout<<"\n1.Search";
cout<<"\n2.Purchase";
cout<<"\n3.Update Price";
cout<<"\n4.Transcations";
cout<<"\n5.Exit";
for(;;)
{
cout<<"\n\nEnter your choice";
int ch;
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter author name";
char nam[20],tit[20];
cin>>nam;
cout<<"\nEnter Title of book";
cin>>tit;
for(i=0;i<n;i++)
{
if((strcmp(nam,b[i].name)&&strcmp(tit,b[i].title))==0
)
b[i].search();
else
cout<<"\nNot found";
}
break;
case 2:
cout<<"\nEnter the no. of books u wish 2 buy";
int m;
int amount=0;
cin>>m;
for(i=0;i<m;i++)
{
cout<<"\nEnter author name";
13.507 Object Oriented Programming Lab

66

67

13.507 Object Oriented Programming Lab

cin>>nam;
cout<<"\nEnter Title of books";
cin>>tit;
for(int j=0;j<n;j++)
{
if((strcmp(nam,b[j].name)&&strcmp(tit,b[j].title))==0
)
{
if(b[j].sp==0)
{
cout<<"\nStock empty";
}
else
{
cout<<"Price"<<b[j].price;
amount=amount+b[j].price;
b[j].sp--;
ts++;
}
}
else
cout<<"\nno such book available";
}
cout<<"\nTotal price:"<<amount;
break;
case 3:
cout<<"\nEnter author name";
char na[20],ti[20];
cin>>na;
cout<<"\nEnter Title of book";
cin>>ti;
for(i=0;i<n;i++)
{
if((strcmp(na,b[i].name)&&strcmp(ti,b[i].title))==0)
b[i].uprice();
else
{
cout<<"\nNo such book available";
}
}
break;
case 4:
cout<<"No transaction made:"<<ts;
break;
case 5:
exit(0);
13.507 Object Oriented Programming Lab

68

69

13.507 Object Oriented Programming Lab

default:
break;
}
}
getch();
return 0;
}
}
Result
The program was successfully compiled and run to get the desired output.

13.507 Object Oriented Programming Lab

70

Algorithm
1. Start
2. Creating a class A with a as its private datamember and setvalue(),getvalue()
and show() as public members
3. Creating a class B which is publically derived from class A with c as its private
datamember and mul(),display() as its public member
4. Creating an object of class B as b1
5. Calling function b1.setvalue()
6. Calling function b1.mul()
7. Calling function b1.show()
8. Calling function b1.display()
9. Assign 20 to b1.b
10.Calling function b1.mul()
11.Calling function b1.display()
12.Stop
Function:setvalue()
1.
2.
3.
4.

Start
Assign 5 to a
Assign 10 to b
Stop

Function:getvalue()
1. Start
2. Return value of a
3. Stop
Function:show()
1. Start
2. Display value of a
3. Stop
Function:mul()
1. Start
2. Find product of b and return value of getvalue() function to c
3. Stop

71

13.507 Object Oriented Programming Lab

Date :

Experiment No: 20
SINGLE INHERITANCE

Aim
A program to implement single inheritance
Program
#include<iostream.h>
#include<conio.h>
class A
{
int a;
public:
int b;
void setvalue();
int getvalue(void) ;
void show(void);
};
class B:public A
{
int c;
public:
void mul(void);
void display(void);
};
void A::setvalue(void)
{
a=5;
b=10;
}
int A::getvalue()
{
return a;
}
void A::show()
{
cout<<"a="<<a<<"\n";
}
void B::mul(void)
{
c=b*getvalue();
}
void B::display()
{
cout<<"a="<<getvalue()<<"\n";
13.507 Object Oriented Programming Lab

72

Function:display()
1.
2.
3.
4.
5.

Start
Display value of a
Display value of b
Display value of c
Stop

Output
a=5
a=5
b=10
c=50
a=5
b=20
c=100

73

13.507 Object Oriented Programming Lab

cout<<"b="<<b<<"\n";
cout<<"c="<<c<<"\n";
}
int main()
{
clrscr();
B b1;
b1.setvalue();
b1.mul();
b1.show();
b1.display();
b1.b=20;
b1.mul();
b1.display();
getch();
return 0;
}
Result
The program was successfully compiled and run to get the desired output.

13.507 Object Oriented Programming Lab

74

Algorithm
1. Start
2. Creating a class M with m as its protected member and getm() as its public
member
3. Creating a class N with n as its protected member and getn() as its public
member
4. Creating a class P which is publically derived from class M and N with
display() as its public member
5. Creating object of the class P as p
6. Calling function p.getm(5)
7. Calling function p.getn(8)
8. Calling function display()
9. Stop
Function:getm()
1. Start
2. Assign value of a to m
3. Stop
Function:getn()
1. Start
2. Assign value of b to n
3. Stop
Function:display()
1.
2.
3.
4.
5.

Start
Display value of m
Display value of n
Display value of m*n
Stop

Output
m=5
n=8
m*n=40

75

13.507 Object Oriented Programming Lab

Date :

Experiment No: 21
MULTIPLE INHERITANCE

Aim
A program to implement multiple inheritance
Program
# include<iostream.h>
#include<conio.h>
class M
{
protected:
int m;
public:
void getm(int a)
{
m=a;
}
};
class N
{
protected:
int n;
public:
void getn(int b)
{
n=b;
}
};
class P:public M,public N
{
public:
void display();
};
void P::display()
{
cout<<"m="<<m<<"\n";
cout<<"n="<<n<<"\n";
cout<<"m*n="<<m*n<<"\n";
}
int main()
{
clrscr();
P p;
13.507 Object Oriented Programming Lab

76

77

13.507 Object Oriented Programming Lab

p.getm(5);
p.getn(8);
p.display();
getch();
return 0;
}
Result
The program was successfully compiled and run to get the desired output.

13.507 Object Oriented Programming Lab

78

Algorithm
1. Start.
2. Create a class student with roll_number as protected member and
get_number(),put_number as public methods.
3. Create a class test which is publically inherited from class student having sub1
, sub2 as protected members and get_mark(),put_mark() as public methods.
4. Create a class result which is publically inherited from class test having total as
public member and display() as public method.
5. Create an object student1 for class student.
6. Call function student1.get_number(11)
7. Call function student1.get_marks(75, 59.5).
8. Call function student.display().
9. Stop.
Function get_number()
1.
2.
3.
4.

Start.
Pass an integer value a as formal parameter.
Assign a to the variable roll_number.
Stop.

Function put_number()
1. Start.
2. Display roll_number.
3. Stop.
Function get_marks
1.
2.
3.
4.

Start.
Pass float type values x,y as formal parameters.
Assign x to sub1 and y to sub2.
Stop.

Function put_marks
1. Start.
2. Display sub1 and sub2.
3. Stop.

79

13.507 Object Oriented Programming Lab

Date :

Experiment No: 22
MULTILEVEL INHERITANCE

Aim
A program to implement multilevel inheritance
Program
# include<iostream.h>
#include<conio.h>
class Student
{
protected:
int rollno;
public:
void get_rollno(int a)
{
rollno=a;
}
void put_rollno(void)
{
cout<<"Rollno:"<<rollno<<"\n";
}
};
class test:public Student
{
protected:
int m1,m2,m3;
public:
void get_marks(int x,int y)
{
m1=x;
m2=y;
}
void put_marks(void)
{
cout<<"mark1="<<m1<<"\n";
cout<<"mark2="<<m2<<"\n";
}
};
class total:public test
{
int total;
public:
void display(void);
};
13.507 Object Oriented Programming Lab

80

display()
1.
2.
3.
4.
5.
6.

Start.
Assign the sum of sub1 and sub2 to total.
Call function put_number().
Call function put_marks().
Display total.
Stop.

Output
Roll Number :11.
Mark in sub1:75.
Mark in sub2 :59.5
Total =134.5

81

13.507 Object Oriented Programming Lab

void total::display(void)
{
total=m1+m2;
put_rollno();
put_marks();
cout<<"Total="<<total<<"\n";
}
int main()
{
clrscr();
total t;
t.get_rollno(20);
t.get_marks(40,35);
t.display();
getch();
return 0;
}
Result
The program was successfully compiled and run to get the desired output Function

13.507 Object Oriented Programming Lab

82

Algorithm
1. Start
2. Create a class student with id,name as its protected members and get(),show()
as its public members
3. Create class exam which is publically derived from class student.It has
sm1,m2,m3 as its protected members and getm(), showm() as its public
members
4. Create a class sports with wt as its protected members and getw() and
showm() as its public members
5. Create a class result which is publically derived from class exam and class
sports.It has tot as its private members and sum(),showr() as its public
members.
6. Read n
7. for(i<- 0;i<n;i++)
8. r[i].get()
9. r[i].getm()
10.r[i].getw()
11.r[i].sum()
12.End for
13.for(i<-0;i<n;i++)
14.r[i].show()
15.r[i].showm()
16.r[i].showw()
17.r[i].showr()
18.End for
19.Stop
Function :get()
1. Start
2. Read id,name
3. Stop
Function :show()
1. Start
2. Display id,name
3. Stop
Function :getm()
1. Start
83

13.507 Object Oriented Programming Lab

Date :

Experiment No: 23
STUDENT DETAILS USING INHERITANCE

Aim
A program to implement student details using inheritance
Program
# include<iostream.h>
#include<conio.h>
class student
{
protected:
int id;
char name[20];
public:
void get()
{
cin>>id>>name;
}
void show ()
{
cout<<id<<"\t"<<name;
}
};
class exam:public student
{
protected:
int m1,m2,m3;
public:
void get_m()
{
cin>>m1>>m2>>m3;
}
void show_m()
{
cout<<"\t"<<m1<<"\t"<<m2<<"\t"<<m3;
}
};
class sports
{
protected:
int wt;
public:
void get_wt()
{
13.507 Object Oriented Programming Lab

84

2. Display m1,m2,m3
3. Stop
Function :show()
1. Start
2. Display wt
3. Stop
Function :sum()
1. Start
2. tot<-m1+m2+m3+wt
3. Stop
Function :showr()
1. Start
2. Display tot
3. Stop
Output :
Enter the no of students
2
Enter the details of students

ID

NAME

Asha

Arya

m1

m2

m3

wt

56

34

23

54

36

45

65

78

m2

m3

wt

STUDENT DETAILS

ID

NAME

Asha

56

34

23

54

Arya

36

45

65

78

85

m1

13.507 Object Oriented Programming Lab

cin>>wt;
}
void show_w()
{
cout<<"\t"<<wt;
}
};
class result:public exam ,public sports
{
int tot;
public:
void sum() { tot=m1+m2+m3+wt; }
void show_r ()
{
cout<<"\t"<<tot;
}
};
int main(){
clrscr();
int n;
result r[50];
cout<<"\n enter the no of student:";
cin>>n;
cout<<"\n entre details of"<<n<<"students";
cout<<"\n id name m1 m2 m3 wt\n";
for(int i=0;i<n;i++)
{
r[i].get();
r[i].get_m();
r[i].get_wt();
r[i].sum();
cout<<"\n";
}
cout<<"\n .. student details..\n";
cout<<"\n id\t name\tm1\tm2\tm3\twt\ttotal\n" ;
for(i=0;i<n;i++){
r[i].show();
r[i].show_m();
r[i].show_w();
r[i].show_r();
cout<<"\n";
}
getch();
return(0);
}
Result
The program was successfully compiled and run to get the desired output.
13.507 Object Oriented Programming Lab

86

Algorithm
1. Start
2. Create a class bank with name, actype, amount, bal, accno,
get(),deposit(),withdraw(),display() as its public members.
3. Create a class current which is publically inherited from bank.
4. create a class saving which is publically inherited from bank and it has
interact() as it's public member.
5. From menu Read any option,ch if ch<-1,it is saving account.
6. Read option ch1.
7. If ch<-1,Read n,Call get()
8. If ch<-2,Read an
9. Check an==accno,if yes
10.Call deposit()
11.If ch<-3,Read ann
12.Check an==accno,if yes
13.Call withdraw()
14.If ch1<-4,Read ano
15.Check an==accno,if yes
16.Call display()
17.If ch1<-5,Read ano
18.Check ano=accno,if yes
19.call intrest()
20.If ch<-2, it is current account
21.Read option ch2
22.If ch<-1,repeat step 7
23.If ch<-2,repeat step 8 to 10
24.If ch<-3,repeat step 11 to 13
25.If ch<-4,repeat step 14to 16
26.otherwise exit
27.Stop
Function :get ()
1.
2.
3.
4.

Start
Read name,accno,amount,type.
bal<-bal+amount
Stop

Function:deposit ()
1. Start
87

13.507 Object Oriented Programming Lab

Date :

Experiment No: 24
BANK INHERITANCE

Aim
A program to create a class for bank account that stores name, accno,
type. Derive subclasses current accounts & savings account &
include necessary member functions to perform the necessary
operations.
Program
##include<iostream.h>
#include<conio.h>
#include<process.h>
class bank
{
public:
char name[20];
char acctype[20];
double amount;
double bal;
long accno;
void get()
{
cout<<"\n Enter name\n";
cin>>name;
cout<<"Enter accont no";
cin>>accno;
cout<<"\n Enter amount deposited\n" ;
cin>>amount;
cout<<"\n Enter acconut type \n";
cin>>acctype;
bal=0;
bal=bal+amount;
}
void deposit()
{
double dep;
cout<<"Enter the amount\n";
cin>>dep;
if(bal>500)
bal=bal+dep;
else
bal=bal+(dep-50);
}
void withdraw()
13.507 Object Oriented Programming Lab

88

2.
3.
4.
5.
6.
7.
8.

Read dep
if(bal>500)
bal<-bal+dep
else
bal<-bal+(dep-50)
End if
Stop

Function: withdraw ()
1. Start
2. Read wd
3. if (bal-wd)>0)
4. bal<-bal-wd
5. else
6. Display "Insufficient balance"
7. End if
8. Stop
9. Function :display ()
10.Start
11.Display name,actype,bal
12.Stop
Function :interest ()
1.
2.
3.
4.
5.

Start
Read month
inter<-0.5*bal*month
bal<-bal+inter
Stop

Output
1. Savings account
2. Current account
3. Exit
Enter your choice
1

89

13.507 Object Oriented Programming Lab

{
double wd;
cout<<"Enter the amount\n";
cin>>wd;
if((bal-wd)>0)
bal=bal-wd;
else
cout<<"Insufficient balance\n";
}
void display()
{
cout<<"\n Name:"<<name;
cout<<"\nAccount type:"<<acctype;
cout<<"\n Balance:"<<bal;
}
}b[100];
class current:public bank
{
}c[100];
class saving:public bank
{
public:
void interest()
{
int month;
float inter;
cout<<"\nEnter the no:of months\n";
cin>>month;
inter=0.5*bal*month;
bal=bal+inter;
}
};
int main()
{
int n,ch,ch1,ch2;
clrscr();
saving s[100];
a:for(;;)
{
cout<<"\n1.Saving account";
cout<<"\n2.Current account";
cout<<"\n3.Exit";
cout<<"\n\nEnter your choice\n";
cin>>ch;
switch(ch)
{
13.507 Object Oriented Programming Lab

90

1.
2.
3.
4.
5.
6.

91

Create
Deposite
Withdraw
Balance
Interest
Exit
Enter your choice
1
Enter the no: of customers 1
Enter name
ram
Enter account number
3456
Enter amount deposited
2000
Enter account type
savings

13.507 Object Oriented Programming Lab

case 1:for(;;)
{
cout<<"\n1.Create";
cout<<"\n2.Deposite";
cout<<"\n3.Withdraw\n4.Balance\n5.Interest\n6.exit";
cout<<"\nEnter ur choice";
cin>>ch1;
switch(ch1)
{
case 1:cout<<"\n Enter the no. of customers";
cin>>n;
for(int i=0;i<n;i++)
s[i].get();
break;
case 2:long an;
cout<<"\n Enter account no:";
cin>>an;
for( i=0;i<n;i++)
{
if(an==s[i].accno)
{
s[i].deposit();
}
else
{
cout<<"\n..Invalid account no..";
}
}
break;
case 3:long ann;
cout<<"\nEnter account number ";
cin>>ann;
for(i=0;i<n;i++)
{
if(ann==s[i].accno)
{
s[i].withdraw();
}
else
cout<<"\n..Invalid account no..";
}
break;
case 4 :long ano;
cout<<"\nEnter account number ";
cin>>ano;
13.507 Object Oriented Programming Lab

92

93

13.507 Object Oriented Programming Lab

for(i=0;i<n;i++)
{
if(ano==s[i].accno)
{
s[i].display();
}
else
cout<<"\n..Invalid account no..";
}
break;
case 5:long anoo;
cout<<"\nEnter account number ";
cin>>anoo;
for(i=0;i<n;i++)
{
if(anoo==s[i].accno)
{
s[i].interest();
}
else
cout<<"\n..Invalid account no..";
}
break;
default:goto a;
}
}
break;
case 2:for(;;)
{
cout<<"\n1.Create";
cout<<"\n2.Deposite";
cout<<"\n3.Withdraw\n4.Balance\n5.Interest\n6.exit";
cout<<"\nEnter ur choice";
cin>>ch2;
switch(ch2)
{
case 1:cout<<"\n Enter the no. of customers";
cin>>n;
for(int i=0;i<n;i++)
c[i].get();
break;
case 2:long an;
cout<<"\n Enter account no:";
cin>>an;
for( i=0;i<n;i++)
13.507 Object Oriented Programming Lab

94

95

13.507 Object Oriented Programming Lab

{
if(an==c[i].accno)
{
c[i].deposit();
}
else
{
cout<<"\n..Invalid account no..";
}
}
break;
case 3:long ann;
cout<<"\nEnter account number ";
cin>>ann;
for(i=0;i<n;i++)
{
if(ann==s[i].accno)
{
c[i].withdraw();
}
else
cout<<"\n..Invalid account no..";
}
break;
case 4 :long ano;
cout<<"\nEnter account number ";
cin>>ano;
for(i=0;i<n;i++)
{
if(ano==c[i].accno)
{
c[i].display();
}
else
cout<<"\n..Invalid account no..";
}
break;
default:goto a;
}
}
break;
case 3:exit(0);
default:break;
}
}
getch();
13.507 Object Oriented Programming Lab

96

97

13.507 Object Oriented Programming Lab

return 0;
}
Result
The program was successfully compiled and run to get the desired output.

13.507 Object Oriented Programming Lab

98

Algorithm
1. Start
2. Create a class shape with r,h,l,b,br as protected members and show() as it's
public virtual function
3. Create a class cir which is publically derived from shape and it has show () as
it's public member
4. Create a class rect which is publically derived from shape and it has show ()
as it's public member
5. Create a class tir which is publically derived from shape and it has show () as
it's public member
6. Create a pointer sptr of base class shape
7. Using sptr call the function show ()of each class
8. Stop
Function :virtual show()
1. Start
2. Display base class shape
3. Stop
Function :show () of class cir
1. Start
2. Read r
3. ar<- 3.14*r*r
4. Display ar
5. Stop
6. Function :show () of class rect
7. Start
8. Read l,b
9. ar<-l*b
10.Display ar
11.Stop
Function:show () of class tri
1.
2.
3.
4.
5.

Start
Read br,h
ar<-0.5*br*h
Display ar
Stop

99

13.507 Object Oriented Programming Lab

Date :

Experiment No: 25
SHAPE VIRTUAL FUNCTION

Aim
A program to implement shape class using virtual function.
Program
# include<iostream.h>
#include<conio.h>
class shape
{
protected:
float r;
int l,b,br,h;
public:
virtual void show()
{
cout<<"Base class Shape";
}
};
class cir:public shape
{
public:
void show()
{
cout<<"\nenter the radious of circle\n";
cin>>r;
float ar=3.14*r*r;
cout<<"area of circle:\n " <<ar;
}
};
class rect : public shape
{
public:
void show()
{
cout<<"\nenter the length and breadth\n";
cin>>l>>b;
int ar=l*b;
cout<<"area of rectangcle:\n " <<ar;
}
};
class tri:public shape
{
public:
13.507 Object Oriented Programming Lab

100

Output
Base class shape
Enter the radius of circle
1
Area of circle: 3.14
Enter the length and breadth
2 3
Area of rectangle:6
Enter height and breadth of triangle
4 5
Area of triangle: 10

101

13.507 Object Oriented Programming Lab

void show()
{
cout<<"\nenter the height and breadth of triangcle\n";
cin>>h>>br;
float ar=0.5*h*br;
cout<<"area of triangle: " <<ar;
}
};
int main()
{
clrscr();
shape *sptr;
shape s;
rect r;
cir c;
tri t;
sptr=&s;
sptr->show();
sptr=&c;
sptr->show();
sptr=&r;
sptr->show();
sptr=&t;
sptr->show();
getch();
return (0);
}
Result
The program was successfully compiled and run to get the desired output.

13.507 Object Oriented Programming Lab

102

Algorithm
1. Start
2. Define a constant size with value 3
3. Create a class vector with a type t vector as private data member, a default
constructor , a parametrized constructor & member function operator*() and
display()
4. Define an array x[] with elements 1,2,3
5. Define an array y[] with elements 4,5,6
6. Create v1,v2 as the objects of class vector
7. Assign v1 as x and v2 as y
8. Display v1 by calling function v1.display()
9. Display v2 by calling function v2.display()
10.Display the result of v1* v2
11.Stop

Function:operator*()
1. Pass an vector object y as the formal parameter
2. Assign result as 0
3. for(i<-0;i<size;i++)
4. Assign reault as the sum of result and the product of this->v[i] and y.v[i]
5. End for
6. Return the value result
7. Stop
Function:display()
1. Start
2. for(i<-0;i<size;i++)
3. Display the value v[i]
4. End for
5. Stop

Output
V1
V2
V1

=
=
X

103

1 2 3
4 5 6
V2 = 32

13.507 Object Oriented Programming Lab

Date :

Experiment No: 26
CLASS TEMPLATE

Aim
A program to implement class template.
Program
# include<iostream.h>
#include<conio.h>
const size=3;
template<class T>
class vector
{
int i;
T *V;
public:
vector()
{
V=new T[size];
for(i=0;i<size;i++)
V[i]=0;
}
vector(T* a)
{
for(i=0;i<size;i++)
V[i]=a[i];
}
T operator*(vector &y)
{
T sum=0;
for(i=0;i<size;i++)
sum +=this->V[i] * y.V[i];
return sum;
}
void display(void)
{
for(i=0;i<size;i++)
cout<<V[i]<<"\t";
cout<<"\n";
}
};
int main()
{
clrscr();
int x[3]={1,2,3};
13.507 Object Oriented Programming Lab

104

105

13.507 Object Oriented Programming Lab

int y[3]={4,5,6};
vector<int>v1;
vector<int>v2;
v1=x;
v2=y;
cout<<"v1=";
v1.display();
cout<<"v2=";
v2.display();
int r=v1*v2;
cout<<"v1 * v2="<<r;
getch();
return 0;
}
Result
The program was successfully compiled and run to get the desired output.

13.507 Object Oriented Programming Lab

106

Algorithm
1. Start
2. Call function fun( 100,200,11.22,33.44)
3. Stop

Function:fun(int m,int n,float a,float b)


1.
2.
3.
4.
5.
6.
7.

Start
Display m,n
Call swap(m,n)
Display a,b
Call swap(a,b)
Display a,b
Stop

Function:swap(T &x,T &y)


1.
2.
3.
4.
5.
6.

Start
Swap is a template function
Assign value of x to temp
Assign value of y to x
Assign value of temp to y
Stop

Output
m and n before swap

:100

200

m and n after swap

:200

100

a and b before swap

:11.22

a and b after swap

:33.439999

107

13.507 Object Oriented Programming Lab

33.439999
11.22

Date :

Experiment No: 27
SWAP USING TEMPLATE

Aim
A program to implement swap operation using template.
Program
# include<iostream.h>
#include<conio.h>
template <class T>
void swap (T &x,T &y)
{
T temp=x;
x=y;
y=temp;
}
void fun(int m,int n,float a,float b)
{
cout<<"m and n before swap:"<< m << " " << n <<"\n";
swap(m,n);
cout<<"m and n after swap:"<< m << " " << n <<"\n";
cout<<"a and b before swap:"<< a << " "<< b <<"\n";
swap(a,b);
cout<<"a and b after swap:"<< a << " "<< b <<"\n";
}
int main()
{
clrscr();
fun(100,200,11.22,33.44);
getch();
return 0;
}
Result
The program was successfully compiled and run to get the desired output.

13.507 Object Oriented Programming Lab

108

Algorithm
1. Start
2. Create a class inventory with name,cost,code,readdata(),writedata() as its
members.
3. Open the file and read the details of stock.
4. Initialize i as 0 ,call the function readdata() ,write the file using the function
write()
5. Repeat the step 3 and 4 untill I less than 3.
6. Display the output.
7. Intialize I as 0.Display item and size of item.
8. Call the function writedata() then repeat step 7 and 8 untill I less than 3.
9. Close the file using close().
10.Stop.
Function:readdata()
1. Start.
2. Read name,code,cost.
3. Stop
Function:writedata()
1. Start.
2. Display name, code,cost.
3. Stop.

Output
Enter the details of stock
Enter name, code and cost
Pen 101 5
Enter name, code and cost
ink 102 20
Enter name, code and cost
Pencil 103 4

109

13.507 Object Oriented Programming Lab

Date :

Experiment No: 28
FILE USING CLASS

Aim
Program to read and write a file using class
Program
# include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
class inventory
{
char name[10];
int cost,code;
public:
void readdata()
{
cout<<"\n enter name,code and cost\n";
cin>>name>>code>>cost;
}
void writedata()
{
cout<<setw(10)<<name;
cout<<setw(10)<<code;
cout<<setw(10)<<cost;
}
};
int main()
{
inventory item[3];
fstream f;
f.open("stock",ios::in|ios::out);
cout<<"\n enter the details of stock";
for(int i=0;i<3;i++)
{
item[i].readdata();
f.write((char *)&item[i],sizeof(item[i]));
}
f.seekg(0);
cout<<"\n output";
for(i=0;i<3;i++)
{
f.read((char *)&item[i],sizeof(item[i]));
13.507 Object Oriented Programming Lab

110

output
Pen 101
Ink 102
Pencil 103

111

5
20
4

13.507 Object Oriented Programming Lab

item[i].writedata();
}
f.close();
return 0;
}Result
The program was successfully compiled and run to get the desired output.

13.507 Object Oriented Programming Lab

112

113

13.507 Object Oriented Programming Lab

Você também pode gostar