Labmanual
Labmanual
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
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.
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
LAB#01
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
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
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
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
LAB#05
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:
}
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:
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
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
obj.assign();
obj.compute();
obj.display();
return 0;
}
Output:
LAB#06
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:
}
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
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:
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
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
{
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
#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:
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:
LAB#10
Derived ( )
{
cout<< "Inside Derived constructor" <<endl;
}
~Derived ( )
{
cout<< "Inside Derived destructor" <<endl;
}
};
int main( )
{
Derived x;
}
Output:
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
{
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
{
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:
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()
{