You are on page 1of 27

Practical 1

Aim: Write a program to demonstrate the difference between Macro and inline
function.
#include<iostream>
using namespace std;
inline int in(int a, int b)
{
return a+b;
}
int main()
{
int a,b;
cout<<"Enter a";
cin>>a;
cout<<"Enter b";
cin>>b;
cout<<in(a,b);
}

o/p:
Enter a10
Enter b20
30
Practical 2
Aim: Implement a program to demonstrate Default Argument.
#include<iostream>
using namespace std;
int sum(int a, int b)
{
return a+b;
}
int main()
{
cout<<"Sum = "<<sum(10,20);
}

o/p:
Sum = 30
Practical 3
Aim: Write a c++ program to demonstrate use of Bool datatype.
#include<iostream>
using namespace std;
bool odd_eve(int x)
{
if(x%2==0)
{
return true;
}
else
{
return false;
}
}
int main()
{
int a;
cout<<"enter No =";
cin>>a;
if(odd_eve(a))
{
cout<<"Even";
}
else
{
cout<<"Odd";
}
}

o/p:
enter No =20
Even
Practical 4
Aim: Write C++ program to compare the individual elements of two arrays
using bool data type.
#include<iostream>
using namespace std;
bool com(int a, int b)
{
if(a==b)
{
return true;
}
else
{
return false;
}
}
int main()
{
int a[10],b[10],n,i;
cout<<"Enter No. of Element Of Array = ";
cin>>n;
cout<<"Enter element in first array";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter element in second array";
for(i=0;i<n;i++)
{
cin>>b[i];
}
cout<<"\n\n\n";
for(i=0;i<n;i++)
{
if(com(a[i],b[i]))
{
cout<<a[i]<<" and "<<b[i]<<"are same";
}
else
{
cout<<a[i]<<" and "<<b[i]<<"are not same";
}
cout<<"\n";
}
}
o/p:
Enter No. of Element Of Array = 2
Enter element in first array10
2
Enter element in second array10
3

10 and 10are same


2 and 3are not same
Practical 5
Aim: Write a c++ program to demonstrate use of enum datatype.
#include<iostream>
using namespace std;
enum od_ev {even,odd};
int iseven(int n)
{
if(n%2==0)
{
return odd;
}
else
{
return even;
}

}
int main()
{
int n;
cout<<"Enter No. = ";
cin>>n;
if(iseven(n))
{
cout<<"even";
}
else
{
cout<<"odd";
}
}

o/p:
Enter No. = 1
odd
Practical 6
Aim: Write a C++ program to illustrate the use of wchar_t data type.
#include<iostream>
using namespace std;
int main()
{
wchar_t p=L'b';
char b='b';
cout<<"="<<sizeof(b)<<sizeof(p);
}

o/p:
=14
Practical 7
Aim: Create a strucutre called ITEM that has data for item number(int) and
item cost(float). Call following functions (1) Using global structure varibale (2)
Using Local structure variable.
#include<iostream>
#define t 0.18;
using namespace std;
struct str
{
int number;
float cost,c;
}s;
class item
{
public:
float c;

void satdata()
{

c=s.cost*s.number;
c=c+c*0.18;

}
void getdata()
{
cout<<"\nEnter number of product=";
cin>>s.number;
cout<<"\nenter cost of product=";
cin>>s.cost;
}
void putdata()
{
cout<<"total cost+"<<c;
}
};
int main()
{
item a1;
a1.getdata();
a1.satdata();
a1.putdata();

}
o/p:
Enter number of product=2

enter cost of product=10


total cost+23.6
Practical 8
Aim: Writa a C++ program using class which contains two member functions
getdata and putdata to read the information(name and age) of the person and
display it on the screen respectively. Use Scope Resolution Operator.
#include<iostream>
#define t 0.18;
using namespace std;
class item
{
public:
int age;
char name[10];

void getdata()
{

cout<<"\nEnter Employee Name=";


cin>>name;
cout<<"\nenter Age of Employee=";
cin>>age;
}
void putdata();
};
void item :: putdata()
{
cout<<"\nEmployee Name ="<<name<<"\nEmployee Age
="<<age<<endl;
}
int main()
{
item a1[10];
int i,n;
cout<<"\nEnter number of Employee =";
cin>>n;
for(i=0;i<n;i++)
{
a1[i].getdata();
}
for(i=0;i<n;i++)
{
a1[i].putdata();
}
}
o/p:
Enter number of Employee =2

Enter Employee Name=arjun

enter Age of Employee=19

Enter Employee Name=chandresh

enter Age of Employee=20

Employee Name =arjun


Employee Age =19

Employee Name =chandresh


Employee Age =20
Practical 9
Aim: Write a C++ program using class Rectangle which contains length and
width as data members and two member functions – getdata and area. The
getdata function takes length and width as an input and the area function
calculates the area and display it on the screen. Use Scope Resolution Operator
#include<iostream>
using namespace std;
class size
{
public:
int v;
int l,b;
void getdata()
{
cout<<"Enter Length of cylinder= ";
cin>>l;
cout<<"Enter Width of cylinder= ";
cin>>b;
}
int area();
};

int size::area()
{
v=l*b;
return v;
}
int main()
{
size s;
int vol;
s.getdata();
vol=s.area();

cout<<"volume = "<<vol;

o/p:
Enter Length of cylinder= 3
Enter Width of cylinder= 4
volume = 12
Practical 10
Aim: Write a class to create array objects with given size. Write setval()
function to get values from user and putval() function to print values. And
demonstrate in main function.
#include<iostream>
#define t 0.18;
using namespace std;
class item
{
public:
int age;
char name[10];

void getdata()
{

cout<<"\nEnter Employee Name=";


cin>>name;
cout<<"\nenter Age of Employee=";
cin>>age;
}
void putdata();
};
void item :: putdata()
{
cout<<"\nEmployee Name ="<<name<<"\nEmployee Age
="<<age<<endl;
}
int main()
{
item a1[10];
int i,n;
cout<<"\nEnter number of Employee =";
cin>>n;
for(i=0;i<n;i++)
{
a1[i].getdata();
}
for(i=0;i<n;i++)
{
a1[i].putdata();
}
}
o/p:

Enter number of Employee =2

Enter Employee Name=arjun

enter Age of Employee=20

Enter Employee Name=chandresh

enter Age of Employee=19

Employee Name =arjun


Employee Age =20

Employee Name =chandresh


Employee Age =19
Practical 11
Aim: Define a class to represent a bank account. include the following members: data
members 1) name of the depositor 2) account number 3) type of account 4) balance amount in
the account member functions: 1) to assign initial value 2) to deposite an amount 3) to
withdraw an amount after checking the balance 4) to display name and balance write a main
program to test the program.

#include<iostream>
using namespace std;

class bank
{
public:
int sum;
int a,de;

void init_val()
{
cout<<"Enter the initial Value = ";
cin>>sum;
}
void deposit()
{
cout<<"enter Deposit Amount = ";
cin>>de;
sum=sum+de;
}
void withdraw()
{
cout<<"Enter withdraw amount = ";
cin>>de;
sum=sum-de;
}
void display()
{
cout<<"Your presenr Balance = "<<sum;
}
};
int main()
{
bank b;
char name[10],ac_type[10];
int ac_no,n;
cout<<" Welcome = ";
cout<<"Enter Your name = ";
cin>>name;
cout<<"Enter Your Account No. = ";
cin>>ac_no;
cout<<"Enter Your Account Type = ";
cin>>ac_type;
b.init_val();
cout<<"1. Deposit \n2. Withdraw \n3.Display Balance \n4.
Exit";
cout<<"\nEnter Your Choice = ";
cin>>n;
while(n<4)
{
switch(n)
{
case 1:
{
b.deposit();
break;
}
case 2:
{
b.withdraw();
break;
}
case 3:
{
b.display();
break;
}
}
cout<<"\n1. Deposit \n2. Withdraw \n3.Display
Balance \n4. Exit";
cout<<"\nEnter Your Choice = ";
cin>>n;
}
}

o/p:
Welcome = Enter Your name = hello
Enter Your Account No. = 985525
Enter Your Account Type = saving
Enter the initial Value = 10000
1. Deposit
2. Withdraw
3.Display Balance
4. Exit
Enter Your Choice = 1
enter Deposit Amount = 2000

1. Deposit
2. Withdraw
3.Display Balance
4. Exit
Enter Your Choice = 3
Your presenr Balance = 12000
1. Deposit
2. Withdraw
3.Display Balance
4. Exit
Enter Your Choice = 4
Practical 12
Aim: Modify program 1. for handling 10 customers.
#include<iostream>
using namespace std;

class bank
{
public:
int sum;
int a,de;

void init_val()
{
cout<<"Enter the initial Value = ";
cin>>sum;
}
void deposit()
{
cout<<"enter Deposit Amount = ";
cin>>de;
sum=sum+de;
}
void withdraw()
{
cout<<"Enter withdraw amount = ";
cin>>de;
sum=sum-de;
}
void display()
{
cout<<"Your presenr Balance = "<<sum;
}
};
int main()
{
bank b[10];
char name[10],ac_type[10];
int ac_no,n,i;
for(i=0;i<10;i++)
{
cout<<" ||| Welcome |||\n\n ";
cout<<"Enter Your name = ";
cin>>name;
cout<<"Enter Your Account No. = ";
cin>>ac_no;
cout<<"Enter Your Account Type = ";
cin>>ac_type;
b[i].init_val();
cout<<"1. Deposit \n2. Withdraw \n3.Display Balance \n4.
Exit";
cout<<"\nEnter Your Choice = ";
cin>>n;

while(n<4)
{
switch(n)
{
case 1:
{
b[i].deposit();
break;
}
case 2:
{
b[i].withdraw();
break;
}
case 3:
{
b[i].display();
break;
}
}
cout<<"\n1. Deposit \n2. Withdraw \n3.Display
Balance \n4. Exit";
cout<<"\nEnter Your Choice = ";
cin>>n;
}
}
}

o/p:
Welcome = Enter Your name = hello
Enter Your Account No. = 8976231
Enter Your Account Type = saving
Enter the initial Value = 13000
1. Deposit
2. Withdraw
3.Display Balance
4. Exit
Enter Your Choice = 1
enter Deposit Amount = 5000

1. Deposit
2. Withdraw
3.Display Balance
4. Exit
Enter Your Choice = 3
Your presenr Balance = 18000
1. Deposit
2. Withdraw
3.Display Balance
4. Exit
Enter Your Choice = 4
Practical 13
Aim : Create a class called Distance that has separate member data for feet(int)
and inches(float).Include the following member functions: • setdist( )to set these
values to predefined values in the program • getdist( )to get these values from
the user • showdist( ) to display these values in the format 5’-07 ”. 2. Member
function add_dist( ) to add two Distance objects to a third Distance object (e.g.
d3.add_dist(d1,d2). Define this member function outside the class. 3. Make new
function to return a Distance object, so that the function works as follows:
d3=d1.add_dist(d2).
#include<iostream>
using namespace std;
class distance
{
public:
float i,f;

void setdist(){
if(i>=12){

i=i-12;
f++;

}
}
void getdist(){
cout<<"Enter feet = ";
cin>>f;
cout<<"Enter Inch = ";
cin>>i;
while(i>=12){
cout<<"Enter Proper input(inch)=";
getdist();
}
}

void add_dist(distance d1,distance d2){

i=d1.i+d2.i;
f=d1.f+d2.f;

}
distance add_dist(distance d2){
distance d3;
d3.i=i+d2.i;
d3.f=f+d2.f;
return d3;
}
void showdist(){
cout<<"Total="<<f<<"'"<<i<<"\"\n";
}

};
int main()
{
class distance d1,d2,d3;

d1.getdist();
d2.getdist();
d3.add_dist(d1,d2);
d3.setdist();
d3.showdist();
d3=d1.add_dist(d2);
d3.setdist();
cout<<"Total="<<d3.f<<"'"<<d3.i<<"\"\n";

return 0;

o/p:
Enter feet = 2.6
Enter Inch = 1.2
Enter feet = 3
Enter Inch = 5
Total=5.6'6.2"
Total=5.6'6.2"
Practical 14
Aim: Create a Class for matrix representation of m*n size. Create following
member functions. • read matrix • display matrix • add two matrices • multiply
two matrices • find transpose of matrix ( i.e. m2= m1.transpos())
#include<iostream>
using namespace std;
int m,n;
class matrix
{
int i,j,k,a[10][10],sum;
public:
void read(){
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
}
void display(){
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
}
void add(matrix m1,matrix m2){
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=m1.a[i][j] + m2.a[i][j];
}
}
}
void mult(matrix m1,matrix m2){
sum=0;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
for (k = 0; k < m; k++)
{
sum = sum +
m1.a[i][k]*m2.a[k][j];
}

a[i][j] = sum;
sum = 0;
}
}

}
void transpose(){
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<a[j][i]<<" ";
}
cout<<"\n";
}
}
};
int main()
{
cout<<"ENter size of matrix = ";
cin>>m>>n;
matrix m1,m2,m3,m4;
cout<<"ENter matrix 1 = \n";
m1.read();
cout<<"ENter matrix 2 = \n";
m2.read();
cout<<"Sum\n";
m3.add(m1,m2);
m3.display();
cout<<"Multiplication = \n";
m4.mult(m1,m2);
m4.display();
cout<<"Transpose = \n";
m1.transpose();

return 0;

}
o/p:
ENter size of matrix = 2 2
ENter matrix 1 =
1 3
2 4
ENter matrix 2 =
1 2
4 6
Sum
2 5
6 10
Multiplication =
13 20
18 28
Transpose =
1 2
3 4
Practical 15
Aim: A Team has a following table of batting figure for the series of test
matche Write a program to read the figures set out in above form and calculate
the average for each player and add that in the table too. And print out the
complete table.
#include<iostream>
using namespace std;
int n;
class player
{
char name[10];
int run,in,nout,avg;
public:
void getdata()
{
cout<<"Enter name = ";
cin>>name;
cout<<"Enter total runs = ";
cin>>run;
cout<<"Enter no of Innings = ";
cin>>in;
cout<<"Enter No. of Not Outs = ";
cin>>nout;
}
void setdata()
{
avg=(run)/(in-nout);

}
void display()
{
{

cout<<name<<"\t\t"<<run<<"\t"<<in<<"\t"<<nout<<"\t\t"<<av
g<<"\n";
}

}p[10];
int main()
{
int i;
cout<<"Enter No. of player = \n";
cin>>n;
for(i=0;i<n;i++)
{
p[i].getdata();
p[i].setdata();
}
cout<<"Player Name\tRuns\tInnings\tTimes Not
Out\tAverage\n";
for(i=0;i<n;i++)
{
p[i].display();
}
return 0;
}

o/p:
Enter No. of player =
2
Enter name = arjun
Enter total runs = 89
Enter no of Innings = 10
Enter No. of Not Outs = 2
Enter name = chandresh
Enter total runs = 80
Enter no of Innings = 12
Enter No. of Not Outs = 3
Player Name Runs Innings Times Not Out Average
arjun 89 10 2 11
chandresh 80 12 3 8

You might also like