MSC 1sem Lab A
MSC 1sem Lab A
1st Semester
DEFAULT ARGUMENTS
i. OBJECTIVE:
To write a C++ program to demonstrate default arguments with a simple example.
ii. ALGORITHM:
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.
#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;
}
Final value=1005.68
Final value=3712.93
CONSTRUCTOR AND DESTRUCTOR (Dynamic memory allocation)
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.
#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;
of constructor:
Enter the matrix
elements: 1 2 3 4
Matrix Elements
1 2
3 4
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.
#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;
}
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:
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
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:
#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;
}
1) Deposit
2) withdraw
3) display Balance
5) Exit
Balance:- 510
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:
#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();
}
no.:100
Enter Marks: 89 90
Result
roll no.:100
Grade in sports: A
Total= 179
VIRTUAL FUNCTION
i. OBJECTIVE:
To write a C++ program to illustrate virtual function implementation.
ii. ALGORITHM:
#include<iostream.h
> class base
{
public:
void display()
{cout<<"\nDisplay base";}
virtual void show(){cout<<"\nShow
base.";} };
class derived:public base
{
public:
Show base
Display base
Show derived
Display derived
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:
#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);
Square s(sq);
Area of circle=12.56
Area of rectangle=24
of triangle=3 Area of
Trapezium=1.5
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:
#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;
}
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:
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;
}
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