CP1_2_Introduction_cont_1122

CP1_2_Introduction_cont_1122 - Introduction to Programming...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Programming Introduction to Programming with C++ (cont.) with C++ (cont.) Writing the Program Writing the Program A simple C++ program A simple C++ program Here is the traditional first C++ program. We might write it to a file named hello.cpp // This program displays Hello World! Information for #include <iostream> sing namespace std; human readers Information for the using namespace std; int main() compiler { cout << "Hello World!" << endl; return 0; Our code } he following slides explain the meaning of each 2 CSIS1117B Computer Programming 1 2011-2012 The following slides explain the meaning of each section of the program Comments Comments // This program displays Hello World! #include <iostream> sing namespace std; using namespace std; int main() { out < "Hello World!" << ndl cout << "Hello World!" << endl; return 0; } We always try to make our programs understandable. An effective way to do this is to provide explanatory comments. The line beginning with the double slash is a comment. A comment line starts with a double slash and ends at the end of the line. The whole line is ignored by the compiler. 3 CSIS1117B Computer Programming 1 2011-2012 Including libraries Including libraries // This program displays Hello World! #include <iostream> using namespace std; int main() { { cout << "Hello World!" << endl; return 0; } We saw earlier that we don’t write everything ourselves. For example, here we want to use an output routine which is available in the standard C++ input/output library iostream . The include directive , #include <…>, tells the compiler that the program requires external library components and where to look for information about them. The linker will combine object code for the required libraries with the object code for our own part of the program. 4 CSIS1117B Computer Programming 1 2011-2012 Namespaces – the using directive Namespaces – the using directive // This program displays Hello World! #include <iostream> using namespace std; int main() { cout << "Hello World!" << endl; return 0; } Namespaces support the approach of constructing our programs by combining components, where each component developed separately and possibly not by us is developed separately and possibly not by us. You won’t appreciate namespaces until you begin to onstruct large programs but we can introduce the general construct large programs, but we can introduce the general idea here, so that you know why we need the using directive in the example. 5 CSIS1117B Computer Programming 1 2011-2012 Informal intro to namespaces (1) Informal intro to namespaces (1) Say we want to include two different libraries. However, those libraries happen to use the same name for something and both names are visible to our program....
View Full Document

Page1 / 19

CP1_2_Introduction_cont_1122 - Introduction to Programming...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online