CSI 250L Lab 3 Solution, Spring 2012

American University of Science and Technology · Programming II Lab · 3 pages · Dr. Saeed Raheel · with solution

All CSI 250L files
CSI 250L Lab 3 Solution, Spring 2012, page 1

Stuck on a question? A tutor can walk you through it.

CSI 250L Lab 3 Solution, Spring 2012, page 2 CSI 250L Lab 3 Solution, Spring 2012, page 3

More CSI 250L files

Show the text of this lab
American University of Science & Technology Department Of Computer Science CSI 250L – Computer Programming-II Lab Fall 2011/2012 Lab Work 3 Solution provided by : Dr. Saeed Raheel Problem 4: 1. Write the class definition and implementation for the above specified class. Use separate files for the definition and implementation. 2. Write a program to do the following:  Create 3 objects of the class Invoice.  Set the attributes of each object to new values entered by the user. Note that each invoice should have a unique number. Print the total of the invoices amounts. “Invoice.h” #ifndef INVOICE_H #define INVOICE_H class Invoice { private: int Number; double Amount; public: Invoice(); Invoice(int, double); void Print(); 1 Invoice Attributes  Number: an integer to identify an invoice.  Amount: a double number to represent the cash figure of the invoice. Methods  Constructor.  Provide a print, set and get functions. void setNumber(int); int getNumber(); void setAmount(double); double getAmount(); }; #endif “Invoice.cpp” #include <iostream> #include "invoice.h" using namespace std; Invoice::Invoice() { Number = 0; Amount = 0; } Invoice::Invoice(int n, double a) { Number = n; Amount = a; } void Invoice::setNumber(int n) { Number = n; } void Invoice::setAmount(double a) { Amount = a; } int Invoice::getNumber() { return Number; } double Invoice::getAmount() { return Amount; } void Invoice::Print() { cout << "Invoice Number : " << getNumber() << endl << "Amount : " << getAmount() << endl; } “Testing program” 2 #include <iostream> #include "Invoice.h" using namespace std; int main() { Invoice i1, i2, i3; int number; double amount; cout << "Please enter the details of the first invoice :\n" << "Number : "; cin >> number; i1.setNumber(number); cout << "Amount : "; cin >> amount; i1.setAmount(amount); cout << "\nPlease enter the details of the second invoice :\n" << "Number : "; cin >> number; while (number == i1.getNumber()) { cout << "Error: Invoice number should be unique!\nPlease enter another number : "; cin >> number; cout << endl; } i2.setNumber(number); cout << "Amount : "; cin >> amount; i2.setAmount(amount); cout << "\nPlease enter the details of the third invoice :\n" << "Number : "; cin >> number; while (number == i1.getNumber() || number == i2.getNumber()) { cout << "Error: Invoice number should be unique!\nPlease enter another number : "; cin >> number; cout << endl; } i3.setNumber(number); cout << "Amount : "; cin >> amount; i3.setAmount(amount); cout << "\nTotal amount of the 3 invoices : " << i1.getAmount() + i2.getAmount() + i3.getAmount() << endl << endl; system("pause"); return 0; } 3