Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C++ Program for Calculating Customer Bills with Tax and Discount, Exercises of Applications of Computer Sciences

The source code for a c++ program that defines a customer class with private data members and friend function calculatebill() to calculate the total bill after tax and discount based on the customer's spending. The program demonstrates the usage of the customer class by creating objects using both default and parameterized constructors and setting data using setter functions.

Typology: Exercises

2011/2012

Uploaded on 08/01/2012

omni
omni 🇮🇳

4.6

(9)

46 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
/****************** Assignment No. 3 : Solution
**********************/
using namespace std;
#include<iostream>
#include <cstring>
#include <conio.h>
// 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;
docsity.com
pf3
pf4

Partial preview of the text

Download C++ Program for Calculating Customer Bills with Tax and Discount and more Exercises Applications of Computer Sciences in PDF only on Docsity!

/****************** Assignment No. 3 : Solution **********************/

using namespace std;

#include #include #include <conio.h>

// 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; }

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" <<endl; cout <<"Customer Name : " << cus3->getName() << endl << "ID : " << cus3-

getID() << endl << "Spending : " << cus3->getSpending() << endl << "Total Bill : " << calculateBill(cus3) << endl; cout << endl << endl;

getch();

}