0% found this document useful (0 votes)
51 views26 pages

MSC 1sem Lab A

This document contains a lab manual for the first semester of an M.Sc. in Computer Science. It includes documentation and source code for programs demonstrating default arguments, constructors and destructors, operator overloading using matrix addition, and single inheritance using a banking example. The programs include inputs, outputs, and step-by-step instructions for implementation.

Uploaded by

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

MSC 1sem Lab A

This document contains a lab manual for the first semester of an M.Sc. in Computer Science. It includes documentation and source code for programs demonstrating default arguments, constructors and destructors, operator overloading using matrix addition, and single inheritance using a banking example. The programs include inputs, outputs, and step-by-step instructions for implementation.

Uploaded by

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

LAB MANUAL

M.Sc. (Computer Science)

1st Semester
DEFAULT ARGUMENTS

1. To write a C++ program to demonstrate default arguments with a simple example.

i. OBJECTIVE:
To write a C++ program to demonstrate default arguments with a simple example.

ii. ALGORITHM:

STEP 1: Include the Header file.

STEP 2: Create a function which takes in three arguments of which one is a preset default argument.

STEP 3: Now call this function and while calling this pass in 2 arguments rather than 3.
STEP 4: The default argument is already set.
STEP 5: The function will execute without showing any error.

iii. SOURCE CODE :

#include<iostream.h>
int main()
{
float amount;
float value(float p,int n,float r=0.15);
amount=value(500.00,5); cout<<"\nFinal
Value="<<amount<<"\n";
amount=value(1000.00,5,0.30);
cout<<"\nFinal Value="<<amount<<"\n";
return 0;
}
float value(float p,int n,float r)
{
int year=1; float
sum=p;
while(year<=n)
{
sum*=(1+r);
year+=1;
}
return sum;
}

iv. SAMPLE INPUTS & OUTPUTS:

Final value=1005.68

Final value=3712.93
CONSTRUCTOR AND DESTRUCTOR (Dynamic memory allocation)

2. To write a C++ program to demonstrate the use of constructors and destructors.

i. OBJECTIVE:
To write a C++ program to demonstrate the use of constructors and destructors.

ii. ALGORITHM:
Step 1: Create a
class
Step 2: Create a constructor for this class which will set the value of its member variables.
Step 3: Create the other required methods for the given class
Step 4: Define the destructor.
Step 5: Create an object of this class
Step 6: The constructor will be called upon creation and when the program is ending the destructor will be
calle
d.

iii. SOURCE CODE :

#include<iostream.h
> class Arr
{
int
m,n;
int
**a;
public
:
Arr(int x,int y);
Arr(Arr
&c){a=c.a;m=c.m;n=c.n;} void
getd();
void
shd();
~Arr()
{delete
a;} };
Arr::Arr(int x,int y)
{
m=x;
n=y;
a=new int*[m];
for(int
i=0;i<m;i++)
a[i]=new int[n];
}

void Arr::getd()
{
int j;
cout<<"\nEnter the matrix elements:";
for(int i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
}
void Arr::shd()
{
int j;
cout<<"\n\nMatrix elements:\n";
for(int i=0;i<m;i++){
for(j=0;j<n;j++)
cout<<a[i][j]<<" ";
cout<<endl;}
}
int main()
{
int m,n;
cout<<"Enter the size of matrix needed:";
cin>>m>>n;
Arr t(m,n),s(t);
cout<<"By help of constructor:";
t.getd();
t.shd();
cout<<"By help of copy constructor:";
s.shd();
s.getd();
s.shd();
return 0;

iv. INPUTS & OUTPUTS: Enter the

size of matrix needed: 2 2 By help

of constructor:
Enter the matrix

elements: 1 2 3 4

Matrix Elements

1 2

3 4

By help of copy constructor:

Matrix Elements

1 2

3 4

1 2

3 4

Matrix Elements

2 2

3 4
OPERATOR OVERLOADING

3. To write a C++ program to illustrate the operator overloading concept using Matrix addition as an
example.

i. OBJECTIVE:
To write a C++ program to illustrate the operator overloading concept using Matrix addition as an
example.

ii. ALGORITHM:

Step 1: Create a class ’matrix’ with constructor that sets the value of its member variables.
Step 2: This class also overloads the + and = operator with proper operator overloaded functions.
Step 3: Define the operators.
Step 4: Create the object of this class and call these overloaded functions.

iii. SOURCE CODE :

#include<iostream.h>
class Matrix
{
public:
float row,col; float
**arr; Matrix();
Matrix(float,float);

int inputDim(float,float);
int getMatrix();
Matrix operator+(Matrix);
Matrix
operator=(Matrix&);
};
int Matrix::getMatrix()
{
for(int i=0;i<row;i++)

for(int j=0;j<col;j++)
cin>>arr[i][j];
return 0;
}
Matrix::Matrix(float r,float c)
{
row=r;
col=c;
arr=new float*[row];
for(int i=0;i<row;i++)
arr[i]=new float[col];
}
Matrix::Matrix()
{row=col=0;
arr=NULL;}
int Matrix::inputDim(float r,float c)
{
row=r;
col=c;
arr=new float*[row];
for(int i=0;i<row;i++)
arr[i]=new float[col];
return 0;

}
Matrix Matrix::operator+(Matrix m)
{
Matrix t;
t.inputDim(m.row,m.col);
for(int i=0;i<m.row;i++)
for(int j=0;j<m.col;j++)
t.arr[i][j]=arr[i][j]+m.arr[i][j];
return t;
}

Matrix Matrix::operator=(Matrix &m)


{
for(int i=0;i<m.row;i++)
for(int j=0;j<m.col;j++)
arr[i][j]=m.arr[i][j];
return *this;
}

int main()
{
int r,c;
int getDimension(int &r,int &c);
int showResult(Matrix);
cout<<"\nEnter the matrix size of A\n";
getDimension(r,c);
Matrix a(r,c);
cout<<"\nEnter the matrix A\n";
a.getMatrix();
cout<<"\nEnter the matrix size of B\n";
getDimension(r,c);
Matrix b(r,c);
cout<<"\nEnter the matrix B\n";
b.getMatrix();
if(a.row==b.row && a.col==b.col)
{
Matrix x(r,c);
x=a+b;
cout<<"\nMatrix A:\n";
showResult(a);
cout<<"\nMatrix B:\n";
showResult(b);
cout<<"\nResultant Matrix:\n";
showResult(x);
}
else
cout<<"\nAddition not possible\n";

return 0;
}
int getDimension(int &r,int &c)
{
cout<<"\nEnter row and column:\n";
cin>>r>>c;
return 0;
}
int showResult(Matrix x)
{
for(int i=0;i<x.row;i++){
for(int j=0;j<x.col;j++)
cout<<x.arr[i][j]<<" ";

cout<<"\n";}
return 0;
}
INPUTS & OUTPUTS: Enter the matrix

size of A:

Enter rows & columns 2 2

Enter the matrix A:

1 1 1 1

1 1 1 1

Matrix A: 1 1 1 1

Matrix B: 1 1 1 1

Resultant Matrix:

2 2

2 2

SINGLE INHERITANCE USING BANKING SYSTEM

4. To write a C++ program to illustrate the single inheritance using banking system as an example.

i. OBJECTIVE:
To write a C++ program to illustrate the single inheritance using banking system as an example.

ii. ALGORITHM:

STEP 1: Include the Header file.


STEP 2: Create a forward declaration of a class.
STEP 3:Create a new class that has certain member functions and methods.
STEP 4:Create the class earlier defined in step 2 and Create it inherit from the class in step 2 using the
resolution : operator
STEP 5: Now create an object of the sun class and Create it call some methods you defined in sgtep 2.

iii. SOURCE CODE :

#include<iostream.h
> class sav_acct
class account
{
char
cust_name[20]; int
acc_no;
char
acc_type[20];
public:
int get_accinfo()
{
cout<<"\n\nEnter customer name:-";
cin>>cust_name;
cout<<"\n\nEnter Account no:-";
cin>>acc_no;
cout<<"\n\nEnter Account type:-";
cin>>acc_type;
return 0;
}
int display_accinfo()
{
cout<<"\n\nCustomer Name:-"<<cust_name;
cout<<"\n\nAccount Number:-"<<acc_no;
cout<<"\n\nAccount Type:-"<<acc_type;
return 0;
}
};
class sav_acct:public account
{
static float savbal;
public:
int disp_savbal()
{
cout<<"\nBalance:-"<<savbal;
return 0;
}
int deposit_savbal()
{
float deposit,interest;
cout<<"\n Enter the amount to Deposit:-";
cin>>deposit;
savbal=savbal+deposit;
interest=(savbal*2)/100;
savbal=savbal+interest;
return 0;
}
void withdraw_savbal()
{
float withdraw;
cout<<"\nBalance:-"<<savbal;
cout<<"\nEnter amount to be withdrawn:-";
cin>>withdraw;
savbal-=withdraw;
if(withdraw>savbal)
{
cout<<"\n\nYou have to take permission for Bank overdraft Facility\n";

savbal+=withdraw;
}
else
cout<<"\nAfter withdrawl your Balance reveals:"<<savbal;
}
};
float sav_acct::savbal;
int main()
{
sav_acct s1;
int choice;
s1.get_accinfo();
while(1)
{
cout<<"\nchoose your choice\n";
cout<<"\n1)Deposit\n";
cout<<"\n2)Withdraw\n";
cout<<"\n3)Display Balance\n";
cout<<"\n4)Display with full details\n";
cout<<"\n5)Exit\n";
cout<<"Enter your choice:-";
cin>>choice;
switch(choice)
{
case 1:s1.deposit_savbal();
break;
case 2:s1.withdraw_savbal();
break;
case 3:s1.disp_savbal();
break;
case 4:s1.display_accinfo();
s1.disp_savbal();
break;
case 5:return(0);
default:cout<<"\n\nEntered choice is invalid";
}
}

return 0;
}

INPUTS & OUTPUTS: Enter

customer name: Ajai

Enter account No: 123

Enter account type: Savings

Choose your choice

1) Deposit

2) withdraw

3) display Balance

4) Display with full details

5) Exit

Enter your Choice: 1

Enter the amount to Deposit 500

Enter your Choice: 1

Balance:- 510

Enter your Choice: 5

HYBRID INHERITANCE USING STUDENT DATABASE

5. To write a C++ program to illustrate hybrid inheritance concept using student database creation as an
example.
i. OBJECTIVE:
To write a C++ program to illustrate hybrid inheritance concept using student database creation as
an example.

ii. ALGORITHM:

Step 1:Create a class with some methods and variables.


Step 2:Create a new class that extends this class.
Step 3:Create a new class with some new methods.
Step 4:Create a new class again which inherits from classes defined in step 2 and step 3.
Step 5.Create an object of class defined in step 4 and Create it call methods defined in step 1 and step 3.

iii. SOURCE CODE :

#include<iostream.h
> class tests;
class sports;
class result;
class Student{
protected :int rn;
public:
void get()
{cout<<"\n\nEnter roll
no.:"; cin>>rn;}
void put()
{cout<<"\nRoll no.:"<<rn<<endl;

}
};
class tests:public Student
{
protected:
float sub1,sub2;
public:
void get_m()
{cout<<"\nenter marks:";
cin>>sub1>>sub2;}
void put_m()
{
cout<<"\nMarks in subject 1="<<sub1<<endl;
cout<<"\nMarks in subject 2="<<sub2<<endl;
}
};
class sports
{
protected:
char g;
public:
void get_g()
{
cout<<"Enter the grade:";
cin>>g;
}
void put_g()
{
cout<<"\nGrade in sports:"<<g<<endl;
}
};
class result:private tests,private sports
{
public:
void res()
{
get();
get_m();
get_g();
cout<<"\n------\nRESULT\n------\n";
put();
put_m();

put_g();
float total=sub1+sub2;
cout<<"TOTAL="<<total;
}
};
void main()
{
result s1;
s1.res();
}

INPUTS & OUTPUTS: Enter roll

no.:100

Enter Marks: 89 90

Enter the Grade: A

Result
roll no.:100

Marks in subject 1=89

Marks in subject 2=90

Grade in sports: A

Total= 179

VIRTUAL FUNCTION

6. To write a C++ program to illustrate virtual function implementation.

i. OBJECTIVE:
To write a C++ program to illustrate virtual function implementation.

ii. ALGORITHM:

Step 1:Create a class base with a method marked as virtual.


Step 2:Create a subclass of base called derived and redefine the method defined in step 1.
Step 3:Create an object pointer of type base and Create it point to an object of type base first and call
the methods you had marked as virtual using -> operator.
Step 4: repeat the same steps but by making the pointer point to an object of derived type now.

iii. SOURCE CODE :

#include<iostream.h
> class base
{
public:
void display()
{cout<<"\nDisplay base";}
virtual void show(){cout<<"\nShow
base.";} };
class derived:public base
{
public:

void display(){cout<<"\nDisplay derived";}


void show(){cout<<"\nShow Derived.";}
};
int main()
{
base b;
derived d;
base *bptr;
bptr=&b;
cout<<"\nbptr points to base\n";
bptr->display();
bptr->show();
cout<<"\n\nbptr points to derived.";
bptr=&d;
bptr->display();
bptr->show();
return 0;
}

INPUTS & OUTPUTS:

bptr points to base

Show base

Display base

bptr points to derived

Show derived

Display derived

VIRTUAL BASE CLASS

7. To write a C++ program to illustrate virtual base class implementation.


DYNAMIC POLYMORPHISM

8. To write a C++ program to illustrate dynamic polymorphism using different shapes as an example.

i. OBJECTIVE:
To write a C++ program to illustrate dynamic polymorphism using different shapes as an example.

ii. ALGORITHM:

Step 1:Make a class area which has a method.


Step 2:Make subclasses to this which have constructors that will take in the value for the
required fields and will compute the area and put it in one of the member variables.
Step 3:call the method defined in step 1, which is overridden in all the subclasses to show
their respective areas .

iii. SOURCE CODE :

#include<iostream.h>
class Circle;
class Rectangle;
class Square;
class Triangle;
class Trapezium;
class Area
{
protected:
float area;
public:
Area(float a)
{
area=a;
}
virtual void display(){}
};
class Circle:public Area
{
protected:
float ar;
public:
Circle(float a):Area( a)
{ar=3.14*a*a;}
void display()
{
cout<<"Area of Circle="<<ar;
}
};
class Rectangle:public Area
{
protected:
float ar;
public:
Rectangle(float a,float b):Area( a)
{ar=a*b;}
void display()
{
cout<<"Area of Rectangle="<<ar;
}
};
class Square:public Area
{
protected:
float ar;
public:
Square(float a):Area( a)
{ar=a*a;}
void display()
{

cout<<"Area of Square="<<ar;
}
};
class Triangle:public Area
{
protected:
float ar;
public:
Triangle(float a,float b):Area( a)
{ar=0.5*a*b;}
void display()
{
cout<<"Area of Triangle="<<ar;
}
};
class Trapezium:public Area
{
protected:
float ar;
public:
Trapezium(float a,float b):Area( a)
{ar=0.5*(a+b);}
void display()
{
cout<<"Area of Trapezium="<<ar;
}
};
int main()
{
cout<<"\nEnter radius of circle:";
int ra;
cin>>ra;
Circle c(ra);

cout<<"\nEnter length and breadth of rectangle:";


int a,b;
cin>>a>>b;
Rectangle r(a,b);

cout<<"Enter side of square:";


int sq;
cin>>sq;

Square s(sq);

cout<<"Enter height and base of circle:";


int h,ba;
cin>>h>>ba;
Triangle t(h,ba);

cout<<"Enter parallel sides's length of trapezium:";


int p,pt;
cin>>p>>pt;
Trapezium tr(p,pt);
Area *bptr[5];
bptr[0]=&c;
bptr[1]=&r;
bptr[2]=&s;
bptr[3]=&t;
bptr[4]=&tr;
bptr[0]->display();
bptr[1]->display();
bptr[2]->display();
bptr[3]->display();
bptr[4]->display();
return 0;
}
INPUTS & OUTPUTS:

Enter radius of circle: 2


Enter length and breadth of rectangle: 4 6
Enter side of square: 2
Enter height and base of circle: 3 2
Enter parallel sides's length of trapezium: 3 3

Area of circle=12.56

Area of rectangle=24

Area of square=4 Area

of triangle=3 Area of

Trapezium=1.5

EXCEPTION HANDLING IN STACKS

9. To write a C++ program to illustrate exception handling concept using stack operation as an example.

i. OBJECTIVE:
To write a C++ program to illustrate exception handling concept using stack operation as an
example.

ii. ALGORITHM:

STEP 1: Include the Header file.


STEP 2: Create a class Stack which initialize size of the stack
STEP 3: Create function push to do insert operation which in turn throws an exception when it overflow.
STEP 4: Create function pop to do delete operation which in turn throws an exception when
it underflow.
STEP 5: Display the operation on console using display function.
iii. SOURCE CODE :

#include<iostream.h
> class Stack
{
public:
int size;
int top;
int *a;
Stack(int sz)
{
size=sz;
a=new int[sz];
top =-1;
}
void push(int c)
{
if(top==size-1){
throw top;
}
else
{
a[++top]=c;
}
}
void pop()
{
if(top==-1)
throw top;
else
cout<<"\n"<<a[top--]<<" removed\n";
}
void display();
};
void Stack::display()
{
cout<<"\nStack:\n";
for(int i=0 ;i<=top;i++)
cout<<a[i]<<" ";
}
int main()
{
int sz,x,ch;
cout<<"\nEnter stack size:";
cin>>sz;
Stack s(sz);

try
{

do
{
cout<<"\nEnter choice:\n1 for push\n2 for pop\n3 for display\n4 to exit\n";
cin>>ch;
switch(ch)
{
case 1:cout<<"\nEnter a no.:";
cin >>x;
s.push(x);
break;
case 2:s.pop();
break;
case 3:s.display();
break;
case 4:return 0;
break;
default:
cout<<"Enter a valid choice.";
}
}while(1);
}
catch(int a)
{
if(a==s.size-1)
cout<<"\nOverflow\n";
else
cout<<"\nUnderflow";
}

return 0;
}

INPUTS & OUTPUTS: Enter the size of


stack: 3

Enter choice:
1 for push
2 for pop
3 for display
4 to exit
1
Enter a no 2
1
Enter a no 3
3
3
2
2
Removed
3
2
EXCEPTION HANDLING IN QUEUES

10. To write a C++ program to illustrate exception handling concept using queue operation as an
example.

i. OBJECTIVE:
To write a C++ program to illustrate exception handling concept using queue operation as
an example.
ii. ALGORITHM:

STEP 1: Include the Header file.


STEP 2: Create a class Stack which initialize size of the stack
STEP 3: Create function push to do insert operation which in turn throws an exception when it
overflow.
STEP 4: Create function pop to do delete operation which in turn throws an exception when it
undererflow.
STEP 5: Display the operation on console using display function.

iii. SOURCE CODE :


#include<iostream.h
> class queue
{

public:
int size;
int front ,rear;
int *a;
queue(int sz)
{
size=sz;
a=new int[sz];
front =-1;
rear=-1;
}
void insert(int c)
{
if(rear==size-1){
throw rear;
}
else
{
a[++rear]=c;
}
}
void remove()
{
if(front==rear)
throw 'u';
else
cout<<"\n"<<a[++front]<<" removed\n";
}
void display();
};
void queue::display()
{
cbout<<"\nQueue:\n";
for(int i=front + 1 ;i<=rear;i++)
cobut<<a[i]<<" ";
}
int main()
{
int sz,x,ch;
cout<<"\nEnter queue size:";
cin>>sz;
queue s(sz);

try
{
do
{
cout<<"\nEnter choice:\n1 for insert\n2 for remove\n3 for display\n4 to exit\n";
cin>>ch;
switch(ch)
{
case 1:cout<<"\nEnter a no.:";
cin >>x;
s.insert(x);
break;
case 2:s.remove();
break;
case 3:s.display();
break;
case 4:return 0;
break;
default:
cout<<"Enter a valid choice.";
}
}while(1);
}
catch(int a)
{
if(a==s.size-1)
cout<<"\nOverflow\n";
}
catch(char c)
{
if(c=='u')
cout<<"\nUnderflow.";
}
return 0;
}

INPUTS & OUTPUTS: Enter the size of


queue: 3

Enter choice:
1 for insert

2 for delete
3 for display
4 to exit
1
Enter a no 2
1
Enter a no 3
3232
Removed 2
2

You might also like