Lab Manual Oop C 2022f
Lab Manual Oop C 2022f
Lab Manual Oop C 2022f
Fall 2022
Program Objectives:
LAB#01:
LAB#02:
a) Write a C++ program to find the sum of individual digits of a positive integer.
LAB#03:
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.
LAB#04:
LAB#05
a) Write a program Illustrating Class Declarations, Definition, and Accessing Class Members.
Member Description
Data members
Member functions
Member Description
Sindh Madressatul Islam University SMIU- F2022
LAB#06:
LAB#07:
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.
LAB#08:
Revision laboratory
LAB#09
Write a C++ program to implement the matrix ADT using a class. The operations supported by
this ADT are:
LAB#10
Write C++ programs that illustrate how the following forms of inheritance are supported:
a)Single inheritance
b)Multiple inheritance
LAB#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.
LAB#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.
LAB#13
a) Write a Program Containing a Possible Exception. Use a Try Block to Throw it and a Catch
Block to Handle it Properly.
LAB#14
Revision
Sindh Madressatul Islam University SMIU- F2022
Text Books:
References:
Program Outcomes:
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
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
Sindh Madressatul Islam University SMIU- F2022
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
14 Revision of programs
Sindh Madressatul Islam University SMIU- F2022
These are the instructions for the students attending the lab :
• Before entering the lab the student should carry the following things (MANDATORY)
1. Identity card issued by the college.
2. Class notes
3. Lab observation book
4. Lab Manual
5. Lab Record
• Student must sign in and sign out in the register provided when attending the lab session
without fail.
• Come to the laboratory in time. Students, who are late more than 15 min., will not be allowed
to attend the lab.
• Students need to maintain 100% attendance in lab if not a strict action will be taken.
• All students must follow a Dress Code while in the laboratory
• Foods, drinks are NOT allowed.
• All bags must be left at the indicated place.
• Refer to the lab staff if you need any help in using the lab.
• Respect the laboratory and its other users.
• Workspace must be kept clean and tidy after experiment is completed.
• Read the Manual carefully before coming to the laboratory and be sure about what you are
supposed to do.
• Do the experiments as per the instructions given in the manual.
• Copy all the programs to observation which are taught in class before attending the lab
session.
• Lab records need to be submitted on or before the date of submission.
Sindh Madressatul Islam University SMIU- F2022
LAB#01
• 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.
• I/O
• Mathematical
• Dynamic allocation
• Miscellaneous
• Wide-character functions
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:
LAB#02
2.a) Write a C++ program to find the sum of individual digits of a positive integer.
Program:
#include<iostream.h>
intsum_of_digits(int n)
{
intdigit,sum=0;
while(n!=0)
{
digit=n%10;
sum=sum+digit;
n=n/10;
}
return sum;
}
int main()
{
intnumber,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
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
LAB#03
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
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
LAB#04
Program:
#include<iostream.h>
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
cin>>a[i];
cout<<"Sorted array is \n";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
return 0;
}
Input:
Enter How many elements to sort:5
Enter Elements5 4 3 2 1
Output:
Sorted array is
5 4 3 2 1
Sindh Madressatul Islam University SMIU- F2022
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
LAB#05
Output:
Enter an integer value:12
Enter a character:S
Integer value:12
character is :S
}
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()
{
cout<<"id="<<id<<endl;
}
~code()
{
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();
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
Sindh Madressatul Islam University SMIU- F2022
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
{
charsname[50];
float marks[6];
float total;
floatmax_marks;
public:
student();
void assign();
void compute();
void display();
};
student::student()
{ strcpy(sname," ");
for(int i=0;i<6;i++)
marks[i]=0;
Sindh Madressatul Islam University SMIU- F2022
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 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;
Sindh Madressatul Islam University SMIU- F2022
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
Sindh Madressatul Islam University SMIU- F2022
LAB#06
}
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
Sindh Madressatul Islam University SMIU- F2022
Output:
Printingint:5
Printingfloat:500.263
Sindh Madressatul Islam University SMIU- F2022
sample2::sample2(int b)
{
y=b;
}
int main()
{
sample1 obj1(3);
sample2 obj2(5);
max(obj1,obj2);
}
Output
LAB#07
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:
Enter roll number
123
Enter Name jayapal
Name :jayapal
Roll no:123
Pointer to class
Name :jayapal
Roll no:123
Sindh Madressatul Islam University SMIU- F2022
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
LAB#08
Revision of Programs
Sindh Madressatul Islam University SMIU- F2022
LAB#09
};
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 ";
Sindh Madressatul Islam University SMIU-F2022
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
{
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;
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)
{
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
22
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
LAB#10
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
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<<"\n\n\tRoll No : "<<rno<<"\n\tTotal : "<<tot;
cout<<"\n\tAverage : "<<avg;
}
};
void main()
{
clrscr();
statementobj;
obj.get();
obj.getsm();
obj.display();
getch();
}
Output:
Enter the Roll no: 100
Enter two marks
90
80
int main()
{
clrscr();
bottom b1;
b1.cube();
getch();
}
Input:
Enter first number ::: 4
Output:
Square Is ::: 16
Cube ::: 64
Sindh Madressatul Islam University SMIU- F2022
};
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
Sindh Madressatul Islam University SMIU- F2022
LAB#11
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;
}
Sindh Madressatul Islam University SMIU- F2022
Output:
Inside Base constructor
Inside Derived constructor
Inside Derived destructor
Inside Base destructor
Sindh Madressatul Islam University SMIU- F2022
p = &b;
p->print_me();
p = &c;
p->print_me();
return 0;
}
Output:
I'm A
I'm B
I'm C
Sindh Madressatul Islam University SMIU- F2022
LAB#12
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);
}
Output:
Sorted Order Integers: -1 9 14 15 16 17
Sorted Order Characters: a b x z
Sindh Madressatul Islam University SMIU- F2022
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;
Sindh Madressatul Islam University SMIU- F2022
default:
cout<<"Invalid choice entered...";
}
return 0;
}
Sindh Madressatul Islam University SMIU- F2022
LAB#13
13.a) 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)
{
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)
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 catches\n:";
test(10);
test(0);
getch();
}
Output:
Revision Of Programs