EECE 230 Code C++ 3rd Edition, Spring 2011

American University of Beirut · Introduction to Programming · 812 pages

All EECE 230 files
EECE 230 Code C++ 3rd Edition, Spring 2011, page 1

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

EECE 230 Code C++ 3rd Edition, Spring 2011, page 2 EECE 230 Code C++ 3rd Edition, Spring 2011, page 3 EECE 230 Code C++ 3rd Edition, Spring 2011, page 4 EECE 230 Code C++ 3rd Edition, Spring 2011, page 5 EECE 230 Code C++ 3rd Edition, Spring 2011, page 6 EECE 230 Code C++ 3rd Edition, Spring 2011, page 7 EECE 230 Code C++ 3rd Edition, Spring 2011, page 8 EECE 230 Code C++ 3rd Edition, Spring 2011, page 9 EECE 230 Code C++ 3rd Edition, Spring 2011, page 10

Showing the first 10 of 812 pages. Download the PDF to see all of it.

More EECE 230 files

Show the text of this file
#include <iostream> using namespace std; int main() { cout << "My first C++ program." << endl; cout << "The sum of 2 and 3 = " << 5 << endl; cout << "7 + 8 = " << 7 + 8 << endl; return 0; } //Program: Sales data analysis #include <iostream> #include <fstream> #include <iomanip> #include <string> using namespace std; const int NO_OF_SALES_PERSON = 6; struct salesPersonRec { string ID; //salesperson's ID double saleByQuarter[4]; //array to store the total //sales for each quarter double totalSale; //salesperson's yearly sales amount }; void initialize(ifstream& indata, salesPersonRec list[], int listSize); void getData(ifstream& infile, salesPersonRec list[], int listSize); void saleByQuarter(salesPersonRec list[], int listSize, double totalByQuarter[]); void totalSaleByPerson(salesPersonRec list[], int listSize); void printReport(ofstream& outfile, salesPersonRec list[], int listSize, double saleByQuarter[]); void maxSaleByPerson(ofstream& outData, salesPersonRec list[], int listSize); void maxSaleByQuarter(ofstream& outData, double saleByQuarter[]); int main() { //Step 1 ifstream infile; //input file stream variable ofstream outfile; //output file stream variable string inputFile; //variable to hold the input file name string outputFile; //variable to hold the output file name double totalSaleByQuarter[4]; //array to hold the //sale by quarter salesPersonRec salesPersonList[NO_OF_SALES_PERSON]; //array //to hold the salesperson's data cout << "Enter the salesPerson ID file name: "; //Step 2 cin >> inputFile; //Step 3 cout << endl; infile.open(inputFile.c_str()); //Step 4 if (!infile) //Step 5 { cout << "Cannot open the input file." << endl; return 1; } initialize(infile, salesPersonList, NO_OF_SALES_PERSON); //Step 6 infile.close(); //Step 7 infile.clear(); //Step 7 cout << "Enter the sales data file name: "; //Step 8 cin >> inputFile; //Step 9 cout << endl; infile.open(inputFile.c_str()); //Step 10 if (!infile) //Step 11 { cout << "Cannot open the input file." << endl; return 1; } cout << "Enter the output file name: "; //Step 12 cin >> outputFile; //Step 13 cout << endl; outfile.open(outputFile.c_str()); //Step 14 outfile << fixed << showpoint << setprecision(2); //Step 15 getData(infile, salesPersonList, NO_OF_SALES_PERSON); //Step 16 saleByQuarter(salesPersonList, NO_OF_SALES_PERSON, totalSaleByQuarter); //Step 17 totalSaleByPerson(salesPersonList, NO_OF_SALES_PERSON); //Step 18 printReport(outfile, salesPersonList, NO_OF_SALES_PERSON, totalSaleByQuarter); //Step 19 maxSaleByPerson(outfile, salesPersonList, NO_OF_SALES_PERSON); //Step 20 maxSaleByQuarter(outfile, totalSaleByQuarter); //Step 21 infile.close(); //Step 22 outfile.close(); //Step 22 return 0; } void initialize(ifstream& indata, salesPersonRec list[], int listSize) { int index; int quarter; for (index = 0; index < listSize; index++) { indata >> list[index].ID; //get salesperson's ID for (quarter = 0; quarter < 4; quarter++) list[index].saleByQuarter[quarter] = 0.0; list[index].totalSale = 0.0; } } //end initialize void getData(ifstream& infile, salesPersonRec list[], int listSize) { int index; int quarter; string sID; int month; double amount; infile >> sID; //get salesperson's ID while (infile) { infile >> month >> amount; //get the sale month and //the sale amount for (index = 0; index < listSize; index++) if (sID == list[index].ID) break; if (1 <= month && month <= 3) quarter = 0; else if (4 <= month && month <= 6) quarter = 1; else if (7 <= month && month <= 9) quarter = 2; else quarter = 3; if (index < listSize) list[index].saleByQuarter[quarter] += amount; else cout << "Invalid salesperson's ID." << endl; infile >> sID; } //end while } //end getData void saleByQuarter(salesPersonRec list[], int listSize, double totalByQuarter[]) { int quarter; int index; for (quarter = 0; quarter < 4; quarter++) totalByQuarter[quarter] = 0.0; for (quarter = 0; quarter < 4; quarter++) for (index = 0; index < listSize; index++) totalByQuarter[quarter] += list[index].saleByQuarter[quarter]; } //end saleByQuarter void totalSaleByPerson(salesPersonRec list[], int listSize) { int index; int quarter; for (index = 0; index < listSize; index++) for (quarter = 0; quarter < 4; quarter++) list[index].totalSale += list[index].saleByQuarter[quarter]; } //end totalSaleByPerson void printReport(ofstream& outfile, salesPersonRec list[], int listSize, double saleByQuarter[]) { int index; int quarter; outfile << "----------- Annual Sales Report ---------" << "----" << endl; outfile << endl; outfile << " ID QT1 QT2 QT3 " << "QT4 Total" << endl; outfile << "____________________________________________" << "_________________" << endl; for (index = 0; index < listSize; index++) { outfile << list[index].ID << " "; for (quarter = 0; quarter < 4; quarter++) outfile << setw(10) << list[index].saleByQuarter[quarter]; outfile << setw(10) << list[index].totalSale << endl; } outfile << "Total "; for (quarter = 0; quarter < 4; quarter++) outfile << setw(10) << saleByQuarter[quarter]; outfile << endl << endl; } //end printReport void maxSaleByPerson(ofstream& outData, salesPersonRec list[], int listSize) { int maxIndex = 0; int index; for (index = 1; index < listSize; index++) if (list[maxIndex].totalSale < list[index].totalSale) maxIndex = index; outData << "Max Sale by SalesPerson: ID = " << list[maxIndex].ID << ", Amount = $" << list[maxIndex].totalSale << endl; } //end maxSaleByPerson void maxSaleByQuarter(ofstream& outData, double saleByQuarter[]) { int quarter; int maxIndex = 0; for (quarter = 0; quarter < 4; quarter++) if (saleByQuarter[maxIndex] < saleByQuarter[quarter]) maxIndex = quarter; outData << "Max Sale by Quarter: Quarter = " << maxIndex + 1 << ", Amount = $" << saleByQuarter[maxIndex] << endl; } //end maxSaleByQuarter 12345 1 893 32214 1 343 23422 3 903 57373 2 893 35864 5 329 54654 9 392 12345 2 999 32214 4 892 23422 4 895 23422 2 492 57373 6 892 35864 10 1223 54654 11 3420 12345 12 322 35864 5 892 54654 3 893 12345 8 494 32214 8 9023 23422 6 223 23422 4 783 57373 8 8834 35864 3 2882 ----------- Annual Sales Report ------------- ID QT1 QT2 QT3 QT4 Total _____________________________________________________________ 12345 1892.00 0.00 494.00 322.00 2708.00 32214 343.00 892.00 9023.00 0.00 10258.00 23422 1395.00 1901.00 0.00 0.00 3296.00 57373 893.00 892.00 8834.00 0.00 10619.00 35864 2882.00 1221.00 0.00 1223.00 5326.00 54654 893.00 0.00 392.00 3420.00 4705.00 Total 8298.00 4906.00 18743.00 4965.00 Max Sale by SalesPerson: ID = 57373, Amount = $10619.00 Max Sale by Quarter: Quarter = 3, Amount = $18743.00 12345 32214 23422 57373 35864 54654 //Candy Machine Header File class cashRegister { public: int getCurrentBalance() const; //Function to show the current amount in the cash //register. //Postcondition: The value of cashOnHand is returned. void acceptAmount(int amountIn); //Function to receive the amount deposited by //the customer and update the amount in the register. //Postcondition: cashOnHand = cashOnHand + amountIn; cashRegister(int cashIn = 500); //Constructor //Sets the cash in the register to a specific amount. //Postcondition: cashOnHand = cashIn; // If no value is specified when the // object is declared, the default value // assigned to cashOnHand is 500. private: int cashOnHand; //variable to store the cash //in the register }; //*********** class dispenserType class dispenserType { public: int getNoOfItems() const; //Function to show the number of items in the machine. //Postcondition: The value of numberOfItems is returned. int getCost() const; //Function to show the cost of the item. //Postcondition: The value of cost is returned. void makeSale(); //Function to reduce the number of items by 1. //Postcondition: numberOfItems--; dispenserType(int setNoOfItems = 50, int setCost = 50); //Constructor //Sets the cost and number of items in the dispenser //to the values specified by the user. //Postcondition: numberOfItems = setNoOfItems; // cost = setCost; // If no value is specified for a // parameter, then its default value is // assigned to the corresponding member // variable. private: int numberOfItems; //variable to store the number of //items in the dispenser int cost; //variable to store the cost of an item }; //Implementation file candyMachineImp.cpp //This file contains the definitions of the functions to //implement the operations of the classes cashRegister and //dispenserType #include <iostream> #include "candyMachine.h" using namespace std; int cashRegister::getCurrentBalance() const { return cashOnHand; } void cashRegister::acceptAmount(int amountIn) { cashOnHand = cashOnHand + amountIn; } cashRegister::cashRegister(int cashIn) { if (cashIn >= 0) cashOnHand = cashIn; else cashOnHand = 500; } int dispenserType::getNoOfItems() const { return numberOfItems; } int dispenserType::getCost() const { return cost; } void dispenserType::makeSale() { numberOfItems--; } dispenserType::dispenserType(int setNoOfItems, int setCost) { if (setNoOfItems >= 0) numberOfItems = setNoOfItems; else numberOfItems = 50; if (setCost >= 0) cost = setCost; else cost = 50; } // Main Program #include <iostream> #include "candyMachine.h" using namespace std; 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(100, 85); int choice; //variable to hold the selection showSelection(); cin >> choice; while (choice != 9) { 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; }//end switch showSelection(); cin >> choice; }//end while return 0; }//end main void showSelection() { cout << "*** Welcome to Shelly's 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 << "9 to exit" << endl; }//end showSelection void sellProduct(dispenserType& product, cashRegister& pCounter) { int amount; //variable to hold the amount entered int amount2; //variable to hold the extra amount needed if (product.getNoOfItems() > 0) //if the dispenser is not //empty { 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; }//end sellProduct //cashRegister Header File class cashRegister { public: int getCurrentBalance() const; //Function to show the current amount in the cash //register. //Postcondition: The value of cashOnHand is returned. void acceptAmount(int amountIn); //Function to receive the amount deposited by //the customer and update the amount in the register. //Postcondition: cashOnHand = cashOnHand + amountIn; cashRegister(int cashIn = 500); //Constructor //Sets the cash in the register to a specific amount. //Postcondition: cashOnHand = cashIn; // If no value is specified when the // object is declared, the default value // assigned to cashOnHand is 500. private: int cashOnHand; //variable to store the cash //in the register }; //Implementation file cashRegisterImp.cpp //This file contains the definitions of the functions to //implement the operations of the class cashRegister #include <iostream> #include "cashRegister.h" using namespace std; int cashRegister::getCurrentBalance() const { return cashOnHand; } void cashRegister::acceptAmount(int amountIn) { cashOnHand = cashOnHand + amountIn; } cashRegister::cashRegister(int cashIn) { if (cashIn >= 0) cashOnHand = cashIn; else cashOnHand = 500; } //dispenserType Header File class dispenserType { public: int getNoOfItems() const; //Function to show the number of items in the machine. //Postcondition: The value of numberOfItems is returned. int getCost() const; //Function to show the cost of the item. //Postcondition: The value of cost is returned. void makeSale(); //Function to reduce the number of items by 1. //Postcondition: numberOfItems--; dispenserType(int setNoOfItems = 50, int setCost = 50); //Constructor //Sets the cost and number of items in the dispenser //to the values specified by the user. //Postcondition: numberOfItems = setNoOfItems; // cost = setCost; // If no value is specified for a // parameter, then its default value is // assigned to the corresponding member // variable. private: int numberOfItems; //variable to store the number of //items in the dispenser int cost; //variable to store the cost of an item }; //Implementation file dispenserType.cpp //This file contains the definitions of the functions to //implement the operations of the class dispenserType #include <iostream> #include "dispenserType.h" using namespace std; int dispenserType::getNoOfItems() const { return numberOfItems; } int dispenserType::getCost() const { return cost; } void dispenserType::makeSale() { numberOfItems--; } dispenserType::dispenserType(int setNoOfItems, int setCost) { if (setNoOfItems >= 0) numberOfItems = setNoOfItems; else numberOfItems = 50; if (setCost >= 0) cost = setCost; else cost = 50; } // Main Program #include <iostream> #include "cashRegister.h" #include "dispenserType.h" using namespace std; 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(100, 85); int choice; //variable to hold the selection showSelection(); cin >> choice; while (choice != 9) { 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; }//end switch showSelection(); cin >> choice; }//end while return 0; }//end main void showSelection() { cout << "*** Welcome to Shelly's 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 << "9 to exit" << endl; }//end showSelection void sellProduct(dispenserType& product, cashRegister& pCounter) { int amount; //variable to hold the amount entered int amount2; //variable to hold the extra amount needed if (product.getNoOfItems() > 0) //if the dispenser is not //empty { 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; }//end sellProduct //clockType.h, the specification file for the class clockType class clockType { public: void setTime(int hours, int minutes, int seconds); //Function to set the time. //The time is set according to the parameters. //Postcondition: hr = hours; min = minutes; // sec = seconds; // The function checks whether the // values of hours, minutes, and seconds // are valid. If a value is invalid, the // default value 0 is assigned. void getTime(int& hours, int& minutes, int& seconds) const; //Function to return the time. //Postcondition: hours = hr; minutes = min; // seconds = sec; void printTime() const; //Function to print the time. //Postcondition: The time is printed in the form // hh:mm:ss. void incrementSeconds(); //Function to increment the time by one second. //Postcondition: The time is incremented by one second. // If the before-increment time is // 23:59:59, the time is reset to 00:00:00. void incrementMinutes(); //Function to increment the time by one minute. //Postcondition: The time is incremented by one minute. // If the before-increment time is // 23:59:53, the time is reset to 00:00:53. void incrementHours(); //Function to increment the time by one hour. //Postcondition: The time is incremented by one hour. // If the before-increment time is // 23:45:53, the time is reset to 00:45:53. bool equalTime(const clockType& otherClock) const; //Function to compare the two times. //Postcondition: Returns true if this time is equal to // otherClock; otherwise, returns false. clockType(int hours, int minutes, int seconds); //Constructor with parameters //The time is set according to the parameters. //Postcondition: hr = hours; min = minutes; // sec = seconds; // The constructor checks whether the // values of hours, minutes, and seconds // are valid. If a value is invalid, the // default value 0 is assigned. clockType(); //Default constructor //The time is set to 00:00:00. //Postcondition: hr = 0; min = 0; sec = 0; private: int hr; //variable to store the hours int min; //variable to store the minutes int sec; //variable to store the seconds }; //Implementation File for the class clockType #include <iostream> #include "clockType.h" using namespace std; void clockType::setTime(int hours, int minutes, int seconds) { if (0 <= hours && hours < 24) hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) min = minutes; else min = 0; if (0 <= seconds && seconds < 60) sec = seconds; else sec = 0; } void clockType::getTime(int& hours, int& minutes, int& seconds) const { hours = hr; minutes = min; seconds = sec; } void clockType::incrementHours() { hr++; if(hr > 23) hr = 0; } void clockType::incrementMinutes() { min++; if (min > 59) { min = 0; incrementHours(); } } void clockType::incrementSeconds() { sec++; if (sec > 59) { sec = 0; incrementMinutes(); } } void clockType::printTime() const { if (hr < 10) cout << "0"; cout << hr << ":"; if (min < 10) cout << "0"; cout << min << ":"; if (sec < 10) cout << "0"; cout << sec; } bool clockType::equalTime(const clockType& otherClock) const { return (hr == otherClock.hr && min == otherClock.min && sec == otherClock.sec); } clockType::clockType(int hours, int minutes, int seconds) { if (0 <= hours && hours < 24) hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) min = minutes; else min = 0; if (0 <= seconds && seconds < 60) sec = seconds; else sec = 0; } clockType::clockType() //default constructor { hr = 0; min = 0; sec = 0; } //The user program that uses the class clockType #include <iostream> #include "clockType.h" using namespace std; int main() { clockType myClock; clockType yourClock; int hours; int minutes; int seconds; //Set the time of myClock myClock.setTime(5, 4, 30); //Line 1 cout << "Line 2: myClock: "; //Line 2 myClock.printTime(); //print the time of myClock Line 3 cout << endl; //Line 4 cout << "Line 5: yourClock: "; //Line 5 yourClock.printTime(); //print the time of yourClock Line 6 cout << endl; //Line 7 //Set the time of yourClock yourClock.setTime(5, 45, 16); //Line 8 cout << "Line 9: After setting, yourClock: "; //Line 9 yourClock.printTime(); //print the time of yourClock Line 10 cout << endl; //Line 11 //Compare myClock and yourClock if (myClock.equalTime(yourClock)) //Line 12 cout << "Line 13: Both times are equal." << endl; //Line 13 else //Line 14 cout << "Line 15: The two times are not equal." << endl; //Line 15 cout << "Line 16: Enter the hours, minutes, and " << "seconds: "; //Line 16 cin >> hours >> minutes >> seconds; //Line 17 cout << endl; //Line 18 //Set the time of myClock using the value of the //variables hours, minutes, and seconds myClock.setTime(hours, minutes, seconds); //Line 19 cout << "Line 20: New myClock: "; //Line 20 myClock.printTime(); //print the time of myClock Line 21 cout << endl; //Line 22 //Increment the time of myClock by one second myClock.incrementSeconds(); //Line 23 cout << "Line 24: After incrementing myClock by " << "one second, myClock: "; //Line 24 myClock.printTime(); //print the time of myClock Line 25 cout << endl; //Line 26 //Retrieve the hours, minutes, and seconds of the //object myClock myClock.getTime(hours, minutes, seconds); //Line 27 //Output the value of hours, minutes, and seconds cout << "Line 28: hours = " << hours << ", minutes = " << minutes << ", seconds = " << seconds << endl; //Line 28 return 0; }//end main //The complete program listing of the program that defines //and uses the class clockType #include <iostream> using namespace std; class clockType { public: void setTime(int, int, int); void getTime(int&, int&, int&) const; void printTime() const; void incrementSeconds(); void incrementMinutes(); void incrementHours(); bool equalTime(const clockType&) const; private: int hr; int min; int sec; }; int main() { clockType myClock; clockType yourClock; int hours; int minutes; int seconds; //Set the time of myClock myClock.setTime(5, 4, 30); //Line 1 cout << "Line 2: myClock: "; //Line 2 myClock.printTime(); //print the time of myClock Line 3 cout << endl; //Line 4 cout << "Line 5: yourClock: "; //Line 5 yourClock.printTime(); //print the time of yourClock Line 6 cout << endl; //Line 7 //Set the time of yourClock yourClock.setTime(5, 45, 16); //Line 8 cout << "Line 9: After setting, yourClock: "; //Line 9 yourClock.printTime(); //print the time of yourClock Line 10 cout << endl; //Line 11 //Compare myClock and yourClock if (myClock.equalTime(yourClock)) //Line 12 cout << "Line 13: Both times are equal." << endl; //Line 13 else //Line 14 cout << "Line 15: The two times are not equal." << endl; //Line 15 cout << "Line 16: Enter the hours, minutes, and " << "seconds: "; //Line 16 cin >> hours >> minutes >> seconds; //Line 17 cout << endl; //Line 18 //Set the time of myClock using the value of the //variables hours, minutes, and seconds myClock.setTime(hours, minutes, seconds); //Line 19 cout << "Line 20: New myClock: "; //Line 20 myClock.printTime(); //print the time of myClock Line 21 cout << endl; //Line 22 //Increment the time of myClock by one second myClock.incrementSeconds(); //Line 23 cout << "Line 24: After incrementing myClock by " << "one second, myClock: "; //Line 24 myClock.printTime(); //print the time of myClock Line 25 cout << endl; //Line 26 //Retrieve the hours, minutes, and seconds of the //object myClock myClock.getTime(hours, minutes, seconds); //Line 27 //Output the value of hours, minutes, and seconds cout << "Line 28: hours = " << hours << ", minutes = " << minutes << ", seconds = " << seconds << endl; //Line 28 return 0; }//end main void clockType::setTime(int hours, int minutes, int seconds) { if (0 <= hours && hours < 24) hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) min = minutes; else min = 0; if (0 <= seconds && seconds < 60) sec = seconds; else sec = 0; } void clockType::getTime(int& hours, int& minutes, int& seconds) const { hours = hr; minutes = min; seconds = sec; } void clockType::incrementHours() { hr++; if (hr > 23) hr = 0; } void clockType::incrementMinutes() { min++; if (min > 59) { min = 0; incrementHours(); //increment hours } } void clockType::incrementSeconds() { sec++; if (sec > 59) { sec = 0; incrementMinutes(); //increment minutes } } void clockType::printTime() const { if (hr < 10) cout << "0"; cout << hr << ":"; if (min < 10) cout << "0"; cout << min << ":"; if (sec < 10) cout << "0"; cout << sec; } bool clockType::equalTime(const clockType& otherClock) const { return (hr == otherClock.hr && min == otherClock.min && sec == otherClock.sec); } class testClass { public: void print() const; //Line a testClass(); //Line b testClass(int, int); //Line c testClass(int, int, double); //Line d testClass(double, char); //Line e private: int x; int y; double z; char ch; }; //Constructor with default values implementation file #include <iostream> #include "testClassConstructor.h" using namespace std; void testClass::print() const { cout << "x = " << x << ", y = " << y << ", z = " << z << ", ch = " << ch << endl; } testClass::testClass() //default constructor { x = 0; y = 0; z = 0; ch = '*'; } testClass::testClass(int tX, int tY) { x = tX; y = tY; z = 0; ch = '*'; } testClass::testClass(int tX, int tY, double tZ) { x = tX; y = tY; z = tZ; ch = '*'; } testClass::testClass(double tZ, char tCh) { x = 0; y = 0; z = tZ; ch = tCh; } //Test program for constructor with default values #include <iostream> #include "testClassConstructor.h" using namespace std; int main() { testClass one; //Line 1 testClass two(5, 6); //Line 2 testClass three(5, 7, 4.5); //Line 3 testClass four(4, 9, 12); //Line 4 testClass five(3.4, 'D'); //Line 5 one.print(); //Line 6; output one two.print(); //Line 7; output two three.print(); //Line 8; output three four.print(); //Line 9; output four five.print(); //Line 10; output five return 0; } //Constructor with default Parameters class testClass { public: void print() const; testClass(int = 0, int = 0, double = 0.0, char = '*'); private: int x; int y; double z; char ch; }; //Constructor with default values implementation file #include <iostream> #include "testClassConstDefaultValues.h" using namespace std; void testClass::print() const { cout << "x = " << x << ", y = " << y << ", z = " << z << ", ch = " << ch << endl; } testClass::testClass(int tX, int tY, double tZ, char tCh) { x = tX; y = tY; z = tZ; ch = tCh; } //Test program for constructor with default values #include <iostream> #include "testClassConstDefaultValues.h" using namespace std; int main() { testClass one; //Line 1 testClass two(5, 0, 6.0); //Line 2 testClass three(5, 7, 4.5); //Line 3 testClass four(4, 9, 12); //Line 4 testClass five(0, 0, 3.4, 'D'); //Line 5 one.print(); //Line 6; Output one two.print(); //Line 7; output two three.print(); //Line 8; output three four.print(); //Line 9; output four five.print(); //Line 10; output five return 0; } //personType.h #include <string> using namespace std; class personType { public: void print() const; //Function to output the first name and last name //in the form firstName lastName. void setName(string first, string last); //Function to set firstName and lastName according //to the parameters. //Postcondition: firstName = first; lastName = last string getFirstName() const; //Function to return the first name. //Postcondition: The value of firstName is returned. string getLastName() const; //Function to return the last name. //Postcondition: The value of lastName is returned. personType(string first = "", string last = ""); //Constructor //Sets firstName and lastName according to the parameters. //The default values of the parameters are null strings. //Postcondition: firstName = first; lastName = last private: string firstName; //variable to store the first name string lastName; //variable to store the last name }; //personTypeImp.cpp #include <iostream> #include <string> #include "personType.h" using namespace std; void personType::print() const { cout << firstName << " " << lastName; } void personType::setName(string first, string last) { firstName = first; lastName = last; } string personType::getFirstName() const { return firstName; } string personType::getLastName() const { return lastName; } //constructor personType::personType(string first, string last) { firstName = first; lastName = last; } //Test Program personType #include <iostream> #include <string> #include "personType.h" using namespace std; int main() { personType student("Lisa", "Regan"); student.print(); cout<<endl; return 0; } #include <iostream> #include "illustrate.h" using namespace std; int illustrate::count = 0; int illustrate::y = 0; void illustrate::print() const { cout << "x = " << x << ", y = " << y << ", count = " << count << endl; } void illustrate::setX(int a) { x = a; } void illustrate::incrementY() { y++; } illustrate::illustrate(int a) { x = a; } class illustrate { public: static int count; //public static variable void print() const; //Function to output x, y, and count. void setX(int a); //Function to set x. //Postcondition: x = a; static void incrementY(); //static function //Function to increment y by 1. //Postcondition: y = y + 1 illustrate(int a = 0); //constructor //Postcondition: x = a; // If no value is specified for a, x = 0; private: int x; static int y; //private static variable }; #include <iostream> #include "illustrate.h" using namespace std; int main() { illustrate illusObject1(3); //Line 1 illustrate illusObject2(5); //Line 2 illustrate::incrementY(); //Line 3 illustrate::count++; //Line 4 illusObject1.print(); //Line 5 illusObject2.print(); //Line 6 cout << "Line 7: ***Increment y using " << "illusObject1***" << endl; //Line 7 illusObject1.incrementY(); //Line 8 illusObject1.setX(8); //Line 9 illusObject1.print(); //Line 10 illusObject2.print(); //Line 11 cout << "Line 12: ***Increment y using " << "illusObject2***" << endl; //Line 12 illusObject2.incrementY(); //Line 13 illusObject2.setX(23); //Line 14 illusObject1.print(); //Line 15 illusObject2.print(); //Line 16 return 0; } #ifndef dateType_H #define dateType_H class dateType { public: void setDate(int month, int day, int year); //Function to set the date. //The member variables dMonth, dDay, and dYear are set //according to the parameters. //Postcondition: dMonth = month; dDay = day; // dYear = year int getDay() const; //Function to return the day. //Postcondition: The value of dDay is returned. int getMonth() const; //Funct