C/C++ Cheat Sheet
For your reference; this sheet will also be included in exams
CISC 124, fall 2004
class Salesperson: public Employee {
Sample C++ Program:
private:
#include <stdio.h>
double rate;
#include <string.h>
public:
class Employee {
Salesperson(char name[],
private:
double wage,
char name[51];
double rate)
double wage; // hourly wage
: Employee(name, wage) {
protected:
this->rate = rate;
double payOwed;
} // end constructor
public:
Employee(char n[], double w) {
void payForSale(double amount) {
strcpy(name, n);
payOwed += amount * rate;
setWage(w);
} // end payForSale
payOwed = 0;
} // end constructor
void print() {
printf("Salesperson ");
virtual void pay(double hours) {
Employee::print();
payOwed += wage * hours;
printf(", rate: $%.2f\n", rate);
} // end pay
} // end print
double amountToPay() {
}; // end Salesperson
return payOwed;
} // end amountToPay
int main(void) {
Employee mickey("Mickey Mouse", 20);
void setWage(double wage) {
mickey.setWage(21);
this->wage = wage;
mickey.println();
}
Salesperson *donald =
void zero() {
new Salesperson("Donald Duck",
payOwed = 0;
10, 0.15);
} // end zero
donald->payForSale(100);
donald->println();
// prints employee
Employee *company[2];
virtual void print() {
company[0] = &mickey;
printf("name = %s, ", name);
company[1] = donald;
printf("wage = $%.2f, ", wage);
for (int i = 0; i < 2; i++) {
printf("payOwed = $%.2f",
company[i]->pay(10);
payOwed);
company[i]->println();
} // end print
} // end for
} // end main
void println() {
print();
printf("\n");
} // end println
}; // end class Employee