Answer to Question #254499 in C++ for sunny

Question #254499

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.



1
Expert's answer
2021-10-21T16:07:16-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS