// Filename: LEMONS.CPP
// Child's lemonade sale-tracking program
#include <iostream.h>
// Lemonade class declaration comes next
class Lemon {
private:
int totalLeft; // Will start at 100
int sugarTeasp; // Starts at 80
double total; // Income for day
public:
void init(void); // Initialize members upon startup
void showTot(void); // Print the day's total income
void buySweet(void); // When customer buys sweetened
void buyUnSweet(void); // When customer buys unsweetened
};
// The member functions appear next. The scope resolution
// operator, ::, tells Visual C++ that these are member
// functions of the Lemon class and not regular non-class
// functions such as those that might follow main()
void Lemon::init(void)
{ totalLeft = 100;
sugarTeasp = 80;
total = 0.0;
}
void Lemon::showTot(void)
{ cout.precision(2);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout << endl << "Total so far today is $" << total << endl << endl;
}
void Lemon::buySweet(void)
{
if (totalLeft == 0)
{ cerr << "Sorry, no more lemonade is left." << endl << endl;}
else
if (sugarTeasp == 0)
{ cerr << "No more sugar is left. Sorry." << endl << endl; }
else
{ cout << "Enjoy your drink!" << endl << endl;
totalLeft--; // One less glass left
sugarTeasp -= 2; // Each glass takes 2 teaspoons
total += .50;
}
}
void Lemon:: buyUnSweet(void)
{
if (totalLeft == 0)
{ cerr << "Sorry, no more lemonade is left." << endl << endl;}
else
{ cout << "Enjoy your drink!"<< endl << endl;
totalLeft--; // One less glass left
total += .45;
}
}
////////////////// Class ends here /////////////////////////
main()
{
Lemon drink; // Define a class variable
int ans;
drink.init(); // Initialize data members to start of day
do {
cout << "What's happening?" << endl;
cout << " 1. Sell a sweetened." << endl;
cout << " 2. Sell an unsweetened." << endl;
cout << " 3. Show total sales so far." << endl;
cout << " 4. Quit the program." << endl;
cout << "What do you want to do? ";
cin >> ans;
switch (ans)
{ case 1 : drink.buySweet(); // Update proper totals
break;
case 2 : drink.buyUnSweet();
break;
case 3 : drink.showTot();
break;
case 4 : drink.showTot(); // Print total one last time
return 1;
}
} while (ans >=1 && ans <= 4);
return 0;
}
============================
// Filename: LOGICALS.CPP
// A program with an if, an if-else,
// and a logical operator
#include <iostream.h>
void main()
{
int numCusts;
float totalSales;
cout << "How many customers were in yesterday? ";
cin >> numCusts;
cout << "What were the total sales? ";
cin >> totalSales;
// A simple if
if (numCusts > 25)
{ cout << "Order more snacks for tomorrow." << endl; }
// An if-else
if (totalSales >= 2000.00)
{ cout << "Reorder stock." << endl;
cout << "Give sales staff a raise." << endl;
}
else
{ cout << "Replace the sales staff." << endl; }
// An if with a logical test
if ((numCusts >= 50) && (totalSales >= 5000.00))
{ cout << "Take a day off!" << endl;
cout << "Remodel the store." << endl;
}
return;
}
=========================
Tidak ada komentar:
Posting Komentar