C++ Write the definition for a class where you could be an object in the class.
ID: 3813841 • Letter: C
Question
C++
Write the definition for a class where you could be an object in the class. So your name would end up being a variable name if someone were to use this class. Your class must have at least 2 member variables and at least one member function with its corresponding definition.
The obvious example would be a student class, with member variables like gpa, and number of credits received. The member function could be a gpa calculator. You can't use this example.
You don't need to actually write an executable program, just write a class that someone else could use to write a program. You should try to imagine a program that would use your class while writing the class definition.
Explanation / Answer
PROGRAM CODE:
#include <iostream>
using namespace std;
//Creating an Employee class which calculates the salary for the employee
//depending on the hoursworked
class Employee
{
string employeeID;
double salary;
int hoursWorked;
public:
//constructor for initializing the values
Employee(string id, int hours_worked)
{
employeeID = id;
hoursWorked = hours_worked;
salary = 0;
}
//calculating the salary
double calculateSalary()
{
salary = hoursWorked*50;
}
//printing employee info
void printEmployee()
{
cout<<"Emp ID: "<<employeeID<<" Hours worked: "<<hoursWorked;
cout<<" Salary: "<<salary<<endl;
}
};
//Main progrsm using the class
int main() {
Employee michael("ED12343", 35);
michael.calculateSalary();
michael.printEmployee();
return 0;
}
OUTPUT:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.