Question:9
C++ program to declare a class named product with attributes as product number, product name, rate, quantity, and cost. Take input for the details calculate and display cost of the product.
Sol:
#include <iostream> #include<conio.h> using namespace std; class product { private: int pno; char pname[20]; float rate,qty,cost; public: void input(); void cal(); void output(); }; void product::input() { cout<<"Enter pno,pname,rate and qty "; cin>>pno>>pname>>rate>>qty; } void product::cal() { cost=rate*qty; } void product::output() { cal(); cout<<"Total cost = "<<cost<<endl; } int main() { product p; p.input(); p.output(); return(0); }
/* Output */ Enter pno,pname,rate and qty 1001 Lux 45 5 Total cost = 225
Question:10
C++ program to declare a class named bill with attributes customer code, customer name, room rent, number of days, and bill amount. Take input for the details, Calculate and print total bill amount.
Sol:
#include <iostream> #include<conio.h> using namespace std; class bill { private: int ccode; char cname[20]; float rent,nod,bill; public: void input(); void cal(); void output(); }; void bill::input() { cout<<"Enter ccode,cname,rent and nod "; cin>>ccode>>cname>>rent>>nod; } void bill::cal() { bill=rent*nod; } void bill::output() { cal(); cout<<"Total Bill Amount = "<<bill<<endl; } int main() { bill b; b.input(); b.output(); return(0); }
/* Output */ Enter ccode,cname,rent and nod 1002 Rohit 2500 3 Total Bill Amount = 7500
Question:11
C++ program to declare a class named book with attributes as book code, book title, price, number of copies, and total cost. Take input for the details Calculate and display total cost of the books.
Sol:
#include <iostream> #include<conio.h> using namespace std; class book { private: int bcode; char btitle[20]; float price,noc,cost; public: void input(); void cal(); void output(); }; void book::input() { cout<<"Enter bcode, btitle, price and noc "; cin>>bcode>>btitle>>price>>noc; } void book::cal() { cost=price*noc; } void book::output() { cal(); cout<<"Total Book Cost = "<<cost<<endl; } int main() { book b; b.input(); b.output(); return(0); }
/* Output */ Enter bcode, btitle, price and noc 1005 Java 245 6 Total Book Cost = 1470