Computer Science 112
Honors Credit
-
Send an email to mailto:adams@calvin.edu
Day 2 Notes: Argument Passing
-
c++ program must have a main. must return int.
-
everything can be on one line.
-
if/else if/else. Chaining by statements. Switch is better.
#include <iostream>
#include <cassert>
using namespace std;
// // unsigned factorial(unsigned n) {
// // // if ((n == 0) or (n == 1)) {
// // // return 1;
// // // }
// // // else {
// // // unsigned total = 1;
// // // for (unsigned i = 2; i <= n; i++) {
// // // total *= i;
// // // }
// // // return total;
// // // }
// // unsigned result = 1;
// // for (unsigned number = 2 ; number <= n ; number++) result *= number;
// // return result;
// // }
// void exchange(int &a, int &b) {
// int c = a;
// a = b;
// b = c;
// }
// void rotate(int &a, int &b, int &c) {
// int d = a;
// a = b;
// b = c;
// c = d;
// }
void print(const string &genome) {
for(unsigned i = 0 ; i < genome.size() ; i ++) {
cout << genome[i];
}
}
int main() {
// int x = 11, y = 22, z = 33;
// exchange(x, y);
// assert(x == 22);
// assert(y == 11);
// rotate(x, y, z);
// assert(x == 11); assert (y == 33); assert(z == 22);
// // assert(factorial(0) == 1);
// // assert(factorial(1) == 1);
// // assert(factorial(2) == 2);
// // assert(factorial(3) == 6);
// // assert(factorial(4) == 24);
// // assert(factorial(5) == 120);
// cout << "Tests succeeded." << endl;
print("ACTAGCTA");
}
-
parameter passing modes
-
default: pass by value, parameter is a distinct variable, into which the argument is copied. Changes to the parameter have no effect on the argument back in the caller.
-
pass by reference, place an ampersand between a parameter's type and name. Think of these parameters as aliases of the arguments.
-
call-by-const-reference: the parameter is a read only reference to the parameter.
-
-
primitive types (use pass-by-value):
-
int
-
double
-
char
-
float
-
unsigned
-
long
-
short
-
-
Composite types (classes and structs) (use pass-by-const-value):
-
string
-
stream
-
cstream
-
complex
-
Day 3 Notes: Classes
Modeling payroll
-
employees
-
name
-
paid vacation days
-
sick days
-
identifier (number)
-
-
salaried employees
-
salary
-
-
hourly employees
-
wage
-
hours worked
-
overtime
-
#ifndef EMPLOYEETESTER_H_
#define EMPLOYEETESTER_H_
class EmployeeTester {
public:
EmployeeTester();
void runTests();
void testConstructors();
};
#endif
#include "EmployeeTester.h"
#include <iostream>
using namespace std;
void EmployeeTester::EmployeeTester() {
}
void EmployeeTester::runTests() {
cout << "Testing class Employee..." << endl;
testConstructors();
cout << "All tests passed." << endl;
}
void EmployeeTester::testConstructors() {
cout << "- consturcotrs... " << flush;
Employee emp;
assert(emp.getId() == 0);
assert(emp.getName() == "");
assert(emp.getVacation() == 0);
cout << "Passed!" << endl;
}
#include <iostream>
#include <cassert>
#include "EmployeeTester.h"
using namespace std;
int main () {
EmployeeTester et;
et.runTests();
}