/****************** Assignment No. 3 : Solution **********************/ using namespace std; #include #include #include // Definition of customer class class customer{ friend double calculateBill(customer*); // Declaration of Non-member friend function private: char name[30]; int custID; double spending; double tax; double discount; public: customer(); //Default constructor customer(char[], int, double); //Parameterized constructor /*Setter and getter functions for the class*/ void setName(char[]); void setID(int); void setSpending(double); char* getName(); int getID(); double getSpending(); }; // Default constructor Definition customer::customer() { strcpy(name, "No Name"); custID = 0; spending = tax = discount = 0; } // Parameterized constructor Definition customer :: customer(char Name[], int ID, double Spending) { strcpy(name, Name); custID = ID; spending = Spending; tax = 0; discount = 0; } //Definition of setter and getter functions void customer :: setName(char Name[]) { strcpy(name, Name); } void customer :: setID(int ID) { custID = ID; } void customer :: setSpending(double Spending) { spending = Spending; } char* customer :: getName() { return name; } int customer :: getID() { return custID; } double customer :: getSpending() { return spending; } /* Defining friend function calculateBill which takes an object as parameter and calculates total bill after adding tax and subtracting discount from the bill*/ double calculateBill(customer *obj) { double bill = 0; if(obj->spending <= 5000) //if the spending is less than 5000, then tax is 5% and discount is 1% { obj->tax = obj->spending * 5 / 100; obj->discount = obj->spending * 1 / 100; bill = obj->spending + obj->tax - obj->discount; } else if(obj->spending > 5000 && obj->spending <=10000) //if the spending is more than 5000 and less than 10000, then tax is 10% and discount is 2% { obj->tax = obj->spending * 10 / 100; obj->discount = obj->spending * 2 / 100; bill = obj->spending + obj->tax - obj->discount; } else //if the spending is more than than 10000, then tax is 15% and discount is 3% { obj->tax = obj->spending * 15 / 100; obj->discount = obj->spending * 3 / 100; bill = obj->spending + obj->tax - obj->discount; } return bill; // Function returns the final value of bill calculated in one of above three condition blocks } int main(){ char NAME[30]; int id = 0; double Spending = 0; //Creating object with default constructor customer *cus1 = new customer(); //cus1 is a pointer to an object of type customer /*Following code is showing the values of data members of object pointed by cus1*/ cout << "Bill detail for customer 1, initialized with default constructor" <getName() << endl << "ID : " << cus1->getID() << endl << "Spending : " << cus1->getSpending() << endl << "Total Bill : " << calculateBill(cus1) << endl; cout << endl << endl; //Creating object with parameterized construcor customer *cus2 = new customer("Ali", 112, 8550); /*Following code is showing the values of data members of object pointed by cus2*/ cout << "Bill detail for customer 2, initialized with parameterized constructor" <getName() << endl << "ID : " << cus2->getID() << endl << "Spending : " << cus2->getSpending() << endl << "Total Bill : " << calculateBill(cus2) << endl; cout << endl << endl; //Creating object with default constructor customer *cus3 = new customer(); /*The following code takes input in three ordinary variables and then these variables are passed to object pointed by the pointer cus3, through setter functions*/ cout << "Enter name of customer 3 : "; cin >> NAME; cout << "Enter ID of customer 3 : "; cin >> id; cout << "Enter total spending of customer 3 : "; cin >> Spending; cus3->setName(NAME); cus3->setID(id); cus3->setSpending(Spending); cout << endl; //Showing the values of object pointed by the pointer cus3. cout << "Bill detail for customer 3, input entered by the user" <getName() << endl << "ID : " << cus3->getID() << endl << "Spending : " << cus3->getSpending() << endl << "Total Bill : " << calculateBill(cus3) << endl; cout << endl << endl; getch(); }