C++ Programming
Slip29
by using function template. [Marks 15]
Solution
#include<iostream.h>
#include<conio.h>
template<class T>
T max(T x,T y)
{
if(x>y)
return x;
else
return y;
}
int main()
{ clrscr();
int a,b;
float p,q;
cout<<"Enter two integer value :";
cin>>a>>b;
cout<<"Enter two float value :";
cin>>p>>q;
cout<<"---------------------------"<<endl;
cout<<"maximum no="<<max(a,b)<<endl;
cout<<"maximum no="<<max(p,q)<<endl;
cout<<"---------------------------"<<endl;
getch();
return 0;
}
Output
Enter two integer value :
5
4
Enter two float value :
9.3
8.5
---------------------------
maximum no=5
maximum no=9.3
---------------------------
Q.2) Create a class for inventory of Mobile containing Model_Number, Company_Name,
Price, Stock as data members. Mobile can be sold, if stock is available, otherwise
purchase will be made. Write a C++ program to perform following functions :
i. To accept and display mobile details
ii. To sale a Mobile (Sale contains Model_Number & Quantity of mobile)
iii. To Purchase a Mobile (Purchase contains Model_Numbet & Quantity of
mobile) [Marks 25]
Solution
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class inventory
{
int m_no,stock;
char m_name[20];
float price;
public:
void accept()
{
cout<<"\n\t Enter model no:";
cin>>m_no;
cout<<"\n\t Enter company name:";
cin>>m_name;
cout<<"\n\t Enter price :";
cin>>price;
cout<<"\n\t Enter initial stock:";
cin>>stock;
}
void setting()
{
cout<<"\n"<<setw(12)<< "Model_No";
cout<<setw(15)<<"Company_Name"<<setw(12)<< "Price";
cout<<setw(12)<<"Stock";
}
void display()
{
cout<<"\n"<<setw(12)<<m_no;
cout<<" "<<setw(15)<<m_name;
cout<<" "<<setw(10)<<price;
cout<<" "<<setw(10)<<stock;
}
friend void purchase(inventory *,int);
friend void sale(inventory *,int);
};
void purchase(inventory *a,int n)
{
cout<<"\n\t Enter model no and stock:";
int m,s;
cin>>m>>s;
for(int i=0;i<n;i++)
{
if(a[i].m_no == m)
break;
}
if(i == n)
{
cout<<"\n\t Model no not found";
return ;
}
a[i].stock = a[i].stock + s;
cout<<"\n\t Stock updated";
a[i].setting();
for(i =0; i<n;i++)
a[i].display();
}
void sale(inventory *a,int n)
{
cout<<"\n\t Enter model no and qty:";
int m,q;
cin>>m>>q;
for(int i=0;i<n;i++)
{
if(a[i].m_no == m)
break;
}
if(i == n)
{
cout<<"\n\t Model no not found";
return;
}
if(a[i].stock < q)
{
cout<<"\n\tstock insufficient";
return;
}
cout<<"\n\t Stock available :";
cout<<a[i].m_no<<" "<<a[i].m_name<<" Price/model :"
<<a[i].price;
cout<<"\n\t Quantity :"<<q<<" Total Rs:"<< a[i].price * q;
cout<<"\n\tSold";
a[i].stock = a[i].stock - q;
a[i].setting();
for(i=0;i<n;i++)
a[i].display();
}
int main()
{
inventory a[10];
int n = 0;
int i,ch;
do
{
cout<<"\n\t 1.To accept and display the stock:";
cout<<"\n\t 2.To purchase more stock";
cout<<"\n\t 3.To sale stock";
cout<<"\n\t 4.Exit";
cout<<"\n\t Enter your choice :";
cin>>ch;
switch(ch)
{
case 1:
if(n == 10)
{
cout<<"\n\t Capacity full";
break;
}
a[n].accept();
n++;
a[0].setting();
for(i=0;i<n;i++)
a[i].display();
break;
case 2:
purchase(a,n);
break;
case 3:
sale(a,n);
break;
case 4:
break;
default:
cout<<"\n\t Wrong choice";
}
}while(ch != 4);
getch();
return 0;
}
Output
1.To accept and display the stock:
2.To purchase more stock
3.To sale stock
4.Exit
Enter your choice :1
Enter model no:71
Enter company name:nokia
Enter price :15000
Enter initial stock:50
Model_No Company_Name Price Stock
71 nokia 15000 50
1.To accept and display the stock:
2.To purchase more stock
3.To sale stock
4.Exit
Enter your choice :2
Enter model no and stock:72 75
Model no not found
1.To accept and display the stock:
2.To purchase more stock
3.To sale stock
4.Exit
Enter your choice :3
Enter model no and qty:71 65
Model no not found
1.To accept and display the stock:
2.To purchase more stock
3.To sale stock
4.Exit
Enter your choice :4
Tags:
c++