290 likes | 341 Vues
SPL. Practical Session 1. Administrative Issues. Email: jumanan@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.
E N D
SPL Practical Session 1
Administrative Issues • Email: jumanan@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.
Topics • Introduction to Linux • Introduction to C++ • C++ Types • Compiling & Linking C++ Programs
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.
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.
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.
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
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.
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).
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);
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.
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.
Our First C++ Program • STEP-BY-STEP http://www.cs.bgu.ac.il/~spl141/PracticalSession01/HelloWorld
Primitive Types Examples: int days = 0; bool flag = 1; bool flag = true; double x = 2.511;
Constants • Just like Java’s Final command: • constint x= 5; • constint DAYS_PER_YEAR = 365;
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";
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)
(some of)String Functions • std::string str = “i am an str”; • Full list of commands: http://www.cplusplus.com/reference/string/string/
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
(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/
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:
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.
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
helloWorld (executable binary) helloWorld.o run.o Run.cpp HelloWorld.h HelloWorld.cpp HelloWorld.h Dependency tree of the program
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.
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
STEP-BY-STEP http://www.cs.bgu.ac.il/~spl141/PracticalSession01/Makefile Compiling C++ (and makefile)