SlideShare a Scribd company logo
OBJECT ORIENTED PROGREAMMING LAB MANUAL
Object Oriented Programming LAB for 2023-2024
CONTENTS
SESSION-2023-24MIT
S.NO. Name of the program
1 Study of C++ Standard library functions
2 a)Write a C++ program to find the sum of individual digits of a positive
integer.
b) Write a C++ program to generate the first n terms of the sequence.
3 a)Write a C++ program to generate all the prime numbers between 1 and n,
where n is a value supplied by the user.
b)Write a C++ program to find both the largest and smallest number in a list of
integers.
4 a)Write a C++ program to sort a list of numbers in ascending order.
b)Write a Program to illustrate New and Delete Keywords for dynamic
memory allocation
5 a)Write a program Illustrating Class Declarations, Definition, and Accessing
Class Members.
b)Program to illustrate default constructor, parameterized constructor and copy
constructors
c) Write a Program to Implement a Class STUDENT having Following
Members:
Member functions
Member Description
assign() Assign Initial Values
compute() to Compute Total, Average
display() to Display the Data.
6 a)Write a Program to Demonstrate the
i)Operator Overloading. ii) Function Overloading.
b) Write a Program to Demonstrate Friend Function and Friend Class.
7 a)Write a Program to Access Members of a STUDENT Class Using Pointer to
Object Members.
b).Write a Program to Generate Fibonacci Series use Constructor to Initialize
the Data Members.
8 Revision of Programs
9 Write a C++ program to implement the matrix ADT using a class. The
operations supported by this ADT are:
a) Reading a matrix. b) Addition of matrices. c) Printing a matrix.
d) Subtraction of matrices. e) Multiplication of matrices
Member Description
Data members
sname Name of the student
Marks array Marks of the student
total Total marks obtained
tmax Total maximum marks
10 Write C++ programs that illustrate how the following forms of inheritance are
supported:
a)Single inheritance b)Multiple inheritance
c)Multi level inheritance d)Hierarchical inheritance
11 a.)Write a C++ program that illustrates the order of execution of constructors
and destructors when new class is derived from more than one base class.
b) Write a Program to Invoking Derived Class Member Through Base Class
Pointer.
12 a) Write a Template Based Program to Sort the Given List of Elements.
b) Write a C++ program that uses function templates to find the largest and
smallest number in a list of integers and to sort a list of numbers inascending
order.
13 a) Write a Program Containing a Possible Exception. Use a Try Block to
Throw it and a Catch Block to Handle it Properly.
b) Write a Program to Demonstrate the Catching of All Exceptions.
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 3
Object Oriented Programming LAB for 2023-2024 MIT
Write a C++ program to find the sum of individual digits of a positive integer.
Program:
#include<iostream.h>
int sum_of_digits(int
n)
{
intdigit,sum=0;
while(n!=0)
{
}
return sum;
}
int main()
{
digit=n%10;
sum=sum+digit;
n=n/10;
int number,digits_sum;
cout<<"Enter Positive integer within the range:";
cin>>number;
digits_sum=sum_of_digits(number);
cout<<"sum of digts of "<<number<<" is "<<digits_sum;
return 0;
}
Input:
Enter Positive integer within the range:4321
Output:
sum of digits of 4321 is 10
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 4
Object Oriented Programming LAB for 2023-2024 MIT
Write a C++ Program to generate first n terms of Fibonacci sequence.
Program:
#include<iostream.h>
void fib(int n)
{
int f0,f1,f,count=0;
f0=0;
f1=1;
while(count<n)
{
cout<<f0<<"t";
count++;
f=f0+f1;
f0=f1;
f1=f;
}
}
int main()
{
int terms;
cout<<"Enter How many terms to be printed:";
cin>>terms;
fib(terms);
return 0;
}
Input:
Enter How many terms to be printed:10
Output:
0 1 1 2 3 5 8 13 21 34
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 5
Object Oriented Programming LAB for 2023-2024 MIT
Write a C++ program to generate all the prime numbers between 1 and n, where n
is a value supplied by the user.
Program:
#include<iostream.h>
void prime(int n)
{
int factors;
cout<<"prime numbers are... ";
for(int i=1;i<=n;i++)
{ factors=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
factors=factors+1;
}
if(factors<=2)
cout<<i<<"t";
}
}
int main()
{
int n;
cout<<"Enter a integer value:";
cin>>n;
prime(n);
return 0;
}
Input:
Enter a integer value:10
Output:
prime numbers are....1 2 3 5 7
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 6
Object Oriented Programming LAB for 2023-2024 MIT
Write a C++ Program to find both the largest and smallest number in a list of integers.
Program:
#include<iostream.h>
int main()
{
int a[50],i,n,small,large;
cout<<"Enter The Array Size:";
cin>>n;
cout<<"ENTER ELEMENTS OF ARRAY";
for(i=0;i<n;i++)
cin>>a[i];
small=a[0];
large=a[0];
for(i=0;i<n;i++)
{
if(a[i]<small)
small=a[i];
if(a[i]>large)
large=a[i];
}
cout<<"largest value is"<<large<<endl;
cout<<"smallest value is:"<<small<<endl;
return 0;
}
Input:
Enter The Array Size:5
ENTER ELEMENTS OF ARRAY5 4 3 2 1
Output:
largest value is5
smallest value is:1
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 8
Object Oriented Programming LAB for 2023-2024 MIT
4.b) Write aProgram to illustrate New and Delete Keywords for dynamic memory
allocation.
Program:
#include<iostream.h>
int sum(int *a,int n)
{
int s=0;
for(int i=0;i<n;i++)
s=s+*(a+i);
return s;
}
int main()
{
int *p,i,n;
cout<<"enter how many values to be read:";
cin>>n;
p=new int[n];
cout<<"Enter values :";
for(int i=0;i<n;i++)
cin>>p[i];
intArray_sum=sum(p,n);
cout<<"sum of all values are "<<Array_sum;
return 0;
}
Input:
enter how many values to be read:4
Enter values :1
2
3
4
Output:
sum of all values are 10
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 9
Object Oriented Programming LAB for 2023-2024 MIT
Week-5
Write a program Illustrating Class Declarations, Definition, and Accessing
Class Members.
Program:
#include<iostream.h>
class sample
{
private:
public:
int a;
char b;
float c;
voidget_data()
{
cout<<"Enter an integer value:";
cin>>a;
cout<<"Enter a character:";
cin>>b;
cout<<"Enter a float value:";
cin>>c;
}
voidprint_data()
{
};
int main()
{
cout<<"Values read from keyboard aren";
cout<<"Integer value:"<<a<<endl;
cout<<"character is :"<<b<<endl;
cout<<"float value is :"<<c<<endl;
cin>>c;
}
sample s;//creation of object
s.get_data();
s.print_data();
}
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 10
Output:
Enter an integer value:12
Enter a character:S
Enter a float value:12.12
Values read from keyboard are
Integer value:12
character is :S
float value is :12.12
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 11
Object Oriented Programming LAB for 2023-2024 MIT
Write a C++ Program to illustrate default constructor,parameterized constructorand
copy constructors.
Program:
#include<iostream.h>
class code
{
int id;
int count;
public:
code()
{
cout<<"Default constructor calledn";
id=0;
cout<<"id="<<id<<endl;
}
code(int a)
{
cout<<"Parameterized constructor calledn";
id=a;
cout<<"id="<<id<<endl;
}
code(code&x )
{
cout<<"copy constructor calledn";
id=x.id;
cout<<"id="<<id<<endl;
}
void display()
{
}
~code()
{
cout<<"id="<<id<<endl;
cout<<"Object Destroyed"<<endl;
Object Oriented Programming LAB for 2023-2024 MIT
}
};
int main()
{
code a(100);//calls parameterized constructor
code b(a); //calls copy constructor
code c(a); //calls copy constructor
code d;//calls default constructor
cout<<"n For object d id="; d.display();
cout<<"n For object a id="; a.display();
cout<<"n For object b id="; d.display();
cout<<"n For object c id="; d.display();
return 0;
}
Output:
Parameterized constructor called
id=100
copy constructor called
id=100
copy constructor called
id=100
Default constructor called
id=0
For object d id=id=0
For object a id=id=100
For object b id=id=0For object c id=id=0
Object Destroyed
Object Destroyed
Object Destroyed
Object Destroyed
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 13
Object Oriented Programming LAB for 2023-24 MIT
Write a Program to Implement a Class STUDENT having following members:
Data members
Member Description
sname Name of the student
Marks array Marks of the student
total Total marks obtained
Tmax Total maximum marks
Member functions
Member Description
assign() Assign Initial Values
compute() to Compute Total, Average
display() to Display the Data.
Program:
#include<iostream.h>
#include<string>
class student
{
public:
};
charsname[50];
float marks[6];
float total;
floatmax_marks;
student();
void assign();
void compute();
void display();
student::student()
{ strcpy(sname," ");
for(int i=0;i<6;i++)
marks[i]=0;
Object Oriented Programming LAB for 2023-2024 MIT
total=0;
max_marks=0;
}
void student::assign()
{
cout<<endl<<"Enter Student Name :";
cin>>sname;
for(int i=0;i<6;i++)
{
cout<<"Enter marks of"<<i+1<<" subject:";
cin>>marks[i];
}
cout<<"Enter Maximum total marks";
cin>>max_marks;
}
void student::compute()
{
total=0;
for(int i=0;i<6;i++)
total+=marks[i];
}
void student::display()
{
cout<<"Student Name:"<<sname<<endl;
cout<<"Marks aren";
for(int i=0;i<6;i++)
cout<<"Subject "<<i+1<<": "<<marks[i]<<endl;
cout<<" n";
cout<<"Total :"<<total<<endl;
cout<<" n";
float per;
per=(total/max_marks)*100;
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 14
Object Oriented Programming LAB for 2023-2024 MIT
cout<<"Percentage:"<<per;
}
int main()
{
studentobj;
obj.assign();
obj.compute();
obj.display();
return 0;
}
Output:
Enter Student Name :sunil
Enter marks of1 subject:60
Enter marks of2 subject:60
Enter marks of3 subject:65
Enter marks of4 subject:65
Enter marks of5 subject:70
Enter marks of6 subject:75
Enter Maximum total marks600
Student Name:sunil
Marks are
Subject 1: 60
Subject 2: 60
Subject 3: 65
Subject 4: 65
Subject 5: 70
Subject 6: 75
Total :395
Percentage:65.8333
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 15
Object Oriented Programming LAB for 2023-2024 MIT
6. Write a program to demonstrate the i)Operator Overloading ii)Function Overloading.
i)Operator Overloading:-The mechanism of giving a special meaning to an operator is called
operator overloading. This can be achieved by special function “operator”
Syntax:
return type classname:: operaotor op(list of arguments)
{
……………………………….
}
Program:
#include<iostream.h>
class complex
{
floatreal,img;
public:
complex();
complex(float x,float y);
voidread_complex();
complex operator+(complex);
complex operator-(complex);
void display();
};
complex::complex()
{
real=img=0;
}
complex::complex(float x,float y)
{
real=x;
img=y;
}
void complex::display()
{
char sign;
if(img<0)
{
}
else
{
sign='-';
img=-img;
sign='+';
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 16
Object Oriented Programming LAB for 2023-2024 MIT
}
cout<<real<<sign<<"i"<<img<<endl;
}
complex complex::operator+(complex c)
{
complex r;
r.real=real+c.real;
r.img=img+c.img;
return r;
}
complex complex::operator-(complex c)
{
complex r;
r.real=real-c.real;
r.img=img-c.img;
return r;
}
void complex::read_complex()
{
cout<<"Enter real part of complex number;";
cin>>real;
cout<<"Enter Imaginary part of complex number:";
cin>>img;
}
int main()
{
complex a;
a.read_complex();
complex b;
b.read_complex();
complex c;
c=a+b;
cout<<"After Addition of two complex numbers";
c.display();
c=a-b;
cout<<"Difference of two complex numbers";
c.display();
}
Output:
Enter real part of complex number;1
Enter Imaginary part of complex number:2
Enter real part of complex number;2
Enter Imaginary part of complex number:4
After Addition of two complex numbers3+i6
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 17
Object Oriented Programming LAB for 2023-2024 MIT
Difference of two complex numbers-1-i2
ii)Function Overloading
#include<iostream>
usingnamespacestd;
classprintData
{
public:
voidprint(int i)
{
cout<<"Printing int: "<< i <<endl;
}
voidprint(double f)
{
cout<<"Printing float: "<< f <<endl;
}
voidprint(char*c)
{
cout<<"Printing string: "<< c <<endl;
}
};
int main(void)
{
printDatapd;
// Call print to print integer
pd.print(5);
// Call print to print float
pd.print(500.263);
// Call print to print character
pd.print("Hello C++");
return0;
}
Output:
Printingint:5
Printingfloat:500.263
Printing string:Hello C++
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 18
Object Oriented Programming LAB for 2023-2024 MIT
7. b) Write a Program to demonstrate friend function and friend class.
Program:
#include<iostream>
using namespace std;
class sample2;
class sample1
{
int x;
public:
sample1(int a);
friend void max(sample1 s1,sample2 s2);
};
sample1::sample1(int a)
{
x=a;
}
class sample2
{
int y;
public:
sample2(int b);
friend void max(sample1 s1,sample2 s2);
};
sample2::sample2(int b)
{
y=b;
}
void max(sample1 s1,sample2 s2)
{
if(s1.x>s2.y)
cout<<"Data member in Object of class sample1 is larger "<<endl;
else
}
cout<<"Data member in Object of class sample2 is larger "<<endl;
int main()
{
sample1 obj1(3);
sample2 obj2(5);
max(obj1,obj2);
}
Output
Data member in Object of class sample2 is larger
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 19
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 20
Object Oriented Programming LAB for 2023-2024 MIT
Week-7
7. a)Write a program to access members of a STUDENT class using pointer to object
members.
Program:
#include<iostream.h>
class student
{
introllno;
char name[50];
public:
voidgetdata();
void print();
};
void student::getdata()
{
cout<<"Enter roll number"<<endl;
cin>>rollno;
cout<<"Enter Name ";
cin>>name;
}
void student::print()
{
cout<<"Name :"<<name<<endl;
cout<<"Roll no:"<<rollno<<endl;
}
int main()
{
student a;
a.getdata();
a.print();
cout<<"Pointer to classn";
student *ptr;
ptr=&a;
ptr->print();
}
Output:
Enter roll number
123
Enter Name jayapal
Name :jayapal
Roll no:123
Pointer to class
Name :jayapal
Roll no:123
Object Oriented Programming LAB for 2023-2024 MIT
7. b)Write a Program to generate Fibonacci Series by using Constructor to initialize the
Data Members.
Program:
#include<iostream>
using namespace std;
classfibonacci{
int f0,f1,f;
public:
fibonacci()
{
f0=0;
f1=1;
}
void series(int n)
{
int count=0;
f0=0;
f1=1;
while(count<n)
{
cout<<f0<<"t";
count++;
f=f0+f1;
f0=f1;
f1=f;
}
}
};
int main()
{
fibonacciobj;
int terms;
cout<<"Enter How many terms to be printed:";
cin>>terms;
obj.series(terms);
return 0;
}
Output:Enter How many terms to be printed:5
0 1 1 2 3
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 21
Object Oriented Programming LAB for 2023-2024 MIT
Week-9
9) Write a c++ program to implement the matrix ADT using a class.Theoperations
supported by this ADT are:
a)Reading a marix b)addition of matrices c)printing a matrix
d)subtraction of matrices e)multiplication of matrices
Program:
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<iomanip.h>
class matrix
{
protected:
inti,j,a[10][10],b[10][10],c[10][10];
int m1,n1,m2,n2;
public:
virtual void read()=0;
virtual void display()=0;
virtual void sum()=0;
virtual void sub()=0;
virtual void mult()=0;
};
classresult:public matrix
{
public:
void read();
void sum();
void sub();
voidmult();
void display();
};
void result::read()
{
cout<<"nenter the order of matrix A ";
cin>>m1>>n1;
cout<<"nenter the elements of matrix A ";
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 22
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 23
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
cin>>a[i][j];
}
}
cout<<"nenter the order of matrix B ";
cin>>m2>>n2;
cout<<"nenter the matrix B ";
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
{
cin>>b[i][j];
}
}
}
void result::display()
{
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
cout.width(3);
cout<<c[i][j];
}
cout<<"n";
}
}
void result::sum()
{
if((m1!=m2)||(n1!=n2))
{
cout<<"the order should be same for addition";
}
else
{
Object Oriented Programming LAB for 2023-2024 MIT
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 24
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}
}
void result::sub()
{
if((m1!=m2)||(n1!=n2))
{
cout<<"the order should be same for subtraction ";
}
else
{
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
c[i][j]=a[i][j]-b[i][j];
//cout<<a[i][j];
}
}
}
}
void result::mult(void)
{
if(n2!=m2)
{
cout<<"Invalid order limit ";
}
else
{
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
c[i][j]=0;
Object Oriented Programming LAB for 2023-2024 MIT
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 25
for(int k=0;k<n1;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
}
}
void main()
{
intch;
class matrix *p;
class result r;
p=&r;
clrscr();
while(1)
{
cout<<"n1. Addition of matrices ";
cout<<"n2. Subtraction of matrices ";
cout<<"n3. Multipication of matrices ";
cout<<"n4. Exit";
cout<<"Enter your choice ";
cin>>ch;
switch(ch)
{
case 1:
p->read();
p->sum();
p->display();
break;
case 2:
(p)->read();
p->sub();
p->display();
break;
case 3:
p->read();
p->mult();
Object Oriented Programming LAB for 2023-2024 MIT
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 26
p->display();
break;
case 4:
exit(0);
}
}
}
Output:
1. Addition of matrices
2. Subtraction of matrices
3. Multipication of matrices
4. Exit
Enter your choice
1
enter the order of matrix A
2 2
enter the elements of matrix A
1 1
1 1
enter the order of matrix B
2 2
enter the elements of matrix B
1 1
1 1
2 2
2 2
Object Oriented Programming LAB for 2023-2024 MIT
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 27
Week-10
10.a)Write a C++ Program that illustrate single inheritance.
The mechanism of deriving a new class from an old one is called inheritance or derivation
class derived-class-name : visibility-mode base-class-name
{
………
………
}
Program:
#include<iostream>
using namespace std;
class A
{
protected:
inta,b;
public:
void get()
{
cout<<"Enter any two integer values";
cin>>a>>b;
}
};
class B:public A
{
int c;
public:
void add()
{
c=a+b;
cout<<a<<"+"<<b<<"="<<c;
}
};
int main()
{
B b;
b.get();
b.add();
}
Output:
Enter any two integer values1
2
1+2=3
Object Oriented Programming LAB for 2023-2024 MIT
RCET
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 28
10.b)Write a C++ Program that illustrate multipe inheritance.
Program:
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two marks :";
cin>>m1>>m2;
}
};
class sports
{
protected:
intsm; // sm = Sports mark
public:
voidgetsm()
{
cout<<"nEnter the sports mark :";
cin>>sm;
}
};
classstatement:publicstudent,public sports
{
inttot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"nntRoll No : "<<rno<<"ntTotal : "<<tot;
Object Oriented Programming LAB for 2023-2024 MIT
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 29
cout<<"ntAverage : "<<avg;
}
};
void main()
{
clrscr();
statementobj;
obj.get();
obj.getsm();
obj.display();
getch();
}
Output:
Enter the Roll no: 100
Enter two marks
90
80
Enter the Sports Mark: 90
Roll No: 100
Total : 260
Average: 86.66
Object Oriented Programming LAB for 2023-2024 MIT
RCET
Object Oriented Programming LAB for 2023-2024 MIT
10.c) Write a C++ Program that illustrate multi level inheritance.
Program:
#include<iostream.h>
#include<conio.h>
class top //base class
{
public :
int a;
voidgetdata()
{
cout<<"nnEnter first Number :::t";
cin>>a;
}
voidputdata()
{
cout<<"nFirst Number Is :::t"<<a;
}
};
//First level inheritance
class middle :public top // class middle is derived_1
{
public:
int b;
void square()
{
getdata();
b=a*a;
cout<<"nnSquare Is :::"<<b;
}
};
//Second level inheritance
class bottom :public middle // class bottom is derived_2
{
public:
int c;
void cube()
{
square();
c=b*a;
cout<<"nnCube :::t"<<c;
}
};
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 30
Object Oriented Programming LAB for 2023-2024 MIT
int main()
{
clrscr();
bottom b1;
b1.cube();
getch();
}
Input:
Enter first number ::: 4
Output:
Square Is ::: 16
Cube ::: 64
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 31
Object Oriented Programming LAB for 2023-2024 MIT
10.d)Write a C++ Program that illustrate Hierarchical inheritance.
Program:
#include<iostream.h>
#include<conio.h>
class A //Base Class
{
public:
inta,b;
voidgetnumber()
{
cout<<"nnEnter Number :::t";
cin>>a;
}
};
class B : public A //Derived Class 1
{
public:
void square()
{
getnumber(); //Call Base class property
cout<<"nntSquare of the number :::t"<<(a*a);
cout<<"nnt ";
}
};
class C :public A //Derived Class 2
{
public:
void cube()
{
getnumber(); //Call Base class property
cout<<"nntCube of the number :::t"<<(a*a*a);
cout<<"nnt ";
}
};
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 32
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 33
int main()
{
clrscr();
B b1; //b1 is object of Derived class 1
b1.square(); //call member function of class B
C c1; //c1 is object of Derived class 2
c1.cube(); //call member function of class C
getch();
}
Input:
Enter number ::: 2
Output:
Square of the number ::: 4
Input:
Enter number ::: 2
Output:
Cube of the number ::: 8
Object Oriented Programming LAB for 2023-2024 MIT
Object Oriented Programming LAB for 2023-2024 MIT
Week-11
Write a C++ program to illustrate the order of execution of constructors
and destructors.
Program:
#include<iostream.h>
class Base
{
public:
Base ( )
{
cout<< "Inside Base constructor" <<endl;
}
~Base ( )
{
cout<< "Inside Base destructor" <<endl;
}
};
class Derived : public Base
{
public:
Derived ( )
{
cout<< "Inside Derived constructor" <<endl;
}
~Derived ( )
{
cout<< "Inside Derived destructor" <<endl;
}
};
void main( )
{
Derived x;
}
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 34
Object Oriented Programming LAB for 2023-2024 MIT
Output:
Inside Base constructor
Inside Derived constructor
Inside Derived destructor
Inside Base destructor
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 35
Object Oriented Programming LAB for 2023-2024 MIT
write a program to invoking derived class member through base classpointer.
Program:
#include <iostream.h>
#include <conio.h>
class A
{
public:
virtual void print_me(void)
{
cout<< "I'm A" <<endl;
}
virtual ~A()
{
}
};
class B : public A
{
public:
virtual void print_me(void)
{
cout<< "I'm B" <<endl;
}
};
class C : public A
{
public:
virtual void print_me(void)
{
cout<< "I'm C" <<endl;
}
};
int main()
{
A a;
B b;
C c;
clrscr();
A* p = &a;
p->print_me();
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 36
Object Oriented Programming LAB for 2023-2024 MIT
p = &b;
p->print_me();
p = &c;
p->print_me();
return 0;
}
Output:
I'm A
I'm B
I'm C
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 37
Object Oriented Programming LAB for 2023-2024 MIT
Week-12
12.a)Write a template based program to sort the given list of elements.
Program:
#include<iostream.h>
using namespace std;
template<class T>
void bubble(T a[], int n)
{
int i, j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
T temp;
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
int main()
{
int a[6]={17,16,15,14,9,-1};
char b[4]={'z','b','x','a'};
bubble(a,6);
cout<<"nSorted Order Integers: ";
for(int i=0;i<6;i++)
cout<<a[i]<<"t";
bubble(b,4);
cout<<"nSorted Order Characters: ";
for(int j=0;j<4;j++)
cout<<b[j]<<"t";
}
Output:
Sorted Order Integers: -1 9 14 15 16 17
Sorted Order Characters: a b x z
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 38
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 39
Object Oriented Programming LAB for 2023-2024 MIT
12.b)Write a C++ program that uses function templates to find the largest and smallest
number in a list of integers and to sort a list of numbers in ascending order.
Program:
#include<iostream.h>
template<class T> //Template declaration
voidmaxmin(T a[],int n) //Function Template
{
int i;
T temp;
for(i=0;i<n;i++)
for(int j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
cout<<"max="<<a[n-1]<<"n"<<"min="<<a[0]<<"n";
/*After sorting an Array starting index consists of Small element and Final index consists of
Largest element */
cout<<"sorted list is: n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
}
int main()
{
int a[50],i,ch,n;
double d[50];
float f[50];
char c[50];
cout<<"1.integer"<<endl;
cout<<"2.characters"<<endl;
cout<<" 3.float numbers"<<endl;
cout<<" 4.double numbers"<<endl;
cout<<"enter corresponding Index Example : enter '1' for integers"<<endl;
Object Oriented Programming LAB for 2023-2024 MIT
cin>>ch; //Reading Choice from User
cout<<"enter the n valuen";
cin>>n; //Number of elements is independent of DATA TYPE
switch(ch)
{
case 1: //for operations over Integer Array
cout<<"enter integersn";
for(i=0;i<n;i++)
cin>>a[i];
maxmin(a,n);
break;
case 2: //for operations over Character Array
cout<<"enter charactersn";
for(i=0;i<n;i++)
cin>>c[i];
maxmin(c,n);
break;
case 3: //for operations over Floating Array
cout<<"enter floatnumbersn";
for(i=0;i<n;i++)
cin>>f[i];
maxmin(f,n);
break;
case 4: //for operations over Double
cout<<"enter doublenumbersn";
for(i=0;i<n;i++)
cin>>d[i];
maxmin(d,n);
break;
default:
cout<<"Invalid choice entered...";
}
return 0;
}
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 40
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 41
Object Oriented Programming LAB for 2023-2024 MIT
Week-13
Write a C++ program containing a possible exception.use a try block to throw it
anda catch block to handle it properly.
Program:
#include <iostream>
using namespace std;
int main()
{
int x = -1;
cout<< "Before try n";
try
{
cout<< "Inside try n";
if (x < 0)
{
throw x;
cout<< "After throw (Never executed) n";
}
}
catch (int x )
{
cout<< "Exception Caught n";
}
cout<< "After catch (Will be executed) n";
return 0;
}
Output:
Before try
Inside try
Exception Caught
After catch (Will be executed)
w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 42
Write a C++ program to demonstrate the catching of all exceptions.
Program:
#include<iostream.h>
#include<conio.h>
void test(int x)
{
try
{
if(x>0)
throw x;
else
}
throw 'x';
catch(int x)
{
cout<<"Catch a integer and that integer is:"<<x;
}
catch(char x)
{
cout<<"Catch a character and that character is:"<<x;
}
}
void main()
{
clrscr();
cout<<"Testing multiple catchesn:";
test(10);
test(0);
getch();
}
Output:
Testing multiple catchesCatch a integer and that integer is: 10 Catch a character and that character is: x
Object Oriented Programming LAB for 2023-2024 MIT

More Related Content

DOCX
Programming fundamentals
PDF
Ch 4
DOCX
Programming in c
DOCX
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
PDF
Computer Science Sample Paper 2015
DOCX
Practical File of c++.docx lab manual program question
PDF
Computer P-Lab-Manual acc to syllabus.pdf
PDF
c++ referesher 1.pdf
Programming fundamentals
Ch 4
Programming in c
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Computer Science Sample Paper 2015
Practical File of c++.docx lab manual program question
Computer P-Lab-Manual acc to syllabus.pdf
c++ referesher 1.pdf

Similar to OOP LAB MANUAL BTECH 3RD SEMESTER2023-24.pdf (20)

DOC
programming in C++ report
PDF
Object Oriented Programming Using C++ Practical File
DOCX
PDF
Zoro123456789123456789123456789123456789
 
PDF
labb123456789123456789123456789123456789
 
PPTX
C and C++ programming basics for Beginners.pptx
PDF
Arithmetic operator
PDF
C++ manual Report Full
PPTX
C coding#1
PDF
Subject:Programming in C - Lab Programmes
DOCX
PDF
C- Programming Assignment 3
PPTX
Unit-IV.pptx
PDF
C Programming Example
DOCX
C Programming
PDF
I PUC CS Lab_programs
DOCX
C++ lab assignment
PPT
Linux_C_LabBasics.ppt
DOCX
B.Com 1year Lab programs
DOCX
C program report tips
programming in C++ report
Object Oriented Programming Using C++ Practical File
Zoro123456789123456789123456789123456789
 
labb123456789123456789123456789123456789
 
C and C++ programming basics for Beginners.pptx
Arithmetic operator
C++ manual Report Full
C coding#1
Subject:Programming in C - Lab Programmes
C- Programming Assignment 3
Unit-IV.pptx
C Programming Example
C Programming
I PUC CS Lab_programs
C++ lab assignment
Linux_C_LabBasics.ppt
B.Com 1year Lab programs
C program report tips
Ad

Recently uploaded (20)

PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Well-logging-methods_new................
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
web development for engineering and engineering
DOCX
573137875-Attendance-Management-System-original
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Welding lecture in detail for understanding
PDF
PPT on Performance Review to get promotions
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPT
Project quality management in manufacturing
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Digital Logic Computer Design lecture notes
PPTX
Lecture Notes Electrical Wiring System Components
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
bas. eng. economics group 4 presentation 1.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Well-logging-methods_new................
OOP with Java - Java Introduction (Basics)
Structs to JSON How Go Powers REST APIs.pdf
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Strings in CPP - Strings in C++ are sequences of characters used to store and...
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
web development for engineering and engineering
573137875-Attendance-Management-System-original
Embodied AI: Ushering in the Next Era of Intelligent Systems
Welding lecture in detail for understanding
PPT on Performance Review to get promotions
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Project quality management in manufacturing
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Digital Logic Computer Design lecture notes
Lecture Notes Electrical Wiring System Components
Ad

OOP LAB MANUAL BTECH 3RD SEMESTER2023-24.pdf

  • 1. OBJECT ORIENTED PROGREAMMING LAB MANUAL Object Oriented Programming LAB for 2023-2024 CONTENTS SESSION-2023-24MIT S.NO. Name of the program 1 Study of C++ Standard library functions 2 a)Write a C++ program to find the sum of individual digits of a positive integer. b) Write a C++ program to generate the first n terms of the sequence. 3 a)Write a C++ program to generate all the prime numbers between 1 and n, where n is a value supplied by the user. b)Write a C++ program to find both the largest and smallest number in a list of integers. 4 a)Write a C++ program to sort a list of numbers in ascending order. b)Write a Program to illustrate New and Delete Keywords for dynamic memory allocation 5 a)Write a program Illustrating Class Declarations, Definition, and Accessing Class Members. b)Program to illustrate default constructor, parameterized constructor and copy constructors c) Write a Program to Implement a Class STUDENT having Following Members: Member functions Member Description assign() Assign Initial Values compute() to Compute Total, Average display() to Display the Data. 6 a)Write a Program to Demonstrate the i)Operator Overloading. ii) Function Overloading. b) Write a Program to Demonstrate Friend Function and Friend Class. 7 a)Write a Program to Access Members of a STUDENT Class Using Pointer to Object Members. b).Write a Program to Generate Fibonacci Series use Constructor to Initialize the Data Members. 8 Revision of Programs 9 Write a C++ program to implement the matrix ADT using a class. The operations supported by this ADT are: a) Reading a matrix. b) Addition of matrices. c) Printing a matrix. d) Subtraction of matrices. e) Multiplication of matrices Member Description Data members sname Name of the student Marks array Marks of the student total Total marks obtained tmax Total maximum marks
  • 2. 10 Write C++ programs that illustrate how the following forms of inheritance are supported: a)Single inheritance b)Multiple inheritance c)Multi level inheritance d)Hierarchical inheritance 11 a.)Write a C++ program that illustrates the order of execution of constructors and destructors when new class is derived from more than one base class. b) Write a Program to Invoking Derived Class Member Through Base Class Pointer. 12 a) Write a Template Based Program to Sort the Given List of Elements. b) Write a C++ program that uses function templates to find the largest and smallest number in a list of integers and to sort a list of numbers inascending order. 13 a) Write a Program Containing a Possible Exception. Use a Try Block to Throw it and a Catch Block to Handle it Properly. b) Write a Program to Demonstrate the Catching of All Exceptions.
  • 3. w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 3 Object Oriented Programming LAB for 2023-2024 MIT Write a C++ program to find the sum of individual digits of a positive integer. Program: #include<iostream.h> int sum_of_digits(int n) { intdigit,sum=0; while(n!=0) { } return sum; } int main() { digit=n%10; sum=sum+digit; n=n/10; int number,digits_sum; cout<<"Enter Positive integer within the range:"; cin>>number; digits_sum=sum_of_digits(number); cout<<"sum of digts of "<<number<<" is "<<digits_sum; return 0; } Input: Enter Positive integer within the range:4321 Output: sum of digits of 4321 is 10
  • 4. w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 4 Object Oriented Programming LAB for 2023-2024 MIT Write a C++ Program to generate first n terms of Fibonacci sequence. Program: #include<iostream.h> void fib(int n) { int f0,f1,f,count=0; f0=0; f1=1; while(count<n) { cout<<f0<<"t"; count++; f=f0+f1; f0=f1; f1=f; } } int main() { int terms; cout<<"Enter How many terms to be printed:"; cin>>terms; fib(terms); return 0; } Input: Enter How many terms to be printed:10 Output: 0 1 1 2 3 5 8 13 21 34
  • 5. w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 5 Object Oriented Programming LAB for 2023-2024 MIT Write a C++ program to generate all the prime numbers between 1 and n, where n is a value supplied by the user. Program: #include<iostream.h> void prime(int n) { int factors; cout<<"prime numbers are... "; for(int i=1;i<=n;i++) { factors=0; for(int j=1;j<=i;j++) { if(i%j==0) factors=factors+1; } if(factors<=2) cout<<i<<"t"; } } int main() { int n; cout<<"Enter a integer value:"; cin>>n; prime(n); return 0; } Input: Enter a integer value:10 Output: prime numbers are....1 2 3 5 7
  • 6. w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 6 Object Oriented Programming LAB for 2023-2024 MIT Write a C++ Program to find both the largest and smallest number in a list of integers. Program: #include<iostream.h> int main() { int a[50],i,n,small,large; cout<<"Enter The Array Size:"; cin>>n; cout<<"ENTER ELEMENTS OF ARRAY"; for(i=0;i<n;i++) cin>>a[i]; small=a[0]; large=a[0]; for(i=0;i<n;i++) { if(a[i]<small) small=a[i]; if(a[i]>large) large=a[i]; } cout<<"largest value is"<<large<<endl; cout<<"smallest value is:"<<small<<endl; return 0; } Input: Enter The Array Size:5 ENTER ELEMENTS OF ARRAY5 4 3 2 1 Output: largest value is5 smallest value is:1
  • 7. w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 8 Object Oriented Programming LAB for 2023-2024 MIT 4.b) Write aProgram to illustrate New and Delete Keywords for dynamic memory allocation. Program: #include<iostream.h> int sum(int *a,int n) { int s=0; for(int i=0;i<n;i++) s=s+*(a+i); return s; } int main() { int *p,i,n; cout<<"enter how many values to be read:"; cin>>n; p=new int[n]; cout<<"Enter values :"; for(int i=0;i<n;i++) cin>>p[i]; intArray_sum=sum(p,n); cout<<"sum of all values are "<<Array_sum; return 0; } Input: enter how many values to be read:4 Enter values :1 2 3 4 Output: sum of all values are 10
  • 8. w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 9 Object Oriented Programming LAB for 2023-2024 MIT Week-5 Write a program Illustrating Class Declarations, Definition, and Accessing Class Members. Program: #include<iostream.h> class sample { private: public: int a; char b; float c; voidget_data() { cout<<"Enter an integer value:"; cin>>a; cout<<"Enter a character:"; cin>>b; cout<<"Enter a float value:"; cin>>c; } voidprint_data() { }; int main() { cout<<"Values read from keyboard aren"; cout<<"Integer value:"<<a<<endl; cout<<"character is :"<<b<<endl; cout<<"float value is :"<<c<<endl; cin>>c; } sample s;//creation of object s.get_data(); s.print_data(); }
  • 9. w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 10 Output: Enter an integer value:12 Enter a character:S Enter a float value:12.12 Values read from keyboard are Integer value:12 character is :S float value is :12.12
  • 10. w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 11 Object Oriented Programming LAB for 2023-2024 MIT Write a C++ Program to illustrate default constructor,parameterized constructorand copy constructors. Program: #include<iostream.h> class code { int id; int count; public: code() { cout<<"Default constructor calledn"; id=0; cout<<"id="<<id<<endl; } code(int a) { cout<<"Parameterized constructor calledn"; id=a; cout<<"id="<<id<<endl; } code(code&x ) { cout<<"copy constructor calledn"; id=x.id; cout<<"id="<<id<<endl; } void display() { } ~code() { cout<<"id="<<id<<endl; cout<<"Object Destroyed"<<endl;
  • 11. Object Oriented Programming LAB for 2023-2024 MIT } }; int main() { code a(100);//calls parameterized constructor code b(a); //calls copy constructor code c(a); //calls copy constructor code d;//calls default constructor cout<<"n For object d id="; d.display(); cout<<"n For object a id="; a.display(); cout<<"n For object b id="; d.display(); cout<<"n For object c id="; d.display(); return 0; } Output: Parameterized constructor called id=100 copy constructor called id=100 copy constructor called id=100 Default constructor called id=0 For object d id=id=0 For object a id=id=100 For object b id=id=0For object c id=id=0 Object Destroyed Object Destroyed Object Destroyed Object Destroyed
  • 12. w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 13 Object Oriented Programming LAB for 2023-24 MIT Write a Program to Implement a Class STUDENT having following members: Data members Member Description sname Name of the student Marks array Marks of the student total Total marks obtained Tmax Total maximum marks Member functions Member Description assign() Assign Initial Values compute() to Compute Total, Average display() to Display the Data. Program: #include<iostream.h> #include<string> class student { public: }; charsname[50]; float marks[6]; float total; floatmax_marks; student(); void assign(); void compute(); void display(); student::student() { strcpy(sname," "); for(int i=0;i<6;i++) marks[i]=0;
  • 13. Object Oriented Programming LAB for 2023-2024 MIT total=0; max_marks=0; } void student::assign() { cout<<endl<<"Enter Student Name :"; cin>>sname; for(int i=0;i<6;i++) { cout<<"Enter marks of"<<i+1<<" subject:"; cin>>marks[i]; } cout<<"Enter Maximum total marks"; cin>>max_marks; } void student::compute() { total=0; for(int i=0;i<6;i++) total+=marks[i]; } void student::display() { cout<<"Student Name:"<<sname<<endl; cout<<"Marks aren"; for(int i=0;i<6;i++) cout<<"Subject "<<i+1<<": "<<marks[i]<<endl; cout<<" n"; cout<<"Total :"<<total<<endl; cout<<" n"; float per; per=(total/max_marks)*100; w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 14
  • 14. Object Oriented Programming LAB for 2023-2024 MIT cout<<"Percentage:"<<per; } int main() { studentobj; obj.assign(); obj.compute(); obj.display(); return 0; } Output: Enter Student Name :sunil Enter marks of1 subject:60 Enter marks of2 subject:60 Enter marks of3 subject:65 Enter marks of4 subject:65 Enter marks of5 subject:70 Enter marks of6 subject:75 Enter Maximum total marks600 Student Name:sunil Marks are Subject 1: 60 Subject 2: 60 Subject 3: 65 Subject 4: 65 Subject 5: 70 Subject 6: 75 Total :395 Percentage:65.8333 w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 15
  • 15. Object Oriented Programming LAB for 2023-2024 MIT 6. Write a program to demonstrate the i)Operator Overloading ii)Function Overloading. i)Operator Overloading:-The mechanism of giving a special meaning to an operator is called operator overloading. This can be achieved by special function “operator” Syntax: return type classname:: operaotor op(list of arguments) { ………………………………. } Program: #include<iostream.h> class complex { floatreal,img; public: complex(); complex(float x,float y); voidread_complex(); complex operator+(complex); complex operator-(complex); void display(); }; complex::complex() { real=img=0; } complex::complex(float x,float y) { real=x; img=y; } void complex::display() { char sign; if(img<0) { } else { sign='-'; img=-img; sign='+'; w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 16
  • 16. Object Oriented Programming LAB for 2023-2024 MIT } cout<<real<<sign<<"i"<<img<<endl; } complex complex::operator+(complex c) { complex r; r.real=real+c.real; r.img=img+c.img; return r; } complex complex::operator-(complex c) { complex r; r.real=real-c.real; r.img=img-c.img; return r; } void complex::read_complex() { cout<<"Enter real part of complex number;"; cin>>real; cout<<"Enter Imaginary part of complex number:"; cin>>img; } int main() { complex a; a.read_complex(); complex b; b.read_complex(); complex c; c=a+b; cout<<"After Addition of two complex numbers"; c.display(); c=a-b; cout<<"Difference of two complex numbers"; c.display(); } Output: Enter real part of complex number;1 Enter Imaginary part of complex number:2 Enter real part of complex number;2 Enter Imaginary part of complex number:4 After Addition of two complex numbers3+i6 w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 17
  • 17. Object Oriented Programming LAB for 2023-2024 MIT Difference of two complex numbers-1-i2 ii)Function Overloading #include<iostream> usingnamespacestd; classprintData { public: voidprint(int i) { cout<<"Printing int: "<< i <<endl; } voidprint(double f) { cout<<"Printing float: "<< f <<endl; } voidprint(char*c) { cout<<"Printing string: "<< c <<endl; } }; int main(void) { printDatapd; // Call print to print integer pd.print(5); // Call print to print float pd.print(500.263); // Call print to print character pd.print("Hello C++"); return0; } Output: Printingint:5 Printingfloat:500.263 Printing string:Hello C++ w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 18
  • 18. Object Oriented Programming LAB for 2023-2024 MIT 7. b) Write a Program to demonstrate friend function and friend class. Program: #include<iostream> using namespace std; class sample2; class sample1 { int x; public: sample1(int a); friend void max(sample1 s1,sample2 s2); }; sample1::sample1(int a) { x=a; } class sample2 { int y; public: sample2(int b); friend void max(sample1 s1,sample2 s2); }; sample2::sample2(int b) { y=b; } void max(sample1 s1,sample2 s2) { if(s1.x>s2.y) cout<<"Data member in Object of class sample1 is larger "<<endl; else } cout<<"Data member in Object of class sample2 is larger "<<endl; int main() { sample1 obj1(3); sample2 obj2(5); max(obj1,obj2); } Output Data member in Object of class sample2 is larger w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 19
  • 19. w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 20 Object Oriented Programming LAB for 2023-2024 MIT Week-7 7. a)Write a program to access members of a STUDENT class using pointer to object members. Program: #include<iostream.h> class student { introllno; char name[50]; public: voidgetdata(); void print(); }; void student::getdata() { cout<<"Enter roll number"<<endl; cin>>rollno; cout<<"Enter Name "; cin>>name; } void student::print() { cout<<"Name :"<<name<<endl; cout<<"Roll no:"<<rollno<<endl; } int main() { student a; a.getdata(); a.print(); cout<<"Pointer to classn"; student *ptr; ptr=&a; ptr->print(); } Output: Enter roll number 123 Enter Name jayapal Name :jayapal Roll no:123 Pointer to class Name :jayapal Roll no:123
  • 20. Object Oriented Programming LAB for 2023-2024 MIT 7. b)Write a Program to generate Fibonacci Series by using Constructor to initialize the Data Members. Program: #include<iostream> using namespace std; classfibonacci{ int f0,f1,f; public: fibonacci() { f0=0; f1=1; } void series(int n) { int count=0; f0=0; f1=1; while(count<n) { cout<<f0<<"t"; count++; f=f0+f1; f0=f1; f1=f; } } }; int main() { fibonacciobj; int terms; cout<<"Enter How many terms to be printed:"; cin>>terms; obj.series(terms); return 0; } Output:Enter How many terms to be printed:5 0 1 1 2 3 w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 21
  • 21. Object Oriented Programming LAB for 2023-2024 MIT Week-9 9) Write a c++ program to implement the matrix ADT using a class.Theoperations supported by this ADT are: a)Reading a marix b)addition of matrices c)printing a matrix d)subtraction of matrices e)multiplication of matrices Program: #include<iostream.h> #include<conio.h> #include<process.h> #include<iomanip.h> class matrix { protected: inti,j,a[10][10],b[10][10],c[10][10]; int m1,n1,m2,n2; public: virtual void read()=0; virtual void display()=0; virtual void sum()=0; virtual void sub()=0; virtual void mult()=0; }; classresult:public matrix { public: void read(); void sum(); void sub(); voidmult(); void display(); }; void result::read() { cout<<"nenter the order of matrix A "; cin>>m1>>n1; cout<<"nenter the elements of matrix A "; w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 22
  • 22. w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 23 for(i=0;i<m1;i++) { for(j=0;j<n1;j++) { cin>>a[i][j]; } } cout<<"nenter the order of matrix B "; cin>>m2>>n2; cout<<"nenter the matrix B "; for(i=0;i<m2;i++) { for(j=0;j<n2;j++) { cin>>b[i][j]; } } } void result::display() { for(i=0;i<m1;i++) { for(j=0;j<n1;j++) { cout.width(3); cout<<c[i][j]; } cout<<"n"; } } void result::sum() { if((m1!=m2)||(n1!=n2)) { cout<<"the order should be same for addition"; } else { Object Oriented Programming LAB for 2023-2024 MIT
  • 23. w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 24 for(i=0;i<m1;i++) { for(j=0;j<n1;j++) { c[i][j]=a[i][j]+b[i][j]; } } } } void result::sub() { if((m1!=m2)||(n1!=n2)) { cout<<"the order should be same for subtraction "; } else { for(i=0;i<m1;i++) { for(j=0;j<n1;j++) { c[i][j]=a[i][j]-b[i][j]; //cout<<a[i][j]; } } } } void result::mult(void) { if(n2!=m2) { cout<<"Invalid order limit "; } else { for(i=0;i<m1;i++) { for(j=0;j<n2;j++) { c[i][j]=0; Object Oriented Programming LAB for 2023-2024 MIT
  • 24. w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 25 for(int k=0;k<n1;k++) { c[i][j]+=a[i][k]*b[k][j]; } } } } } void main() { intch; class matrix *p; class result r; p=&r; clrscr(); while(1) { cout<<"n1. Addition of matrices "; cout<<"n2. Subtraction of matrices "; cout<<"n3. Multipication of matrices "; cout<<"n4. Exit"; cout<<"Enter your choice "; cin>>ch; switch(ch) { case 1: p->read(); p->sum(); p->display(); break; case 2: (p)->read(); p->sub(); p->display(); break; case 3: p->read(); p->mult(); Object Oriented Programming LAB for 2023-2024 MIT
  • 25. w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 26 p->display(); break; case 4: exit(0); } } } Output: 1. Addition of matrices 2. Subtraction of matrices 3. Multipication of matrices 4. Exit Enter your choice 1 enter the order of matrix A 2 2 enter the elements of matrix A 1 1 1 1 enter the order of matrix B 2 2 enter the elements of matrix B 1 1 1 1 2 2 2 2 Object Oriented Programming LAB for 2023-2024 MIT
  • 26. w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 27 Week-10 10.a)Write a C++ Program that illustrate single inheritance. The mechanism of deriving a new class from an old one is called inheritance or derivation class derived-class-name : visibility-mode base-class-name { ……… ……… } Program: #include<iostream> using namespace std; class A { protected: inta,b; public: void get() { cout<<"Enter any two integer values"; cin>>a>>b; } }; class B:public A { int c; public: void add() { c=a+b; cout<<a<<"+"<<b<<"="<<c; } }; int main() { B b; b.get(); b.add(); } Output: Enter any two integer values1 2 1+2=3 Object Oriented Programming LAB for 2023-2024 MIT RCET
  • 27. w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 28 10.b)Write a C++ Program that illustrate multipe inheritance. Program: #include<iostream.h> #include<conio.h> class student { protected: int rno,m1,m2; public: void get() { cout<<"Enter the Roll no :"; cin>>rno; cout<<"Enter the two marks :"; cin>>m1>>m2; } }; class sports { protected: intsm; // sm = Sports mark public: voidgetsm() { cout<<"nEnter the sports mark :"; cin>>sm; } }; classstatement:publicstudent,public sports { inttot,avg; public: void display() { tot=(m1+m2+sm); avg=tot/3; cout<<"nntRoll No : "<<rno<<"ntTotal : "<<tot; Object Oriented Programming LAB for 2023-2024 MIT
  • 28. w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 29 cout<<"ntAverage : "<<avg; } }; void main() { clrscr(); statementobj; obj.get(); obj.getsm(); obj.display(); getch(); } Output: Enter the Roll no: 100 Enter two marks 90 80 Enter the Sports Mark: 90 Roll No: 100 Total : 260 Average: 86.66 Object Oriented Programming LAB for 2023-2024 MIT RCET
  • 29. Object Oriented Programming LAB for 2023-2024 MIT 10.c) Write a C++ Program that illustrate multi level inheritance. Program: #include<iostream.h> #include<conio.h> class top //base class { public : int a; voidgetdata() { cout<<"nnEnter first Number :::t"; cin>>a; } voidputdata() { cout<<"nFirst Number Is :::t"<<a; } }; //First level inheritance class middle :public top // class middle is derived_1 { public: int b; void square() { getdata(); b=a*a; cout<<"nnSquare Is :::"<<b; } }; //Second level inheritance class bottom :public middle // class bottom is derived_2 { public: int c; void cube() { square(); c=b*a; cout<<"nnCube :::t"<<c; } }; w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 30
  • 30. Object Oriented Programming LAB for 2023-2024 MIT int main() { clrscr(); bottom b1; b1.cube(); getch(); } Input: Enter first number ::: 4 Output: Square Is ::: 16 Cube ::: 64 w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 31
  • 31. Object Oriented Programming LAB for 2023-2024 MIT 10.d)Write a C++ Program that illustrate Hierarchical inheritance. Program: #include<iostream.h> #include<conio.h> class A //Base Class { public: inta,b; voidgetnumber() { cout<<"nnEnter Number :::t"; cin>>a; } }; class B : public A //Derived Class 1 { public: void square() { getnumber(); //Call Base class property cout<<"nntSquare of the number :::t"<<(a*a); cout<<"nnt "; } }; class C :public A //Derived Class 2 { public: void cube() { getnumber(); //Call Base class property cout<<"nntCube of the number :::t"<<(a*a*a); cout<<"nnt "; } }; w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 32
  • 32. w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 33 int main() { clrscr(); B b1; //b1 is object of Derived class 1 b1.square(); //call member function of class B C c1; //c1 is object of Derived class 2 c1.cube(); //call member function of class C getch(); } Input: Enter number ::: 2 Output: Square of the number ::: 4 Input: Enter number ::: 2 Output: Cube of the number ::: 8 Object Oriented Programming LAB for 2023-2024 MIT
  • 33. Object Oriented Programming LAB for 2023-2024 MIT Week-11 Write a C++ program to illustrate the order of execution of constructors and destructors. Program: #include<iostream.h> class Base { public: Base ( ) { cout<< "Inside Base constructor" <<endl; } ~Base ( ) { cout<< "Inside Base destructor" <<endl; } }; class Derived : public Base { public: Derived ( ) { cout<< "Inside Derived constructor" <<endl; } ~Derived ( ) { cout<< "Inside Derived destructor" <<endl; } }; void main( ) { Derived x; } w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 34
  • 34. Object Oriented Programming LAB for 2023-2024 MIT Output: Inside Base constructor Inside Derived constructor Inside Derived destructor Inside Base destructor w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 35
  • 35. Object Oriented Programming LAB for 2023-2024 MIT write a program to invoking derived class member through base classpointer. Program: #include <iostream.h> #include <conio.h> class A { public: virtual void print_me(void) { cout<< "I'm A" <<endl; } virtual ~A() { } }; class B : public A { public: virtual void print_me(void) { cout<< "I'm B" <<endl; } }; class C : public A { public: virtual void print_me(void) { cout<< "I'm C" <<endl; } }; int main() { A a; B b; C c; clrscr(); A* p = &a; p->print_me(); w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 36
  • 36. Object Oriented Programming LAB for 2023-2024 MIT p = &b; p->print_me(); p = &c; p->print_me(); return 0; } Output: I'm A I'm B I'm C w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 37
  • 37. Object Oriented Programming LAB for 2023-2024 MIT Week-12 12.a)Write a template based program to sort the given list of elements. Program: #include<iostream.h> using namespace std; template<class T> void bubble(T a[], int n) { int i, j; for(i=0;i<n-1;i++) { for(j=0;j<n-1;j++) { if(a[j]>a[j+1]) { T temp; temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } } int main() { int a[6]={17,16,15,14,9,-1}; char b[4]={'z','b','x','a'}; bubble(a,6); cout<<"nSorted Order Integers: "; for(int i=0;i<6;i++) cout<<a[i]<<"t"; bubble(b,4); cout<<"nSorted Order Characters: "; for(int j=0;j<4;j++) cout<<b[j]<<"t"; } Output: Sorted Order Integers: -1 9 14 15 16 17 Sorted Order Characters: a b x z w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 38
  • 38. w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 39 Object Oriented Programming LAB for 2023-2024 MIT 12.b)Write a C++ program that uses function templates to find the largest and smallest number in a list of integers and to sort a list of numbers in ascending order. Program: #include<iostream.h> template<class T> //Template declaration voidmaxmin(T a[],int n) //Function Template { int i; T temp; for(i=0;i<n;i++) for(int j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } cout<<"max="<<a[n-1]<<"n"<<"min="<<a[0]<<"n"; /*After sorting an Array starting index consists of Small element and Final index consists of Largest element */ cout<<"sorted list is: n"; for(i=0;i<n;i++) cout<<a[i]<<" "; } int main() { int a[50],i,ch,n; double d[50]; float f[50]; char c[50]; cout<<"1.integer"<<endl; cout<<"2.characters"<<endl; cout<<" 3.float numbers"<<endl; cout<<" 4.double numbers"<<endl; cout<<"enter corresponding Index Example : enter '1' for integers"<<endl;
  • 39. Object Oriented Programming LAB for 2023-2024 MIT cin>>ch; //Reading Choice from User cout<<"enter the n valuen"; cin>>n; //Number of elements is independent of DATA TYPE switch(ch) { case 1: //for operations over Integer Array cout<<"enter integersn"; for(i=0;i<n;i++) cin>>a[i]; maxmin(a,n); break; case 2: //for operations over Character Array cout<<"enter charactersn"; for(i=0;i<n;i++) cin>>c[i]; maxmin(c,n); break; case 3: //for operations over Floating Array cout<<"enter floatnumbersn"; for(i=0;i<n;i++) cin>>f[i]; maxmin(f,n); break; case 4: //for operations over Double cout<<"enter doublenumbersn"; for(i=0;i<n;i++) cin>>d[i]; maxmin(d,n); break; default: cout<<"Invalid choice entered..."; } return 0; } w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 40
  • 40. w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 41 Object Oriented Programming LAB for 2023-2024 MIT Week-13 Write a C++ program containing a possible exception.use a try block to throw it anda catch block to handle it properly. Program: #include <iostream> using namespace std; int main() { int x = -1; cout<< "Before try n"; try { cout<< "Inside try n"; if (x < 0) { throw x; cout<< "After throw (Never executed) n"; } } catch (int x ) { cout<< "Exception Caught n"; } cout<< "After catch (Will be executed) n"; return 0; } Output: Before try Inside try Exception Caught After catch (Will be executed)
  • 41. w w w . m r c e t . a c . i n D e p t o f H u m a n i t i e s a n d S c i e n c e s Page 42 Write a C++ program to demonstrate the catching of all exceptions. Program: #include<iostream.h> #include<conio.h> void test(int x) { try { if(x>0) throw x; else } throw 'x'; catch(int x) { cout<<"Catch a integer and that integer is:"<<x; } catch(char x) { cout<<"Catch a character and that character is:"<<x; } } void main() { clrscr(); cout<<"Testing multiple catchesn:"; test(10); test(0); getch(); } Output: Testing multiple catchesCatch a integer and that integer is: 10 Catch a character and that character is: x Object Oriented Programming LAB for 2023-2024 MIT