C++ Class Definition Define and invoke class member function
#include <cstdio> #include <cstdlib> #include <iostream> using namespace std; class Employee/*from ww w .ja v a2 s. co m*/ { public: double addWork(int hours, double grade) { double weightedSalary; weightedSalary = workHour * salary; workHour += hours; weightedSalary += grade * hours; salary = weightedSalary / workHour; return salary; } int workHour; double salary; }; int main(int nNumberofArgs, char* pszArgs[]) { Employee s; s.workHour = 3; s.salary = 3.0; cout << "Before: s = (" << s.workHour << ", " << s. salary << ")" << endl; cout << "Adding 3 hours with a grade of 4.0" << endl; s.addWork(3, 4.0); // call the member function cout << "After: s = (" << s.workHour << ", " << s. salary << ")" << endl; return 0; }