0% found this document useful (0 votes)
93 views

Labmanual

The document provides information about the Object Oriented Programming (OOP) lab conducted at Sindh Madressatul Islam University. The lab aims to strengthen problem solving using OOP, design applications using OOP features, handle exceptions, and implement OOP concepts. It outlines the weekly programs to be completed which include programs on classes, inheritance, templates, exceptions and more. It also provides examples of sample programs completed for some early labs on functions, classes, constructors and arrays.

Uploaded by

ghulam ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Labmanual

The document provides information about the Object Oriented Programming (OOP) lab conducted at Sindh Madressatul Islam University. The lab aims to strengthen problem solving using OOP, design applications using OOP features, handle exceptions, and implement OOP concepts. It outlines the weekly programs to be completed which include programs on classes, inheritance, templates, exceptions and more. It also provides examples of sample programs completed for some early labs on functions, classes, constructors and arrays.

Uploaded by

ghulam ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 54

1|Page

SINDH MADRESSATUL ISLAM UNIVERSITY

LAB MANUAL
Submitted by: Maham Imtiaz
ID: BSE-22S-060
Section: 2A
Department of Software Engineering
Course: Object Oriented language(OOP)
Submitted to: Sir Haqua Nawaz Lashari
2|Page

OBJECT ORIENTED PROGRAMMING LAB


Fall 2022

Program Objectives:
• To strengthen problem solving ability by using the characteristics of an object-oriented
approach.
• To design applications using object oriented features
• To handle Exceptions in programs.
• To teach the student to implement object oriented concepts.

Weekly LAB 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 Description
3|Page

Data members
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
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
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 in ascending orde
13 a) Write a Program Containing a Possible Exception. Use a Try
4|Page

Block to Throw it and a Catch Block to Handle it Properly.


b) Write a Program to Demonstrate the Catching of All
Exceptions

LAB#01

C++ Standard Library


The C++ Standard Library can be categorized into two parts:
• The Standard Function Library: This library consists of general-purpose,stand-alone
5|Page

functions that are not part of any class. The function library is inherited from C.
• The Object Oriented Class Library: This is a collection of classes and associated
functions.
Standard C++ Library incorporates all the Standard C libraries also, with small additions
and
changes to support type safety.
The Standard Function Library:
The standard function library is divided into the following categories:
• I/O
• String and character handling
• Mathematical
• Time, date, and localization
• Dynamic allocation
• Miscellaneous
• Wide-character functions
The Object Oriented Class Library:
Standard C++ Object Oriented Library defines an extensive set of classes that provide
support for
a number of common activities, including I/O, strings, and numeric processing. This
library
includes the following:
• The Standard C++ I/O Classes
• The String Class
• The Numeric Classes
• The STL Container Classes
• The STL Algorithms
• The STL Function Objects
• The STL Iterators
• The STL Allocators
6|Page

• The Localization library


• Exception Handling Classes
• Miscellaneous Support Library

LAB#02

2.a) Write a C++ program to find the sum of individual digits of a positive integer.
Program:
#include<iostream>
using namespace std;

int sum_of_digits(int n)
{
int digit,sum=0;
while(n!=0)
{
digit=n%10;
sum=sum+digit;
n=n/10;
}
return sum;
}
int main(){

int number,digits_sum;
cout<<"Enter Positive integer within the range:";
cin>>number;
digits_sum=sum_of_digits(number);
7|Page

cout<<"sum of digts of "<<number<<" is "<<digits_sum;


return 0;
}
Output:

2.b)Write a C++ Program to generate first n terms of Fibonacci sequence.


Program:
#include<iostream>
using namespace std;
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;
}
}
8|Page

int main()
{
int terms;
cout<<"Enter How many terms to be printed:";
cin>>terms;
fib(terms);
return 0;
}
Output:

LAB#03

1)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>
using namespace std;
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++)
{
9|Page

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;
}
Output:

2)Write a C++ Program to find both the largest and smallest number in a list of
integers.
Program:
#include<iostream>
using namespace std;
int main()
10 | P a g e

{
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;
}
Output:
11 | P a g e

LAB#04

4.a)Write a C++ program to sort a list of numbers in ascending order.


Program:
#include<iostream>
using namespace std;
void sort(int data[],int n)
{
for(int i=0;i<n;i++)// read the elements of an array
for(int j=0;j<n-1;j++)
{
int t;
if(data[j]>data[j+1])
{
t=data[j];
data[j]=data[j+1];
data[j+1]=t;
}
}
}
int main()
{
int a[50],i,n;
cout<<"Enter How many elements to sort:";
cin>>n;
cout<<"Enter Elements:";
for(i=0;i<n;i++) // read the elements of an array
12 | P a g e

cin>>a[i];
cout<<"Sorted array is\n";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
return 0;
}
Output:

4.b) Write aProgram to illustrate New and Delete Keywords for dynamic memory
allocation.
Program:
#include<iostream>
using namespace std;
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;
13 | P a g e

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];
int Array_sum=sum(p,n);
cout<<"sum of all values are "<<Array_sum;
return 0;
}
Output:

LAB#05

1)Write a program Illustrating Class Declarations, Definition, and Accessing


Class Members.
Program:
#include<iostream>
using namespace std;
14 | P a g e

class sample
{
private:
public:
int a;
char b;
float c;
void get_data()
{
cout<<"Enter an integer value:";
cin>>a;
cout<<"Enter a character:";
cin>>b;
cout<<"Enter a float value:";
cin>>c;
}
void print_data()
{
cout<<"Values read from keyboard are\n";
cout<<"Integer value:"<<a<<endl;
cout<<"character is :"<<b<<endl;
cout<<"float value is :"<<c<<endl;
cin>>c;
}
};
int main()
{
sample s;//creation of object
15 | P a g e

s.get_data();
s.print_data();
}
Output:

2)Write a C++ Program to illustrate default constructor,parameterized


constructorand
copy constructors.
Program:
#include<iostream>
using namespace std;
class code
{
int id;
int count;
public:
code(){
cout<<"Default constructor called\n";
id=0;
cout<<"id="<<id<<endl;
16 | P a g e

}
code(int a){
cout<<"Parameterized constructor called\n";
id=a;
cout<<"id="<<id<<endl;
}
code(code&x ){
cout<<"copy constructor called\n";
id=x.id;
cout<<"id="<<id<<endl;
}
void display(){
}
~code(){
cout<<"id="<<id<<endl;
cout<<"Object Destroyed"<<endl;
}
};
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();
17 | P a g e

return 0;
}
Output:

3)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
18 | P a g e

assign() Assign Initial Values


compute() to Compute Total, Average
display() to Display the Data.

Program:
#include<iostream>
#include<string.h>
using namespace std;
class student{
public:
char sname[50];
float marks[6];
float total;
float max_marks;

public:
student();
void assign();
void compute();
void display();
};
student::student(){ strcpy(sname," ");
for(int i=0;i<6;i++)
marks[i]=0;
total=0;
max_marks=0;
}
void student::assign(){
19 | P a g e

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 are\n";
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;
cout<<"Percentage:"<<per;
}
int main(){
student obj;
20 | P a g e

obj.assign();
obj.compute();
obj.display();
return 0;
}
Output:

LAB#06

1)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”
21 | P a g e

Program:
#include<iostream>
using namespace std;
class complex
{
float real,img;
public:
complex();
complex(float x,float y);
void read_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)
{
22 | P a g e

}
else
{
sign='-';
img=-img;
sign='+';
}
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:";
23 | P a g e

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:
24 | P a g e

ii)Function Overloading
#include<iostream>
using namespace std;
class printData {
public:
void print(int i) {
cout<<"Printing int: "<<
i <<endl;
}
void print(double f) {
cout<<"Printing float: "<<
f <<endl;
}
void print(char*c) {
cout<<"Printing string: "<<
c <<endl;
}
};
int main(void) {
printData pd;
// Call print to print integer
pd.print(9);
// Call print to print float
pd.print(200.09);
// Call print to print character
25 | P a g e

pd.print("Hello C++");
return 0;
}
Output:

2) 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;
26 | P a g e

}
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:
27 | P a g e

LAB#07

1)Write a program to access members of a STUDENT class using pointer to


object members.
Program:
#include<iostream>
using namespace std;
class student
{
int rollno;
char name[50];
public:
void getdata();
void print();
};
void student::getdata()
{
cout<<"Enter roll number"<<endl;
cin>>rollno;
cout<<"Enter Name ";
28 | P a g e

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 class\n";
student *ptr;
ptr=&a;
ptr->print();
}
Output:

2)Write a Program to generate Fibonacci Series by using Constructor to initialize


the Data Members.
29 | P a g e

Program:
#include<iostream>
using namespace std;
class fibonacci{
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(){
fibonacci obj;
int terms;
cout<<"Enter How many terms to be printed:";
cin>>terms;
30 | P a g e

obj.series(terms);
return 0;
}
Output:

LAB#08

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>
#include<conio.h>
#include<process.h>
#include<iomanip>
using namespace std;
class matrix
{
protected:
int i,j,a[10][10],b[10][10],c[10][10];
int m1,n1,m2,n2;
public:
31 | P a g e

virtual void read()=0;


virtual void display()=0;
virtual void sum()=0;
virtual void sub()=0;
virtual void mult()=0;
};
class result:public matrix
{
public:
void read();
void sum();
void sub();
void mult();
void display();
};
void result::read(void)
{
cout<<"\nenter the order of matrix A ";
cin>>m1>>n1;
cout<<"\nenter the elements of matrix A ";
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
cin>>a[i][j];
}
}
cout<<"\nenter the order of matrix B ";
32 | P a g e

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";
}
33 | P a g e

else
{
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];
}
}
}
}
34 | P a g e

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;
for(int k=0;k<n1;k++){
c[i][j]+=a[i][k]*b[k][j];
}
}
}
}
}
int main(){
int ch;
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;
35 | P a g e

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();
p->display();
break;
case 4:
exit(0);
}
}
}
Output:
36 | P a g e

LAB#9

1)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
37 | P a g e

{
protected:
int a,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:
38 | P a g e

2)Write a C++ Program that illustrate multipe inheritance.


Program:

#include<iostream>
#include<conio.h>
using namespace std;
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:
int sm; // sm = Sports mark
39 | P a g e

public:
void getsm()
{
cout<<"\nEnter the sports mark :";
cin>>sm;
}
};
class statement:public student,public sports
{
int tot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal : "<<tot;
cout<<"\n\tAverage : "<<avg;
}
};
int main()
{
//clrscr();
statement obj;
obj.get();
obj.getsm();
obj.display();
getch();
}
40 | P a g e

Output:

3) Write a C++ Program that illustrate multi level inheritance.


Program:
#include<iostream>
#include<conio.h>
using namespace std;
class top //base class
{
public :
int a;
void getdata(){
cout<<"\n\nEnter first Number :::\t";
cin>>a;
}
void put_data(){
cout<<"\nFirst Number Is :::\t"<<a;
}
};
//First level inheritance
class middle :public top // class middle is derived_1
{
41 | P a g e

public:
int b;
void square(){
void get_data();
b=a*a;
cout<<"\n\nSquare Is :::"<<b;
}
};
//Second level inheritance
class bottom :public middle // class bottom is derived_2
{
public:
int c;
void cube()
{
square();
c=b*a;
cout<<"\n\nCube :::\t"<<c;
}
};
int main() {

bottom b1;
b1.getdata();
b1.put_data();
b1.cube();
getch();
}
42 | P a g e

Output:

4)Write a C++ Program that illustrate Hierarchical inheritance.


Program:
#include <iostream>
using namespace std;
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
class Car : public Vehicle {
};
class Bus : public Vehicle {
};
int main(){
Car obj1;
Bus obj2;
return 0;
}
Output:
43 | P a g e

LAB#10

1)Write a C++ program to illustrate the order of execution of constructors


and destructors.
Program:
#include<iostream>
using namespace std;
class Base
{
public:
Base ( )
{
cout<< "Inside Base constructor" <<endl;
}
~Base ( )
{
cout<< "Inside Base destructor" <<endl;
}
};
class Derived : public Base
{
public:
44 | P a g e

Derived ( )
{
cout<< "Inside Derived constructor" <<endl;
}
~Derived ( )
{
cout<< "Inside Derived destructor" <<endl;
}
};
int main( )
{
Derived x;
}
Output:

2)write a program to invoking derived class member through base classpointer.


Program:
#include <iostream>
#include <conio.h>
using namespace std;
class A
{
45 | P a g e

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;
46 | P a g e

B b;
C c;

A* p = &a;
p->print_me();
p = &b; p->print_me(); p = &c; p->print_me();
return 0;
}
Output:

LAB#11

1)Write a template based program to sort the given list of elements.


Program:
#include<iostream>
using namespace std;
template<class T>
void bubble(T a[], int n)
{
int i, j;
for(i=0;i<n-1;i++)
47 | P a g e

{
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:
48 | P a g e

2)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>
using namespace std;
template<class T> //Template declaration
void maxmin(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
49 | P a g e

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;
cin>>ch; //Reading Choice from User
cout<<"enter the n value\n";
cin>>n; //Number of elements is independent of DATA TYPE
switch(ch)
{
case 1: //for operations over Integer Array
cout<<"enter integers\n";
for(i=0;i<n;i++)
cin>>a[i];
maxmin(a,n);
break;
case 2: //for operations over Character Array
50 | P a g e

cout<<"enter characters\n";
for(i=0;i<n;i++)
cin>>c[i];
maxmin(c,n);
break;
case 3: //for operations over Floating Array
cout<<"enter floatnumbers\n";
for(i=0;i<n;i++)
cin>>f[i];
maxmin(f,n);
break;
case 4: //for operations over Double
cout<<"enter doublenumbers\n";
for(i=0;i<n;i++)
cin>>d[i];
maxmin(d,n);
break;
default:
cout<<"Invalid choice entered...";
}
return 0;
}
Output:
51 | P a g e

LAB#12

1) Write a C++ program containing a possible exception.use a try block to throw it


and a 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)
52 | P a g e

{
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:

2) Write a C++ program to demonstrate the catching of all exceptions.


Program:
#include<iostream>
#include<conio.h>
using namespace std;
53 | P a g e

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;
}
}
int main()
{

cout<<"Testing multiple catches\n:";


test(10);
test(0);
getch();
}
Output:
54 | P a g e

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy