WAP to Create a product class which consist of product no, name, price, quantity and amount. Create 3 functions named getdata() for input from the user, displaydata() to display the details and calculate() to calculate amt = price * quantity. Enter the details for 5 products. Pass the object in the function. Use constructor to initialize the values.
#include <iostream>
using namespace std;
#define MAX 10
class product
{
private:
char name[30];
int product_no;
int price;
int quantity;
double amt;
public:
void getdata(void);
void displaydata(void);
};
void product::getdata(void){
cout << "Enter name: " ;
cin >> name;
cout << "Enter product number: ";
cin >> product_no;
cout<<"Enter the price of the product: ";
cin>>price;
cout << "Enter the product quantity: ";
cin >> quantity;
amt=price*quantity;
}
void product::displaydata(void){
cout << "product details:\n";
cout << "Name:"<< name << ",Product Number:" << product_no <<",Price:"<<price<< ",Amount:" << amt<<endl;
}
int main()
{
product std[MAX];
int n,loop;
cout << "Enter total number of products: ";
cin >> n;
for(loop=0;loop< n; loop++){
cout << "Enter details of product " << loop+1 << ":\n";
std[loop].getdata();
}
cout << endl;
for(loop=0;loop< n; loop++){
cout << "\nDetails of product " << (loop+1) << ":\n";
std[loop].displaydata();
}
return 0;
}
Comments
Leave a comment