Upload
sreehari-b-s
View
235
Download
0
Embed Size (px)
Citation preview
8/18/2019 OOT OR CPP OR C++ LAB RECORD
1/114
VALIA KOONAMBAIKULATHAMMA
COLLEGE OF ENGINEERING & TECHNOLOGYChavarcode, Parippally P.O, Trivandrum, Kerala.
Ph: no: 0470 - 2665223, Email:[email protected], Website: www.vkcet.com
( Governed by Valiakoonambaikulam Sree Bhadrakali Kshethra Trust
Vadakkevila P O,Kollam-691010, Kerala )
13.507 Object Oriented Programming LabLABORATORY RECORD
Name :………………………………………………………………………………………………..
Class No : ……………………………………Semester & Branch………………………....
Reg. No. University Exam :…………………………………………………………………..
CERTIFIED THAT THIS IS A BONAFIDE RECORD OFTHE WORK DONE IN THE LABORATARY OF
VKCET,Chavarcode,Parippally
Head of the Department Staff-in-charge
…………………..... …………………...................
Submitted for the Practical Examination in
…………………………………………………………………….. held on ………………………..
Internal Examiner External Examiner
…………………......... ………………….........
http://www.vkcet.com/http://www.vkcet.com/http://www.vkcet.com/http://www.vkcet.com/
8/18/2019 OOT OR CPP OR C++ LAB RECORD
2/114
1 13.507 Object Oriented Programming Lab
8/18/2019 OOT OR CPP OR C++ LAB RECORD
3/114
13.507 Object Oriented Programming Lab 2
CONTENTS
Sl. No
Date Name of the ExperimentPage No.
Signature ofstaff-in-
charge
1 Swapping 8
2 Area of shapes 12
3 Power of a number 16
4 Factorial of a number 18
5 Student Details 20
6 Bank account details 24
7 Static member function 30
8 Object as arguments 32
9 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
8/18/2019 OOT OR CPP OR C++ LAB RECORD
4/114
8/18/2019 OOT OR CPP OR C++ LAB RECORD
5/114
8/18/2019 OOT OR CPP OR C++ LAB RECORD
6/114
5 13.507 Object Oriented Programming Lab
8/18/2019 OOT OR CPP OR C++ LAB RECORD
7/114
13.507 Object Oriented Programming Lab 6
8/18/2019 OOT OR CPP OR C++ LAB RECORD
8/114
7 13.507 Object Oriented Programming Lab
Algorithm
1. Start
2. Create a class Swap with a,b as private data member and function cvalue with2 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 & b5. 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. Start2. assign x1 to t1,y1 to x1 and t1 to y1
3. Stop
8/18/2019 OOT OR CPP OR C++ LAB RECORD
9/114
13.507 Object Oriented Programming Lab 8
Experiment No: 1
SWAPPING
Aim
Program to perform swap operation using call by value, call by
reference, call by address.
Program #include#includeclass Swap{
public:int a,b;void cvalue(int x1,int y1)
{int t1;t1=x1;x1=y1;y1=t1;cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
10/114
9 13.507 Object Oriented Programming Lab
Output
ENTER TWO NUMBERS :5 7
CALL BY VALUE
AFTER SWAP A=7 B=5
ENTER TWO NUMBERS :9 2
CALL BY REFERENCE
AFTER SWAP A=2 B=9
ENTER TWO NUMBERS :16 8
CALL BY ADDRESS
AFTER SWAP A=8 B=16
8/18/2019 OOT OR CPP OR C++ LAB RECORD
11/114
13.507 Object Oriented Programming Lab 10
int main(){
clrscr();int a,b;Swap s;couta>>b;cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
12/114
11 13.507 Object Oriented Programming Lab
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
parameter3. Create s as the object of class Shape
4. Read the radius of the circle r5. 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. Start
2. Pass a floating value r as the formal parameter
3. area
8/18/2019 OOT OR CPP OR C++ LAB RECORD
13/114
8/18/2019 OOT OR CPP OR C++ LAB RECORD
14/114
13 13.507 Object Oriented Programming Lab
Function:area()
1. Start
2. Pass a integer value a as the formal parameter
3. area
8/18/2019 OOT OR CPP OR C++ LAB RECORD
15/114
13.507 Object Oriented Programming Lab 14
cin>>br>>h;couta;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.
8/18/2019 OOT OR CPP OR C++ LAB RECORD
16/114
15 13.507 Object Oriented Programming Lab
Algorithm
1. Start
2. Create a class Power with powe() as public member function3. Create p as the object of class Power
4. Call function p.powe() with no arguments
5. Call function p.powe() with 1 integer value as parameter6. Call function p.powe() with 2 integer value as parameters
7. Stop8.
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 parameter4. Display power of number
5. Stop
Output
POWER IS 1
POWER IS 2
POWER IS 9
8/18/2019 OOT OR CPP OR C++ LAB RECORD
17/114
13.507 Object Oriented Programming Lab 16
Experiment No: 3
POWER OF A NUMBER
Aim
To implement a program to find the power of a number.
Program #include#include#includeclass power{
public:void powe(int x=1,int y=1){
int pw=pow(x,y);cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
18/114
17 13.507 Object Oriented Programming Lab
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 operator5. stop
Function:getdata()
1. Start
2. read the number and stores it in n
3. stop
Function:fact()
1. Start
2. set f as 1
3. intialize i as n
4. repeat steps 5-6 till i is greater than zero
5. f
8/18/2019 OOT OR CPP OR C++ LAB RECORD
19/114
8/18/2019 OOT OR CPP OR C++ LAB RECORD
20/114
19 13.507 Object Oriented Programming Lab
Algorithm
1. Start
2. Create a class Student with rno,name,gr,m1,m2,t,avg,g as private data memberand 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 n4. initialise i as zero
5. Repeat steps 6-10 till i less than n6. 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.
Start2. t
8/18/2019 OOT OR CPP OR C++ LAB RECORD
21/114
13.507 Object Oriented Programming Lab 20
Experiment No: 5
STUDENT DETAILS
Aim
To implement a program to display details of students in a class.
Program #include#includeclass Student{
int rno,m1,m2,t,avg,g;char name[20],gr;public:void get()
{coutname;coutrno;coutm1>>m2;
}void tot(){
t=m1+m2;avg=t/2;g=avg/10;
}void grade();
void show(){
cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
22/114
21 13.507 Object Oriented Programming Lab
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 98
RNO NAME MARK1 MARK2 TOTAL GRADE
28 ANU 88 92 180 A
14 MANU 100 98 198 A
8/18/2019 OOT OR CPP OR C++ LAB RECORD
23/114
13.507 Object Oriented Programming Lab 22
case 6:gr='D'; break;case 5:gr='E'; break;default:gr='F'; break;
}}
int main(){
clrscr();Student s[10];int n;coutn;
for(int i=0;i
8/18/2019 OOT OR CPP OR C++ LAB RECORD
24/114
23 13.507 Object Oriented Programming Lab
Algorithm
1. Start
2. Create a class Bank with name,actype,amount and bal as private datra membersand public member functions get(),deposit(),withdraw(),display()
3. Create b as the object of class bank
4. set ch as 15. 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 914. 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-2823. 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 program30. Stop
Function:get()
1. Start
2. Read the name,account number,amount deposited,account type
3. set balance as zero
4. add balance amount with amount deposited and saves it in balance
5. Stop
8/18/2019 OOT OR CPP OR C++ LAB RECORD
25/114
13.507 Object Oriented Programming Lab 24
Experiment No: 6
BANK ACCOUNT DETAILS
Aim
To implement a program for display details of n customers in a Bank.
Program #include#includeclass Bank{
char name[15],actype[25];double amount,bal;public:long accno;
void get(){
coutname;coutaccno;coutactype;coutamount;bal=0;bal=bal+amount;
}
void deposit(){
double dep;
coutdep;bal=bal+dep;
}void withdraw(){
double wd;coutwd;
bal=bal-wd;}
Date :
8/18/2019 OOT OR CPP OR C++ LAB RECORD
26/114
25 13.507 Object Oriented Programming Lab
Function:deposit()
1. Start
2. Read the amount to deposit
3. add amount to balance
4. Stop
Function:withdraw()
1. Start
2. Read the amount to withdraw3. subtract amount from balance
4. Stop
Function:display()
1. Start2. 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 4.EXIT
ENTER YOUR CHOICE:1
ENTER ACCOUNT NUMBER:134300214
ENTER AMOUNT:10000
PRESS 1 TO CONTINUE ELSE PRINT 0:1
8/18/2019 OOT OR CPP OR C++ LAB RECORD
27/114
13.507 Object Oriented Programming Lab 26
void display(){
cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
28/114
8/18/2019 OOT OR CPP OR C++ LAB RECORD
29/114
13.507 Object Oriented Programming Lab 28
for(i=0;i
8/18/2019 OOT OR CPP OR C++ LAB RECORD
30/114
29 13.507 Object Oriented Programming Lab
Algorithm
1. Start2. 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. Start2. Display code
3. Stop
Function:showcount()
1. Start
2. Display count
3. Stop
OutputCOUNT:1COUNT:2COUNT:3
OBJECT NUMBER:1
OBJECT NUMBER:2
OBJECT NUMBER:3
8/18/2019 OOT OR CPP OR C++ LAB RECORD
31/114
13.507 Object Oriented Programming Lab 30
Experiment No: 7
STATIC MEMBER FUNCTION
Aim
A program to display count of number of objects created using static
member function.
Program #include#includeclass test{
int code;static int count;public:void setcode(void){
code=++count;}void showcode(void){
cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
32/114
31 13.507 Object Oriented Programming Lab
Algorithm 1. Start2. 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 values6. 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. Start
2. Pass 2 integer variables h and m as formal parameter3. Assign h to hours and m to minutes
4. Stop
Function:puttime()
1. Start
2. Display hours and minutes3. Stop
Function:sum()
1. Start
2. Pass objects t1,t2 of Time as formal parameter
3. minutes
8/18/2019 OOT OR CPP OR C++ LAB RECORD
33/114
13.507 Object Oriented Programming Lab 32
Experiment No: 8
OBJECT AS ARGUMENTS
Aim
A program to implement the use of Object as function arguments.
Program #include#includeclass time{
int hours,minutes;public:void gettime(int h,int m){
hours=h;
minutes=m;}void puttime(void){
cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
34/114
33 13.507 Object Oriented Programming Lab
Algorithm 1. Start2. 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 number3. Stop
Function:sum()
1. Start
2. Pass 2 objects A & B of class Complex
3. Create C as the object of Complex4. C.x
8/18/2019 OOT OR CPP OR C++ LAB RECORD
35/114
13.507 Object Oriented Programming Lab 34
Experiment No: 9
SUM & DIFFERENCE OF COMPLEX NUMBERS
Aim
A program to find the sum and difference of two complex numbers.
Program #include#includeclass complex{
int x,y,a,b;public:void get(){
cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
36/114
35 13.507 Object Oriented Programming Lab
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
8/18/2019 OOT OR CPP OR C++ LAB RECORD
37/114
13.507 Object Oriented Programming Lab 36
int main(){
complex A,B,c;clrscr();A.get();B.get();cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
38/114
37 13.507 Object Oriented Programming Lab
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 y5. 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 & y3. Stop
Output
POINT P1=(1,1)
POINT P2=(5,10)
8/18/2019 OOT OR CPP OR C++ LAB RECORD
39/114
13.507 Object Oriented Programming Lab 38
Experiment No: 10
PARAMETERIZED CONSTRUCTOR
Aim
To implement a program using parameterized constructor.
Program #include#includeclass point{
int x,y;public:point(int a,int b){
x=a;y=b;
}void display(){
cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
40/114
39 13.507 Object Oriented Programming Lab
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
parameter4. 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
8/18/2019 OOT OR CPP OR C++ LAB RECORD
41/114
8/18/2019 OOT OR CPP OR C++ LAB RECORD
42/114
41 13.507 Object Oriented Programming Lab
Algorithm
1. Start
2. Initialise integer variable count as zero
3. Create class Test
4. Create a constructor Test() for the class Test5. increment count
6. Display message7. Create a destructor ~Test() for the class Test
8. Display message
9. decrement count10. 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 NUMBER1 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..
8/18/2019 OOT OR CPP OR C++ LAB RECORD
43/114
13.507 Object Oriented Programming Lab 42
Experiment No: 12
DESTRUCTOR
Aim
To implement a program using destructor.
Program #include#includeint count=0;class test{
public:test(){
count++;
cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
44/114
43 13.507 Object Oriented Programming Lab
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 publicmember functions setvalue() and declare friend function add()
4. Create X as the object of class XYZ, A as the object of class ABC5. 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 parameters8. Stop
Function:setvalue()
1. Start2. Pass integer value as the formal parameter3. Assign value to data
4. Stop
Function:add()
1. Start
2. Pass object obj1 of XYZ & object obj2 of ABC as formal parameter
3. Display sum of values of two classes by adding obj1.data and obj2.data4. Stop
Output
SUM OF DATA VALUES OF XYZ AND ABC OBJECTS USING FRIENDFUNCTION=55
8/18/2019 OOT OR CPP OR C++ LAB RECORD
45/114
13.507 Object Oriented Programming Lab 44
Experiment No: 13
FRIEND FUNCTION
Aim
A program to find the sum of data values used in two classes.
Program #include#includeclass 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
8/18/2019 OOT OR CPP OR C++ LAB RECORD
46/114
45 13.507 Object Oriented Programming Lab
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 Space4. 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. Start
2. Pass 3 integer variables a,b and c as formal parameter
3. Assign a to x4. Assign b to y5. Assign c to z
6. Stop
Function:display()
1. Start
2. Display x
3. Display y4. Display z
5. Stop
Function:operator-()
1. Start
2. Assign -x to x
3. Assign -y to y
4.
Assign -z to z5. Stop
Output
S: X=10 Y=-20 Z=30
-S: X=-10 Y= 20 Z=-30
8/18/2019 OOT OR CPP OR C++ LAB RECORD
47/114
13.507 Object Oriented Programming Lab 46
Experiment No: 14
OVERLOADING UNARY OPERATOR
Aim
A program to overload a unary operator.
Program #include#includeclass 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
8/18/2019 OOT OR CPP OR C++ LAB RECORD
48/114
8/18/2019 OOT OR CPP OR C++ LAB RECORD
49/114
13.507 Object Oriented Programming Lab 48
Experiment No: 15
OVERLOADING BINARY OPERATOR
Aim
A program to overload a binary operator.
Program #include#includeclass 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
8/18/2019 OOT OR CPP OR C++ LAB RECORD
50/114
49 13.507 Object Oriented Programming Lab
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 matrices4. 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. Start
2. Read m,n
3. for(i
8/18/2019 OOT OR CPP OR C++ LAB RECORD
51/114
13.507 Object Oriented Programming Lab 50
Experiment No: 16
MATRIX OPERATION
Aim
To perform matrix operations using operator overloading.
Program #include#include
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(){coutm>>n;cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
52/114
51 13.507 Object Oriented Programming Lab
5. for(j
8/18/2019 OOT OR CPP OR C++ LAB RECORD
53/114
13.507 Object Oriented Programming Lab 52
matrix matrix::operator+(matrix m1){matrix t;t.m=m;t.n=n;for(i=0;i
8/18/2019 OOT OR CPP OR C++ LAB RECORD
54/114
53 13.507 Object Oriented Programming Lab
1 1
1 1
2nd matrix
Enter the row and column
2 2
Enter the elements
1 1
1 1
sum:
2 2
2 2
sub:
0 0
0 0
mul:
2 2
2 2
8/18/2019 OOT OR CPP OR C++ LAB RECORD
55/114
13.507 Object Oriented Programming Lab 54
if(m1.m==m2.m&&m1.n==m2.n){m3=m1+m2;cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
56/114
55 13.507 Object Oriented Programming Lab
Algorithm
1. Start
2. Create a class alpha with a constructor and a function
3. Create a class beta with a constructors and a function
4. Create a class gamma that inherites beta and alpha .It has a constructor and a
function.5. Creating object of class gamma.
6. Call function g.show_x()
7. Call function show_y()
8. Call function show_mn()
9. Stop.
Function: alpha()
1. Start2. x
8/18/2019 OOT OR CPP OR C++ LAB RECORD
57/114
13.507 Object Oriented Programming Lab 56
Experiment No: 17
CONSTRUCTORS IN DERIVED CLASS
Aim
A program to implement constructors in derived class.
Program ##include#includeclass alpha{int x;public:alpha(int i){
x=i;cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
58/114
57 13.507 Object Oriented Programming Lab
Function:show_mn()
1. Start
2. Display m
3. Display n
4. stop.
OUTPUT
BETA INITIALIZED
ALPHA INITIALIZED
GAMMA INITIALIZED
X=5
Y=10.75
M=20
N=30
8/18/2019 OOT OR CPP OR C++ LAB RECORD
59/114
13.507 Object Oriented Programming Lab 58
n=d;cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
60/114
59 13.507 Object Oriented Programming Lab
Algorithm
1. Start
2. Create a class metres with m and cm as it’s private members and get(), show()
as it’s public members and function sum() as friend to it.
3. Create a class inches with f and in as it’s private members and gets(), shows()
as it’s public members and function sum() as friend to it.
4. Create m1 as the object of class metres5. 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,cm3. Stop
Function:show()
1. Start
2. Display m,cm
3. Stop
Function:gets()
1. Start2. Read f,in
3. Stop
Function:shows()
1. Start
2. Display f,in
3. Stop
Function:sum()
1.
Start2. 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,
8/18/2019 OOT OR CPP OR C++ LAB RECORD
61/114
8/18/2019 OOT OR CPP OR C++ LAB RECORD
62/114
61 13.507 Object Oriented Programming Lab
Assign mf as quotient of m1.m and 0.3048
12. Assign cmin as quotient of m1.cm and 2.5413. 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&cm1 1Enter distance in feet & in
1 1Distance in m&cm : 1m 1cmDistance in feet & inch : 1feet 1in
1. Distance in m&cm2. Distance in feet & inchEnter your choice 1
Total distance : 1.3048m 3.54cm
8/18/2019 OOT OR CPP OR C++ LAB RECORD
63/114
13.507 Object Oriented Programming Lab 62
friend void sum(DM,DB);};void sum(DM m1,DB m2 ){
cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
64/114
63 13.507 Object Oriented Programming Lab
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
8/18/2019 OOT OR CPP OR C++ LAB RECORD
65/114
13.507 Object Oriented Programming Lab 64
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#include#include#includeclass 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
8/18/2019 OOT OR CPP OR C++ LAB RECORD
66/114
65 13.507 Object Oriented Programming Lab
Function:price()
1. Start2. display price
3. Read p
4. price
8/18/2019 OOT OR CPP OR C++ LAB RECORD
67/114
13.507 Object Oriented Programming Lab 66
{book b[50];clrscr();coutn;cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
68/114
67 13.507 Object Oriented Programming Lab
8/18/2019 OOT OR CPP OR C++ LAB RECORD
69/114
13.507 Object Oriented Programming Lab 68
cin>>nam;couttit;for(int j=0;j
8/18/2019 OOT OR CPP OR C++ LAB RECORD
70/114
69 13.507 Object Oriented Programming Lab
8/18/2019 OOT OR CPP OR C++ LAB RECORD
71/114
13.507 Object Oriented Programming Lab 70
default:break;
}}getch();return 0;}}Result
The program was successfully compiled and run to get the desired output.
8/18/2019 OOT OR CPP OR C++ LAB RECORD
72/114
71 13.507 Object Oriented Programming Lab
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 member4. 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. Start
2. Assign 5 to a
3. Assign 10 to b
4. Stop
Function:getvalue()
1. Start
2. Return value of a
3. Stop
Function:show()
1. Start
2. Display value of a3. Stop
Function:mul()
1. Start
2. Find product of b and return value of getvalue() function to c
3. Stop
8/18/2019 OOT OR CPP OR C++ LAB RECORD
73/114
13.507 Object Oriented Programming Lab 72
Experiment No: 20
SINGLE INHERITANCE
Aim
A program to implement single inheritance
Program #include#includeclass 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
8/18/2019 OOT OR CPP OR C++ LAB RECORD
74/114
73 13.507 Object Oriented Programming Lab
Function:display()
1. Start
2. Display value of a
3. Display value of b
4. Display value of c
5. Stop
Output
a=5
a=5
b=10
c=50
a=5
b=20
c=100
8/18/2019 OOT OR CPP OR C++ LAB RECORD
75/114
13.507 Object Oriented Programming Lab 74
cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
76/114
75 13.507 Object Oriented Programming Lab
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
member4. 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 n3. Stop
Function:display()
1. Start
2. Display value of m
3. Display value of n
4. Display value of m*n
5. Stop
Output
m=5
n=8
m*n=40
8/18/2019 OOT OR CPP OR C++ LAB RECORD
77/114
13.507 Object Oriented Programming Lab 76
Experiment No: 21
MULTIPLE INHERITANCE
Aim
A program to implement multiple inheritance
Program # include#includeclass 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
8/18/2019 OOT OR CPP OR C++ LAB RECORD
78/114
77 13.507 Object Oriented Programming Lab
8/18/2019 OOT OR CPP OR C++ LAB RECORD
79/114
13.507 Object Oriented Programming Lab 78
p.getm(5);p.getn(8);p.display();getch();return 0;}Result
The program was successfully compiled and run to get the desired output.
8/18/2019 OOT OR CPP OR C++ LAB RECORD
80/114
79 13.507 Object Oriented Programming Lab
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. Start.
2. Pass an integer value a as formal parameter.
3. Assign a to the variable roll_number.
4. Stop.
Function put_number()
1. Start.
2. Display roll_number.
3. Stop.
Function get_marks
1. Start.
2. Pass float type values x,y as formal parameters.
3.
Assign x to sub1 and y to sub2.4. Stop.
Function put_marks
1. Start.
2. Display sub1 and sub2.
3. Stop.
8/18/2019 OOT OR CPP OR C++ LAB RECORD
81/114
13.507 Object Oriented Programming Lab 80
Experiment No: 22
MULTILEVEL INHERITANCE
Aim
A program to implement multilevel inheritance
Program # include#includeclass Student{protected:int rollno;public:void get_rollno(int a)
{rollno=a;}void put_rollno(void){cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
82/114
81 13.507 Object Oriented Programming Lab
display()
1. Start.
2. Assign the sum of sub1 and sub2 to total.
3. Call function put_number().
4. Call function put_marks().
5. Display total.6. Stop.
Output
Roll Number :11.
Mark in sub1:75.
Mark in sub2 :59.5
Total =134.5
8/18/2019 OOT OR CPP OR C++ LAB RECORD
83/114
13.507 Object Oriented Programming Lab 82
void total::display(void){total=m1+m2;put_rollno();put_marks();cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
84/114
8/18/2019 OOT OR CPP OR C++ LAB RECORD
85/114
13.507 Object Oriented Programming Lab 84
Experiment No: 23
STUDENT DETAILS USING INHERITANCE
Aim
A program to implement student details using inheritance
Program # include#includeclass student{protected:int id;char name[20];public:
void get(){cin>>id>>name;}void show (){coutm3;}void show_m()
{cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
86/114
85 13.507 Object Oriented Programming Lab
2. Display m1,m2,m3
3. Stop
Function :show()
1. Start
2. Display wt
3. Stop
Function :sum()
1. Start
2. tot
8/18/2019 OOT OR CPP OR C++ LAB RECORD
87/114
13.507 Object Oriented Programming Lab 86
cin>>wt;}void show_w(){cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
88/114
87 13.507 Object Oriented Programming Lab
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 hasinteract() as it's public member.
5. From menu Read any option,ch if ch
8/18/2019 OOT OR CPP OR C++ LAB RECORD
89/114
13.507 Object Oriented Programming Lab 88
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#include#includeclass bank{
public:char name[20];char acctype[20];double amount;double bal;long accno;void get(){coutname;coutaccno;coutamount;coutacctype;bal=0;bal=bal+amount;
}void deposit(){double dep;coutdep;if(bal>500)bal=bal+dep;elsebal=bal+(dep-50);
}void withdraw()
Date :
8/18/2019 OOT OR CPP OR C++ LAB RECORD
90/114
89 13.507 Object Oriented Programming Lab
2. Read dep
3. if(bal>500)
4. bal
8/18/2019 OOT OR CPP OR C++ LAB RECORD
91/114
13.507 Object Oriented Programming Lab 90
{double wd;coutwd;if((bal-wd)>0)bal=bal-wd;elsecout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
92/114
91 13.507 Object Oriented Programming Lab
1. Create2. Deposite3. Withdraw4. Balance5. Interest6. Exit
Enter your choice1Enter the no: of customers 1Enter nameramEnter account number3456Enter amount deposited2000Enter account type
savings
8/18/2019 OOT OR CPP OR C++ LAB RECORD
93/114
13.507 Object Oriented Programming Lab 92
case 1:for(;;){cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
94/114
93 13.507 Object Oriented Programming Lab
8/18/2019 OOT OR CPP OR C++ LAB RECORD
95/114
13.507 Object Oriented Programming Lab 94
for(i=0;i
8/18/2019 OOT OR CPP OR C++ LAB RECORD
96/114
95 13.507 Object Oriented Programming Lab
8/18/2019 OOT OR CPP OR C++ LAB RECORD
97/114
8/18/2019 OOT OR CPP OR C++ LAB RECORD
98/114
97 13.507 Object Oriented Programming Lab
8/18/2019 OOT OR CPP OR C++ LAB RECORD
99/114
13.507 Object Oriented Programming Lab 98
return 0;}
Result
The program was successfully compiled and run to get the desired output.
8/18/2019 OOT OR CPP OR C++ LAB RECORD
100/114
99 13.507 Object Oriented Programming Lab
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 member4. 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 r3. ar
8/18/2019 OOT OR CPP OR C++ LAB RECORD
101/114
13.507 Object Oriented Programming Lab 100
Experiment No: 25
SHAPE VIRTUAL FUNCTION
Aim
A program to implement shape class using virtual function.
Program # include#includeclass shape{protected:float r;int l,b,br,h;public:
virtual void show(){cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
102/114
101 13.507 Object Oriented Programming Lab
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 triangle4 5
Area of triangle: 10
8/18/2019 OOT OR CPP OR C++ LAB RECORD
103/114
8/18/2019 OOT OR CPP OR C++ LAB RECORD
104/114
8/18/2019 OOT OR CPP OR C++ LAB RECORD
105/114
13.507 Object Oriented Programming Lab 104
Experiment No: 26
CLASS TEMPLATE
Aim
A program to implement class template.
Program # include#includeconst size=3;templateclass vector{
int i;T *V;
public:vector(){
V=new T[size];for(i=0;i
8/18/2019 OOT OR CPP OR C++ LAB RECORD
106/114
105 13.507 Object Oriented Programming Lab
8/18/2019 OOT OR CPP OR C++ LAB RECORD
107/114
13.507 Object Oriented Programming Lab 106
int y[3]={4,5,6};vectorv1;vectorv2;v1=x;v2=y;cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
108/114
8/18/2019 OOT OR CPP OR C++ LAB RECORD
109/114
13.507 Object Oriented Programming Lab 108
Experiment No: 27
SWAP USING TEMPLATE
Aim
A program to implement swap operation using template.
Program # include#includetemplate void swap (T &x,T &y){T temp=x;x=y;y=temp;
}void fun(int m,int n,float a,float b){cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
110/114
8/18/2019 OOT OR CPP OR C++ LAB RECORD
111/114
13.507 Object Oriented Programming Lab 110
Experiment No: 28
FILE USING CLASS
Aim
Program to read and write a file using class
Program # include#include#includeclass inventory{char name[10];int cost,code;public:
void readdata(){coutname>>code>>cost;}void writedata(){cout
8/18/2019 OOT OR CPP OR C++ LAB RECORD
112/114
111 13.507 Object Oriented Programming Lab
output
Pen 101 5Ink 102 20
Pencil 103 4
8/18/2019 OOT OR CPP OR C++ LAB RECORD
113/114
8/18/2019 OOT OR CPP OR C++ LAB RECORD
114/114