C++ For Science and Engineering Lecture 2 John Chrispell Tulane University Wednesday August 25, 2010
Basic Linux Commands Command ls pwd cd What it does. lists the files in the current directory prints the working directory if no argument is given you are changed to working in your home directory chaged working in the path directory Copies old.file to new file cd path cp file copied.file rm file removes file mkdir name makes a directory called name John Chrispell, Wednesday August 25, 2010 slide 2/12
Basic Linux Commands On the command line an * is a generic place holder of unspecified length. John Chrispell, Wednesday August 25, 2010 slide 3/12
Basic Linux Commands On the command line an * is a generic place holder of unspecified length. A? is a generic place holder of length one. John Chrispell, Wednesday August 25, 2010 slide 3/12
Basic Linux Commands On the command line an * is a generic place holder of unspecified length. A? is a generic place holder of length one. The symbol. means this working directory. Achieves absoloutly nothing! Example cd./ John Chrispell, Wednesday August 25, 2010 slide 3/12
Basic Linux Commands On the command line an * is a generic place holder of unspecified length. A? is a generic place holder of length one. The symbol. means this working directory. Achieves absoloutly nothing! Example cd./ The symbol.. means back one directory. moves up one directory. Example cd.. John Chrispell, Wednesday August 25, 2010 slide 3/12
Basic Linux Commands You can look up what commands do by using the man command: Example: man ls tells you what ls does and how to use it. John Chrispell, Wednesday August 25, 2010 slide 4/12
Basic Linux Commands You can look up what commands do by using the man command: Example: man ls tells you what ls does and how to use it. The < tab > is also very useful. John Chrispell, Wednesday August 25, 2010 slide 4/12
The main() function Recall the simple program from last class. John Chrispell, Wednesday August 25, 2010 slide 5/12
Parts of a Program Every program has a main function. John Chrispell, Wednesday August 25, 2010 slide 6/12
Parts of a Program Every program has a main function. The main function is called main(). John Chrispell, Wednesday August 25, 2010 slide 6/12
Parts of a Program Every program has a main function. The main function is called main(). In the example given it is of integer type or int John Chrispell, Wednesday August 25, 2010 slide 6/12
Parts of a Program Every program has a main function. The main function is called main(). In the example given it is of integer type or int Note the return statement at the bottom. John Chrispell, Wednesday August 25, 2010 slide 6/12
Parts of a Program Every program has a main function. The main function is called main(). In the example given it is of integer type or int Note the return statement at the bottom. Spelling and capitalization counts! John Chrispell, Wednesday August 25, 2010 slide 6/12
Comments in your code! You do not only write code for yourself. Be nice to yourself and anyone reading your code. This means code should be well commented. It also means that your code should be well blocked (4 space indenting is a good rule). You never know when you are going to have to come back to something your have written again in the future. I have to grade it. code // comment c++ style code /* comment c style */ John Chrispell, Wednesday August 25, 2010 slide 7/12
The iostream file John Chrispell, Wednesday August 25, 2010 slide 8/12
The iostream file The #include < iostream > allows the program to have input and out put streams. John Chrispell, Wednesday August 25, 2010 slide 8/12
The iostream file The #include < iostream > allows the program to have input and out put streams. The include statment is replaced by the file iostream durring preprocessing when the code is compiled. John Chrispell, Wednesday August 25, 2010 slide 8/12
The iostream file The #include < iostream > allows the program to have input and out put streams. The include statment is replaced by the file iostream durring preprocessing when the code is compiled. In moderm C++ the header files do not have the.h extension that you may be used to in C or FORTRAN. John Chrispell, Wednesday August 25, 2010 slide 8/12
Namespaces John Chrispell, Wednesday August 25, 2010 slide 9/12
Namespaces The is called using a directive. using namespace std; John Chrispell, Wednesday August 25, 2010 slide 9/12
Namespaces The is called using a directive. using namespace std; It allows for vendors or programmers use more than one function with the same name. John Chrispell, Wednesday August 25, 2010 slide 9/12
Namespaces The is called using a directive. using namespace std; It allows for vendors or programmers use more than one function with the same name. In the sample program the cout function lives in the std namespace. John Chrispell, Wednesday August 25, 2010 slide 9/12
Namespaces The is called using a directive. using namespace std; It allows for vendors or programmers use more than one function with the same name. In the sample program the cout function lives in the std namespace. We could have used code that looked like: std::cout << This is my first c++ program. ; if we had omitted the using namespace statement; John Chrispell, Wednesday August 25, 2010 slide 9/12
cout itself John Chrispell, Wednesday August 25, 2010 slide 10/12
cout itself In the line std::cout << This is my first c++ program. ; cout - is an object (a particular instance of a class). << - is the insertion operator This is my first c++ program. - is a string. John Chrispell, Wednesday August 25, 2010 slide 10/12
cout itself In the line std::cout << This is my first c++ program. ; cout - is an object (a particular instance of a class). << - is the insertion operator This is my first c++ program. - is a string. We don t know anything about the object cout we just know how to use it. John Chrispell, Wednesday August 25, 2010 slide 10/12
cout itself In the line std::cout << This is my first c++ program. ; cout - is an object (a particular instance of a class). << - is the insertion operator This is my first c++ program. - is a string. We don t know anything about the object cout we just know how to use it. If you know C then << looks just like the bitwise left-shift operator. This is an example of operator overloading. John Chrispell, Wednesday August 25, 2010 slide 10/12
endl endl is the end line manipulator (as it has special meaning to cout). John Chrispell, Wednesday August 25, 2010 slide 11/12
endl endl is the end line manipulator (as it has special meaning to cout). We may also use the new line character \n in a string: cout << \n ; // starts a new line cout << endl; // starts a new line C++ programmers would argue that endl is easier to type. John Chrispell, Wednesday August 25, 2010 slide 11/12
White Space Spacing in the code helps make it readable. C++ allows statments to be spread over multiple lines. You can t have spacing in some places! Demo. John Chrispell, Wednesday August 25, 2010 slide 12/12