1 / 29

SPL

SPL. Practical Session 1. Administrative Issues. Email: majeek @cs.bgu.ac.il Reception Hour : TBD Course Site: www.cs.bgu.ac.il/~spl1 4 1 Assignments: 4-5 assignments. (in pairs) Targilonim – individual; obligatory submission; no grade.

maire
Télécharger la présentation

SPL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SPL Practical Session 1

  2. Administrative Issues • Email: majeek@cs.bgu.ac.il • Reception Hour: TBD • Course Site: www.cs.bgu.ac.il/~spl141 • Assignments: 4-5 assignments. (in pairs) • Targilonim – individual; obligatory submission; no grade. • The practical sessions contain a lot of detail; won’t go over it all in the tirgulim.

  3. Topics • Introduction to Linux • Introduction to C++ • C++ Types • Compiling & Linking C++ Programs

  4. Introduction to Linux • Linux is an operating system. • Getting familiar with your Linux account. • Log into your Linux accounts – the ones you created in Tirgul0 • Go to course site: www.cs.bgu.ac.il/~spl141 => Practical Sessions => Tirgul1 – Introduction to Linux.

  5. Enter your CS-account terminal STEP-BY-STEP Note: Linux is case sensitive (Ls ≠ ls). • Click the K-menu button (parallel to Window's Start) • Application • System • Konsole or Terminal You are now logged in to the terminal of the local computer.

  6. Verify your Terminal • Check your present-working-directory, type in the terminal: pwd • You should see: /users/studs/bsc/2014/your-username Obviously, the line is different per student.

  7. Directories • /users/studs/bsc/2014/your-username • / is the root directory just like C:/ in windows • everything between / / is a directory name: • users, studs, bsc, 2014, are all directory names. • This is your home directory. • 400M (stud) • Backed-up regularly (type: cd .snapshot) • It is recommended to save all your work here. • Freespace directory:  /freespace/stud/your-name • 2G (stud) • Not backed up. (might get deleted if  nature disasters occur) • For more information read: http://frodo.cs.bgu.ac.il/wiki/Accounts/Storage_Space

  8. Basic Commands STEP-BY-STEP • ls – list files and directories. • cd – change directory • du – estimate file space usage • man – display manual • cp - copy • rm – remove (to delete files and directories) • cat - concatenate files and print on the standard output • mkdir – make dir; create a directory.

  9. Introduction to C++ • C++ has both Free /global functions and class functions/methods. • The same goes for variables. • Avoid declaring functions/variables in the global scope • The main function: • main is a unique free function name; execution starts from it. • returns an int value, and receives the command line arguments: int main(intargc, char**argv).

  10. Usingcoutis a reference to the standard output stream, just like Java's System.outobject. Example: std::cout << "Hello" << std::endl • Function Declaration and Definition: • Function Definition: contains both a prototype and a body. Example: • int add(int a, int b) { return a + b; }; • Function Declaration: contains only the prototype followed by a semicolon. Example: • intadd(int, int);

  11. Forward Declaration: declaration of a variable/function/class not yet implemented. • Used by the compiler to generate the object code. • #include: like java import; however, unlike java it copies the content of the included file to the current file. • be careful as to what you include. As a rule, include only header files • Namespaces: Namespaces are somewhat similar to Java packages.

  12. Header Files • Ends with  .h or .hpp • contains forward declarations. • Similar to interfaces in java. //Always start your header file with #ifndefuniqueName_H_ #define uniqueName_H_ //and end it with #endif • C++ source files (*.cpp) contain the actual implementation.

  13. Our First C++ Program • STEP-BY-STEP http://www.cs.bgu.ac.il/~spl141/PracticalSession01/HelloWorld

  14. C++ Types

  15. Primitive Types Examples: int days = 0; bool flag = 1; bool flag = true; double x = 2.511;

  16. Constants • Just like Java’s Final command: • constint x= 5; • constint DAYS_PER_YEAR = 365;

  17. String Class: #include <string> Namespace: std::string Declaration: std::string str = “string content”; Example: #include <string> using namespace std; string str = "We go step by step to the target";

  18. C++ String Properties • Non unicode: • std:stringhebrew = ”רכילות“; Might show something else on different OS • Mutable. Can be changed! • str.insert(str.end(),3,'!');   Result: "We go step by step to the target!!!“ • Concatenate with other strings only! Not with other objects (like int-s etc)

  19. (some of)String Functions • std::string str = “i am an str”; • Full list of commands: http://www.cplusplus.com/reference/string/string/

  20. Vectors • Just like Arrays, but much safer to use! • Class: #include <vector> • Namespace: using namespace std; • Example: vector<int> a;      a.push_back(1); a.push_back(5); a.push_back(3); intaSize = a.size(); aSize = • Full Information: http://www.cplusplus.com/reference/stl/vector/ 3

  21. (some of) Vector Functions • constinttmp[] = {1,3,5}; • vector<int> a(tmp,tmp+3); • Full list of commands: http://www.cplusplus.com/reference/stl/vector/

  22. When we declare a vector its initial size is zero. • You need to initialize the vector before you can use [], or at(). • You can initialize the vector with zeros by using the function resize(). • Example: std::vector<int> vec; vec[0] = 1; //is illegal vec.resize(2); vec[1] = 2;// is legal vec:

  23. Compiling & Linking C++ Programs

  24. Preprocessor:accepts source code(*.cpp); removes comments; add the content of the include files. Compiler:translates source to assembly code (AND, OR, JMP, SUB etc.). Assembler:creates object code (machine code, 0-s and 1-s, *.o files). Linker: links the object files to an executable and creates an executable machine code; marks the main() as the starting point of the execution.

  25. Example Program We have a program that contains 3 files: • Run.cpp, HelloWorld.cpp, HelloWorld.h • HelloWorld.h included in both .cpp files • Executable should be the file helloWorld

  26. helloWorld(executable binary) helloWorld.o run.o Run.cpp HelloWorld.h HelloWorld.cpp HelloWorld.h Dependency tree of the program

  27. Compiling C++ STEP-BY-STEP • C++ compilation produces machine specific binary files. • You cannot run the executable on an OS that is not compatible with OS that compiled the code.

  28. Some of g++ flags • -c: compile file. input:file.cpp, output: file.o. File is not executable! • -o fileName Place output in file fileName. By default it will create a file called: a.out • -g: produces debugging information for debugger. • -Wall: prints all errors/warnings in detail. • -Weffc++: prints errors/warning that violate the guidelines in “Effective C++” and “More Effective C++” books. • Read more: http://www.programmingforums.org/thread7219.html

  29. STEP-BY-STEP http://www.cs.bgu.ac.il/~spl141/PracticalSession01/Makefile Compiling C++ (and makefile)

More Related