Saturday, 20 January 2018

CPP-Slip29



Slip29
Q.1)     Write a C++ program to find maximum of  two integer numbers and two float numbers by using function template.
                     #include<iostream.h>
#include<conio.h>
template<class T>
int max(T a, T b)
{
if(a>b)
return(a);
else
return(b);
}
void main()
{
int i1,i2;
float f1,f2;
clrscr();
cout<<"Enter two integers:\t";
cin>>i1>>i2;
cout<<"\nEnter two floats:\t";
cin>>f1>>f2;
cout<<"\nMax of two integers:\t"<<max(i1,i2);
cout<<"\nMax of two floats:\t"<<max(f1,f2);
getch();
}                                 

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)


#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

class inventory

{

int modelnum,price,stock;

char company[10];

public:

void getput()

{

cout<<"\n\nEnter model number:\t";

cin>>modelnum;

cout<<"\nEnter mobile company name:\t";

cin>>company;

cout<<"\nEnter price:\t";

cin>>price;

cout<<"\nEnter stock:\t";

cin>>stock;

cout<<"\n\n1.Model number\t"<<modelnum;

cout<<"\n2.Company name\t\t"<<company;

cout<<"\n3.Price\t"<<price;

cout<<"\n4.Stock\t"<<stock;

}

void sale()

{

int nosale;

cout<<"\n\nModel number\t"<<modelnum;

cout<<"\nStock\t"<<stock;

cout<<"\nEnter quantity to sale\t";

cin>>nosale;

stock=stock-nosale;

cout<<"\nStock remaining\t"<<stock;

}

void purchase()

{

int nopurchase;

cout<<"\n\nModel number\t"<<modelnum;

cout<<"\nStock\t"<<stock;

cout<<"\nEnter quantity to purchase\t";

cin>>nopurchase;

stock=stock-nopurchase;

cout<<"\nStock remaining\t"<<stock;

}

};

void main()

{

int i,n,ch;

inventory in[30];

clrscr();

do

{

cout<<"\n\n1.Accept and display details\t";

cout<<"\n2.To sale mobile\t";

cout<<"\n3.To purchase mobile\t";

cout<<"\n4.Exit\t";

cout<<"\n\nEnter choice\t";

cin>>ch;

switch(ch)

{

case 1:

cout<<"How many records you want to enter:\t";

cin>>n;

for(i=0;i<n;i++)

in[i].getput();

break;

case 2:

for(i=0;i<n;i++)

in[i].sale();

break;

case 3:

for(i=0;i<n;i++)

in[i].purchase();

break;

case 4:

exit(0);

default:

cout<<"Wrong choice";

}

}while(ch!=4);

getch();

}      

 

 

Another way

 

 

#include<iostream.h>

 

#include<conio.h>

#include<stdlib.h>

class Inventory

{

int Model_Number;

char Company_name[20];

int price;

int stock;

public:

void accept(void);

void sale(void);

void purchase(void);

} ;

 

void Inventory::accept(void)

{

cout<<"\nEnter the Model Number of Mobile:";

cin>>Model_Number;

cout<<"\nEnter the name of the Mobile Company:";

cin>>Company_name;

cout<<"\nEnter the price of the Mobile:";

cin>>price;

cout<<"\nEnter the Quantity of the Mobile:";

cin>>stock;

}

 

void Inventory::sale(void)

{

int no_of_sale;

cout<<"\nModel Number:"<<Model_Number;

cout<<"\nCompany Name:"<<Company_name;

cout<<"\nPrice:"<<price;

cout<<"\nStock:"<<stock;

cout<<"\nEnter the quantity to sale:";

cin>>no_of_sale;

stock=stock-no_of_sale;

cout<<"\nNumber of stocks Remaining:"<<stock;

}

 

void Inventory::purchase(void)

{

int no_of_purchase;

cout<<"\nModel Number:"<<Model_Number;

cout<<"\nCompany Name:"<<Company_name;

cout<<"\nPrice:"<<price;

cout<<"\nStock:"<<stock;

cout<<"\nEnter the quantity to purchase:";

cin>>no_of_purchase;

stock=stock+no_of_purchase;

cout<<"\nNumber of Stocks Remaining:"<<stock;

}

 

int main()

{

Inventory I[20];

clrscr();

int ch,n;

cout<<"\nEnter the range:";

cin>>n;

 

do

{

cout<<"\n\n*********** Inventory of Mobile ****************";

cout<<"\n1.Accept and display details.";

cout<<"\n2.To sale Mobile.";

cout<<"\n3.To purchase Mobile.";

cout<<"\n4.Exit.";

cout<<"\nEnter your choice:";

cin>>ch;

 

switch(ch)

{

case 1:

for(int i=0;i<n;i++)

{

I[i].accept();

}

break;

 

case 2:

for(i=0;i<n;i++)

{

I[i].sale();

}

break;

 

case 3:

for(i=0;i<n;i++)

{

I[i].purchase();

}

break;

 

case 4:

exit(0);

}

}while(ch!=4);

getch();

return 0;

}