C++ examples for Class:Member Function
Define and invoke a function that's a member of the class Employee
#include <cstdio> #include <cstdlib> #include <iostream> using namespace std; class Employee/*from w w w. j ava 2s. com*/ { 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; }