Constructor & Destructor

Q.1) Problem1: A common place to buy candy is from a machine. The machine sells candies, chips, gum, and cookies. You have been asked to write a program for this candy machine. The program should do the following: 1. Show the customer the different products sold by the candy machine. 2. Let the customer make the selection. 3. Show the customer the cost of the item selected. 4. Accept money from the customer. 5. Release the item. The machine has two main components: a built-in cash register and several dispensers to hold and release the products. Define class cashRegister in C++ with the following descriptions : Private Members:

● cashOnHand of type integer

Public Members:

● A default constructor cashRegister() sets the cash in the register to 500.

● A constructor cashRegister(int) sets the cash in the register to a specific amount.

● A function getCurrentBalance() which returns value of cashOnHand

● A function acceptAmount(int) to receive the amount deposited by the customer and update the amount in the register Define class dispenserType in C++ with the following descriptions : Private Members:

● numberOfItems of type integer

● cost of type integer Public Members:

● A default constructor dispenserType () sets the cost and number of items in the dispenser to 50 and 50.

● A constructor dispenserType (int,int) sets the cost and number of items in the dispenser to the values specified by the user.

● A function getNoOfItems() to return the value of numberOfItems.

● A function getCost() to return the value of cost.

● A function makeSale() to reduce the number of items by 1. When the program executes, it must do the following: 2 www.cppforschool.com 1. Show the different products sold by the candy machine. 2. Show how to select a particular product. Once the user has made the appropriate selection, the candy machine must act accordingly. If the user has opted to buy a product and that product is available, the candy machine should show the cost of the product and ask the user to deposit the money. If the amount deposited is at least the cost of the item, the candy machine should sell the item and display an appropriate message. Divide this program into three functions: showSelection, sellProduct, and main. The function sellProduct must have access to the dispenser holding the product (to decrement the number of items in the dispenser by 1 and to show the cost

of the item) as well as the cash register (to update the cash). Therefore, this function has two parameters: one corresponding to the dispenser and the other corresponding to the cash register.

#include<iostream>

using namespace std;

class cashRegister

{

private:

int cashOnHand;

public:

cashRegister();

cashRegister(int cashIn);

int getCurrentBalance();

void acceptAmount(int amountIn);

};

class dispenserType

{

private:

int numberOfItems;

int cost;

public:

dispenserType();

dispenserType(int setNoOfItems, int setCost);

int getNoOfItems();

int getCost();

void makeSale();

};

cashRegister::cashRegister()

{

cashOnHand = 500;

}

cashRegister::cashRegister(int cashIn)

{

cashOnHand = cashIn;

}

int cashRegister::getCurrentBalance()

{

return cashOnHand;

}

void cashRegister::acceptAmount(int amountIn)

{

cashOnHand = cashOnHand + amountIn;

}

dispenserType::dispenserType()

{

numberOfItems = 50;

cost = 50;

}

dispenserType::dispenserType(int setNoOfItems, int setCost)

{

numberOfItems = setNoOfItems;

cost = setCost;

}

int dispenserType::getNoOfItems()

{

return numberOfItems;

}

int dispenserType::getCost()

{

return cost;

}

void dispenserType::makeSale()

{

numberOfItems--;

}

void showSelection();

void sellProduct(dispenserType& product, cashRegister& pCounter);

int main()

{

cashRegister counter;

dispenserType candy(100, 50);

dispenserType chips(100, 65);

dispenserType gum(75, 45);

dispenserType cookies(50, 85);

int choice;

showSelection();

cin >> choice;

while (choice != 5)

{

switch (choice)

{

case 1:

sellProduct(candy, counter);

break;

case 2:

sellProduct(chips, counter);

break;

case 3:

sellProduct(gum, counter);

break;

case 4:

sellProduct(cookies, counter);

break;

default:

cout << "Invalid selection." << endl;

}

showSelection();

cin >> choice;

}

return 0;

}

void showSelection()

{

cout << "*** Welcome to Candy Shop ***" << endl;

cout << "To select an item, enter " << endl;

cout << "1 for Candy" << endl;

cout << "2 for Chips" << endl;

cout << "3 for Gum" << endl;

cout << "4 for Cookies" << endl;

cout << "5 to exit" << endl;

}

void sellProduct(dispenserType& product, cashRegister& pCounter)

{

int amount;

int amount2;

if (product.getNoOfItems() > 0)

{

cout << "Please deposit " << product.getCost() << " cents" << endl;

cin >> amount;

if (amount < product.getCost())

{

cout << "Please deposit another " << product.getCost()- amount << " cents" << endl;

cin >> amount2;

amount = amount + amount2;

}

if (amount >= product.getCost())

{

pCounter.acceptAmount(amount);

product.makeSale();

cout << "Collect your item at the bottom and " << "enjoy." << endl;

}

else

cout << "The amount is not enough. " << "Collect what you deposited." << endl;

cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl << endl;

}

else

cout << "Sorry, this item is sold out." << endl;

}

Output:-

Q.2)

#include<iostream>

#include<string.h>

using namespace std;

class Customer

{

private:

int Customer_no;

string Customer_name;

int Qty;

float Price,Totalprice,Discount,Netprice;

public:

Customer()

{

Customer_no=111;

Customer_name="Leena";

Qty=0;

Price=0;

Discount=0;

Netprice=0;

}

void input()

{

cout<<"Customer_No :- ";

cin>>Customer_no;

cout<<"Customer_Name :- ";

cin>>Customer_name;

cout<<"Quntity :- ";

cin>>Qty;

cout<<"price :- ";

cin>>Price;

caldiscount();

}

void caldiscount()

{

Totalprice=Price*Qty;

if(Totalprice>=50000)

{

Discount= 0.25*Totalprice;

}

else if(Totalprice>=25000 && Totalprice<50000)

{

Discount= 0.15*Totalprice;

}

else if(Totalprice<25000 && Totalprice<=0)

{

Discount= 0.10*Totalprice;

}

else

{

cout<<"you enter wrong price or quentity";

}

Netprice=Totalprice-Discount;

}

void show()

{

cout<<"Customer_No :- "<<Customer_no<<endl;

cout<<"Customer_Name :- "<<Customer_name<<endl;

cout<<"Quntity :- "<<Qty<<endl;

cout<<"Price :- "<<Price<<endl;

cout<<"Total price :- "<<Totalprice<<endl;

cout<<"Discount :- "<<Discount<<endl;

cout<<"Netprice :- "<<Netprice<<endl;

}

};

int main()

{

Customer obj;

obj.input();

obj.caldiscount();

obj.show();

}

Output:-

Q.3) Define a class Garments in C++ with the following descriptions:- Private members:- GCode 🡪 of type string GType 🡪 of type string GSize 🡪of type integer GFabric🡪of type string GPrice🡪of type float A function Assign() which calculates and assigns the value of GPrice as follows:-11 4 www.cppforschool.com For the value of GFabric “COTTON”, Gtype GPrice(Rs) Trouser 1300 Shirt

1100 For GFabric other than “COTTON” the above mentioned GPrice gets reduced by 10% Public members:-A constructor to assign initial values to GCode,GType and GFabric with the word “NOT ALLOTTED” and GSize and GPrice with 0. A function Input() to input the values of the data members GCode,GType,GSize and GFabric and invoke he assign function. A function Display() which displays the content of all the data members for a Garment.

#include<iostream>

#include<string.h>

#include<stdio.h>

using namespace std;

class Garments

{

char GCode[21],GType[21];

int Gsize;

char Gfabric[21];

float Gprice;

void Assign( )

{

if(strcmp(Gfabric,"COTTON")==0)

{ if(strcmp(GType,"TROUSER")==0)

Gprice=1300;

if(strcmp(GType,"SHIRT")==0)

Gprice=1100;

}

else

{

if(strcmp(GType,"TROUSER")==0)

Gprice=1300*0.90;

if(strcmp(GType,"SHIRT")==0)

Gprice=1100*0.90;

}

}

public:

Garments( )

{

strcpy(GCode,"NOT ALLOTED");

strcpy(GType,"NOT ALLOTED");

Gsize=0;

strcpy(Gfabric,"NOT ALLOTED");

Gprice=0;

}

void Input( )

{

cout<<"\nEnter the Garment Code: ";

cin>>GCode;

cout<<"\nEnter the Garment Type: ";

cin>>GType;

cout<<"\nEnter the Garment Size: ";

cin>>Gsize;

cout<<"\nEnter the Garment Fabric: ";

cin>>Gfabric;

Assign( );

}

void display( )

{

cout<<"\nThe Garment Code: "<<GCode;

cout<<"\nThe Garment Type: "<<GType;

cout<<"\nThe Garment Size: "<<Gsize;

cout<<"\nThe Garment Fabric: "<<Gfabric;

cout<<"\nThe Garment Price: "<<Gprice;

}

};

int main( )

{

Garments G;

G.Input( );

G.display( );

}

Output:-


Comments