CIS 7124 Lecture 1

CISC 7124
Object-Oriented Programming
Payroll Sample Program in C++
salary_info.h

 1 #ifndef SALARY_INFO_H
 2 #define SALARY_INFO_H
 3      
 4 #include <iostream>
 5 
 6 class SalaryInfo {
 7      friend std::ostream &operator <<(std::ostream &os, const SalaryInfo &salaryInfo) {
 8           os << salaryInfo.rate << "\t" << salaryInfo.ytdPay << "\t" << salaryInfo.ytdOvertime;
 9           return os;
10      }
11 public:
12      SalaryInfo(int rate, int ytdPay, int ytdOvertime) : rate(rate), ytdPay(ytdPay), ytdOvertime(ytdOvertime) {}
13 
14      int calcPay(int hours) {
15           int pay;
16           if (hours <= REGULAR_HOURS)
17                pay = rate * hours;
18           else {
19                int overtime = hours - REGULAR_HOURS;
20                pay = rate * hours + rate / 2 * overtime;
21                ytdOvertime += overtime;
22           }
23           ytdPay += pay;
24           return pay;
25      }    
26      
27      int getRate() {return rate;}
28           
29 private:  
30      int ytdOvertime;
31      int rate; 
32      int ytdPay;
33      const static int REGULAR_HOURS = 40;
34 };        
35 
36 #endif
Lines Terms Notes In Java
33 static constant initialization Data members must be initialized within the constructor (typically in the member initialization list). Static members (of which there is only one instance/copy) cannot be initialized within the constructor (they are associated with the class as a whole, and not any particular object/instance of the class, and thus need to be initialized independent of the constructor being called), and are therefore usually initialized within the .cpp file. An exception to this is a static constant of integral type which may be initializaed at its point of declaration within the class declaration. Java allows all variables const, static or otherwise to be initialized at the point of declaration