Uploaded by Axel Villamayor

ReviewForTest1OnJuly052023 (1)

advertisement
Review Intro to Classes
MAC125
07/03/2023
Construct a class names savings containing three floating-point data members
named balance, rate, and interest and a constructor that initializes each
data member to zer0, Include a member function that inputs a balance and rate
and then calculate an interest. The rate should be stored as percent, such as
6.5 for 6.5%, and the interest the interest is computes as interest =
(balance)(rate /100). Add a member function to display all data member values
Note: include the class in a working C++ program that test each member
function
#include <iostream>
#include <string>
using namespace std;
class Savings {
private:
double balance;
double rate;
double interest;
public:
Savings(double = 0.0, double = 0.0, double = 0.0);
void input(double, double);
void showdata();
};
//function definitions
//(scope resoluton operator "::")
Savings::Savings(double bal, double ratval, double ival) {
balance = bal;
rate = ratval;
interest = ival;
return;
}
void Savings:: input(double newbalance, double newrate) {
balance = newbalance;
rate = newrate;
interest = balance * rate / 100.0;
return;
}
void Savings::showdata() {
cout << "Balance: " << balance << " Rate: " << rate << " Interest: " << interest
<< endl;
return;
}
int main() {
1
Review Intro to Classes
MAC125
07/03/2023
// custom data type
//1: data struture
//2: function to perform actions to the create structure
Savings a;
float bval, rateval;
cout << "The current value for the object a is: " << endl;
a.showdata();
cout << "Enter a new balance and rate value: " << endl;
cin >> bval >> rateval;
a.input(bval, rateval);
cout << "The new values for the object a are: " << endl;
a.showdata();
return 0;
}
Solution in-Class Exercise (13) # 2(In-class doc)
#include <iostream>
using namespace std;
class Employee
{
private:
int idnum;
double payrate;
double maxhours;
2
Review Intro to Classes
MAC125
07/03/2023
public:
Employee(int id= 0, double pay= 0.0, double
pay; maxhours = hrs; }
void setdata(int, double, double);
void showdata();
void deleteEmp();
int getid() { return idnum; }
};
hrs =0.0) { idnum = id; payrate =
//function implementations
void Employee::setdata(int id, double pay, double hrs)
{
idnum = id;
payrate = pay;
maxhours = hrs;
}
void Employee::deleteEmp()
{
idnum = 0;
payrate = 0;
maxhours = 0;
}
void Employee::showdata()
{
cout << "the id number is " << idnum << endl;
cout << "the maximum hours worked is " << maxhours << endl;
cout << "The pay rate is " << payrate << endl;
}
int main()
{
//declare variables
int id; // for user input
int choice; //user selection from menu
int count = 0; // number of employees
int index;
int i; // lops
double pay, hrs; // get input from the user for pay and hours
Employee emps[100]; //array of employees
do
{
cout << "1.add " << endl;
cout << "2. modify " << endl;
cout << "3. delete " << endl;
cout << "4. Exit " << endl;
cin >> choice;
if (choice = !4)
{
if (choice == 1)
3
Review Intro to Classes
MAC125
07/03/2023
{
cout << "please enter an id number for the new employee: ";
cin >> id;
cout << "please enter the max hours for the new employee ";
cin >> hrs;
cout << "please enter the pay rate for new employee ";
cin >> pay;
emps[count].setdata(id, pay, hrs);
count++;
}
else if (choice == 2)
{
if (count == 0)
cout << " You must add an employye first " << endl;
else
{
cout << "please enter the old id number ";
cin >> id;
for (i = 0; i < count; i++)
{
if (emps[i].getid() == id)
{
index = i;
break;
}
else
index = -1;
} //loop
if (index == -1)
cout << "Employee with that id does not exist!"
<< endl;
else
{
cout << "please enter a
cin >> id;
cout << "Please enter a
cin >> hrs;
cout << "please enter a
cin >> pay;
emps[index].setdata(id,
} //else
} // else
new id number: ";
new max hours: ";
new pay rate: ";
pay, hrs);
} // else if
else if (choice == 3)
{
if (count == 0)
cout << "you must add an employee first" << endl;
else
{
cout << "please enter the id number: ";
cin >> id;
for (i = 0; i < count; i++)
4
Review Intro to Classes
MAC125
07/03/2023
{
if (emps[i].getid == id)
{
index = i;
break;
}
else
index = -1;
}
if(index == -1)
cout << "Employee with that id does not exist! " <<
endl;
else
{
emps[index].deleteEmp();
cout << "Employee was deleted " << endl;
}
}
}
}
} while (choice != 4);
for(i=0; i < count; i+1)
{
if (emps[i].getid() != 0)
emps[i].showdata();
system("pause");
return 0;
}
Homework
5
Review Intro to Classes
MAC125
07/03/2023
6
Download