Presentation is loading. Please wait.

Presentation is loading. Please wait.

January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 1Ubuntu You will develop your course projects in C++ under Ubuntu Linux. If.

Similar presentations


Presentation on theme: "January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 1Ubuntu You will develop your course projects in C++ under Ubuntu Linux. If."— Presentation transcript:

1 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 1Ubuntu You will develop your course projects in C++ under Ubuntu Linux. If your home computer or laptop is running under Windows, an easy and painless way of installing Ubuntu is Wubi: installer installer The Wubi installer will put the latest version of Ubuntu onto your computer.

2 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 2Ubuntu Wubi does not require its own partition on your hard drive but simulates a file system within one large file. As a consequence, file operations are slightly less efficient than in an actual Ubuntu installation, but that difference does not matter for our purposes. When your computer starts, you will see a boot menu that allows you to choose between Windows and Ubuntu. Wubi can easily be uninstalled, in which case the boot menu will no longer show up.

3 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 3Ubuntu Another possibility is to download a Ubuntu CD image (.iso file): … and then run it as a virtual machine using the VMWare Player: In either case, also download the g++ compiler from your Ubuntu terminal by typing: $ sudo apt-get install g++

4 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 4 C++ Overview History and major features of C++History and major features of C++ Input and outputInput and output Preprocessor directivesPreprocessor directives CommentsComments

5 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 5 History of C : C-with-classes developed by Bjarne Stroustrup 1980: C-with-classes developed by Bjarne Stroustrup 1983: C-with-classes redesigned and called C : C-with-classes redesigned and called C : C++ compilers made available 1985: C++ compilers made available 1989: ANSI/ISO C++ standardization starts 1989: ANSI/ISO C++ standardization starts 1998: ANSI/ISO C++ standard approved 1998: ANSI/ISO C++ standard approved

6 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 6 Major Features of C++ Almost upward compatible with C Almost upward compatible with C Not all valid C programs are valid C++ programs. Not all valid C programs are valid C++ programs. Why? Why? Because of reserved words in C++ such as class Because of reserved words in C++ such as class Extends C with object-oriented features Extends C with object-oriented features Compile-time checking: strongly typed Compile-time checking: strongly typed Classes with multiple inheritance Classes with multiple inheritance No garbage collection, but semi-automatic storage reclamation No garbage collection, but semi-automatic storage reclamation

7 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 7 Sample C++ Program #include #include using namespace std; int main() { string deptChairName, deptChairPasswd, enteredPasswd; string deptChairName, deptChairPasswd, enteredPasswd; bool passwdOK; bool passwdOK; int attempts = 0; int attempts = 0; deptChairName = GetDeptChairName(); deptChairName = GetDeptChairName(); deptChairPasswd = GetDeptChairPasswd(); deptChairPasswd = GetDeptChairPasswd(); cout << "How are you today, " << deptChairName << "?\n"; cout << "How are you today, " << deptChairName << "?\n"; do do { cout << "Please enter your password: "; cout << "Please enter your password: "; cin >> enteredPasswd; cin >> enteredPasswd; attempts++; attempts++; if (enteredPasswd == deptChairPasswd) if (enteredPasswd == deptChairPasswd) { cout << "Thank you!" << endl; cout << "Thank you!" << endl; passwdOK = true; passwdOK = true; } else else { cout << "Hey! Are you really " << deptChairName << "?\n"; cout << "Hey! Are you really " << deptChairName << "?\n"; cout << "Try again!\n"; cout << "Try again!\n"; passwdOK = false; passwdOK = false; } } while (!passwdOK && attempts < 3); while (!passwdOK && attempts < 3); if (passwdOK) if (passwdOK) SetNewParameters(); SetNewParameters(); else else { Shout(Warning! Illegal access!); Shout(Warning! Illegal access!); CallCampusPolice(); CallCampusPolice(); } return 0; return 0;}

8 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 8 Input and Output Some standard functions for input and output are provided by the iostream library.Some standard functions for input and output are provided by the iostream library. The iostream library is part of the standard library.The iostream library is part of the standard library. Input from the terminal (standard input) is tied to the iostream object cin.Input from the terminal (standard input) is tied to the iostream object cin. Output to the terminal (standard output) is tied to the iostream object cout.Output to the terminal (standard output) is tied to the iostream object cout. Error and warning messages can be sent to the user via the iostream object cerr (standard error).Error and warning messages can be sent to the user via the iostream object cerr (standard error).

9 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 9 Input and Output Use the output operator (<<) to direct a value to standard output.Use the output operator (<<) to direct a value to standard output. Successive use of << allows concatenation.Successive use of << allows concatenation. Examples:Examples: cout << Hi there!\n;cout << Hi there!\n; cout << I have << << classes today.;cout << I have << << classes today.; cout << goodbye! << endl; (new line & flush)cout << goodbye! << endl; (new line & flush)

10 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 10 Input and Output Use the input operator (>>) to read a value from standard input.Use the input operator (>>) to read a value from standard input. Standard input is read word by word (words are separated by spaces, tabs, or newlines).Standard input is read word by word (words are separated by spaces, tabs, or newlines). Successive use of >> allows reading multiple words into separate variables.Successive use of >> allows reading multiple words into separate variables. Examples:Examples: cin >> name;cin >> name; cin >> nameA >> nameB;cin >> nameA >> nameB;

11 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 11 Input and Output How can we read an unknown number of input values? int main() { string word; string word; while (cin >> word) cout > word) cout << word read is: << word << \n; cout << OK, thats all.\n; cout << OK, thats all.\n; return 0; } return 0; }

12 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 12 Input and Output The previous program will work well if we use a file instead of the console (keyboard) as standard input. In Linux, we can do this using the < symbol. Let us say that we have created a text file input.txt (e.g., by using gedit) in the current directory. It contains the following text: This is just a stupid test. Let us further say that we stored our program in a file named test.C in the same directory.

13 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 13 Input and Output We can now compile our program using the g++ compiler into an executable file named test: $ g++ test.C –o test The generated code can be executed using the following command: $./test If we would like to use the content of our file input.txt as standard input for this program, we can type the following command: $./test < input.txt

14 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 14 Input and Output We will then see the following output in our terminal: Word read is: This Word read is: is Word read is: just Word read is: a Word read is: stupid Word read is: test. OK, thats all.

15 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 15 Input and Output If we want to redirect the standard output to a file, say output.txt, we can use the > symbol: $./test output.txt We can read the contents of the generated file by simply typing: $ less output.txt We will then see that the file contains the output that we previously saw printed in the terminal window. (By the way, press Q to get the prompt back.)

16 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 16 Input and Output If you use keyboard input for your program, it will never terminate but instead wait for additional input. Use the getline command to read an entire line from cin, and put it into a stringstream object that can be read word by word just like cin. Using stringstream objects requires the inclusion of the sstream header file: #include #include

17 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 17 Input and Output #include #include #include using namespace std; int main() { string userinput, word; getline(cin, userinput); stringstream mystream(userinput); stringstream mystream(userinput); while (mystream >> word) while (mystream >> word) cout << "word read is: " << word << "\n"; cout << "word read is: " << word << "\n"; cout << "OK, thats all.\n"; cout << "OK, thats all.\n"; return 0; return 0;}

18 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 18 Input and Output By the way, you are not limited to strings when using cin and cout, but you can use other types such as integers. However, if your program expects to read an integer and receives a string, the read operation will fail. If your program always uses file input and output, it is better to use fstream objects instead of cin and cout.

19 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 19 File Input and Output #include #include using namespace std; int main() { ofstream outfile(out_file.txt); ofstream outfile(out_file.txt); ifstream infile(in_file.txt); ifstream infile(in_file.txt); if (!infile) if (!infile) { cerr << error: unable to open input file; cerr << error: unable to open input file; return –1; return –1; } if (!outfile) if (!outfile) { cerr << error: unable to open output file; cerr << error: unable to open output file; return –2; return –2; } string word; string word; while (infile >> word) while (infile >> word) outfile << word << _; outfile << word << _; return 0; return 0;}

20 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 20 Preprocessor Directives Preprocessor directives are specified by placing a pound sign (#) in the very first column of a line in our program. For example, header files are made part of our program by the preprocessor include directive. The preprocessor replaces the #include directive with the contents of the named file. There are two possible forms: #include #include #include my_file.h

21 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 21 Preprocessor Directives If the file name is enclosed in angle brackets ( ), the file is presumed to be a standard header file. Therefore, the preprocessor will search for the file in a predefined set of locations. If the file name is enclosed by a pair of quotation marks, the file is presumed to be a user-supplied header file. Therefore, the search for the file begins in the directory in which the including file is located (project directory).

22 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 22 Preprocessor Directives The included file may itself contain an #include directive (nesting). This can lead to the same header file being included multiple times in a single source file. Conditional directives guard against the multiple processing of a header file. Example: #ifndef SHOUT_H #define SHOUT_H /* shout.h definitions go here */ /* shout.h definitions go here */#endif

23 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 23 Preprocessor Directives The #ifdef, #ifndef, and #endif directives are most frequently used to conditionally include program code depending on whether a preprocessor constant is defined. This can be useful, for example, for debugging:

24 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 24 Preprocessor Directives int main() { #ifdef DEBUG cout << Beginning execution of main()\n; cout << Beginning execution of main()\n;#endif string word; string word; while (cin >> word) cout > word) cout << word read is: << word << \n; cout << OK, thats all.; cout << OK, thats all.; return 0; } return 0; }

25 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 25Comments Comments are an important aid to human readers of our programs.Comments are an important aid to human readers of our programs. Comments need to be updated as the software develops.Comments need to be updated as the software develops. Do not obscure your code by mixing it with too many comments.Do not obscure your code by mixing it with too many comments. Place a comment block above the code that it is explaining.Place a comment block above the code that it is explaining. Comments do not increase the size of the executable file.Comments do not increase the size of the executable file.

26 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 26Comments In C++, there are two different comment delimiters: the comment pair (/*, */),the comment pair (/*, */), the double slash (//).the double slash (//). The comment pair is identical to the one used in C: The sequence /* indicates the beginning of a comment.The sequence /* indicates the beginning of a comment. The compiler treats all text between a /* and the following */ as a comment.The compiler treats all text between a /* and the following */ as a comment. A comment pair can be multiple lines long and can be placed wherever a tab, space, or newline is permitted.A comment pair can be multiple lines long and can be placed wherever a tab, space, or newline is permitted. Comment pairs do not nest.Comment pairs do not nest.

27 January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 27Comments The double slash serves to delimit a single-line comment: Everything on the program line to the right of the delimiter is treated as a comment and ignored by the compiler. Everything on the program line to the right of the delimiter is treated as a comment and ignored by the compiler. A typical program contains both types of comments. In general, use comment pairs to explain the capabilities of a class and the double slash to explain a single operation.


Download ppt "January 30, 2014CS410 – Software Engineering Lecture #2: Hello, C++ World! 1Ubuntu You will develop your course projects in C++ under Ubuntu Linux. If."

Similar presentations


Ads by Google