COMP2004
Programming Practice
2002 Summer School


Kevin Pulo
School of Information Technologies
University of Sydney


(page 1)


About Me






(page 2)


Textbook







(page 3)


Course Information






(page 4)


Assessment






(page 5)


Assignment Policy





(page 6)


The C++ Language





(page 7)


C++ At Basser



g++ -Wall -g -o hello hello.cc




(page 8)


A Simple C++ Program



#include
int main() {
std::cout << "Very simple" << std::endl;
}
  • That's a complete C++ program


(page 9)


Variables and Constants





(page 10)


Variables and Constants



#include
int main() {
const int value = 5;
int result = 4;
result += value;
std::cout << result << std::endl;
}


(page 11)


Enumerated Types


enum day_of_week {Sun, Mon, Tue,
Wed, Thu, Fri, Sat};
day_of_week today = Wed;


(page 12)


Functions



double halve (int number) {
double result;
result = number / 2.0;
return result;
}


(page 13)


Control Flow


int main() {
int i = 5;
if (i) // equiv to: if (i != 0)
std::cout << i << " is true\n";
}


(page 14)


C++ Programming Style





(page 15)


Basic Input


#include
int main() {
std::string s;
int i;
double d;
char c;
std::cin >> s >> i >> d >> c;
std::getline(cin, s);
}



(page 16)


Basic Output


#include
int main() {
std::string s = "Hello";
int i = 42;
double d = 1.3;
char c = ' ';
std::cout << s << c << i << c << d;
}



(page 17)


Error Output


#include
int main() {
std::cerr << "Sent to std::cerr\n";
std::cout << "Sent to std::cout\n";
}



(page 18)


Streams




(page 19)


More on Input


#include
int main() {
int x, y;
if (std::cin >> x >> y)
std::cout << "worked" << std::endl;
else
std::cout << "failed" << std::endl;
}

(page 20)


How Input/Output can fail




(page 21)


Manipulators



(page 22)


setw and setfill


std::cout << std::setw(10) << 123;
std::cout << std::setw(5) << std::setfill('#');
std::cout << "hi" << 123;


(page 23)


setprecision


double d = 2.0 / 3.0;
std::cout << d << '\n';
std::cout << setprecision(4) << d << '\n';
std::cout << setprecision(0) << d << '\n';


(page 24)


Buffered Output






(page 25)


flush


std::string name;
std::cout << "Enter name : " << std::flush;
std::cin >> name;


(page 26)


std::cerr for debug output


#include
int main() {
std::cout << "A";
function_which_might_crash();
std::cout << "B";
}

std::cout << "A" << std::flush;
std::cout << "A" << std::endl;
std::cerr << "A";




(page 27)