Download presentation
Presentation is loading. Please wait.
Published byKristin Whitehead Modified over 9 years ago
1
SPL Practical Session 1
2
Administrative Issues Email: majeek@cs.bgu.ac.il@cs.bgu.ac.il Reception Hour: TBD Course Site: www.cs.bgu.ac.il/~spl141www.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/~spl141www.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 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(int argc, char**argv).
10
Using cout is a reference to the standard output stream, just like Java's System.out object. 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: int add(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 #ifndef uniqueName_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/PracticalSessio n01/HelloWorld
14
C++ Types
15
Primitive Types Type NameSize in BytesRange of Values int4–2,147,483,648 to 2,147,483,647 long4 double8-/+ 1.7E +/- 308 bool1false (0), true (1/non-zero) char1 –128 to 127 corresponds to letters: Full table: http://www.asciitable.com/ Examples: int days = 0; bool flag = 1; bool flag = true; double x = 2.511;
16
Constants Just like Java’s Final command: const int x= 5; const int DAYS_PER_YEAR = 365;
17
String Class: #include Namespace: std::string Declaration: std::string str = “string content”; Example: #include using namespace std; string str = "We go step by step to the target";
18
C++ String Properties Non unicode: – std:string hebrew = ” רכילות “; 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/ FunctionExampleResultExplanation []str[0]Iletter at index length()/size()len=str.length()len=11length of string substr(pos, length) res = str.substr(2,2) res =“am” substring of str starting at pos 2 with length 2 insert(pos, str) str.insert (4,”not ”)“i am not an str” inserts “not “ at position 4. pos can be a number or an iterator. find(otherStr)index = str.find(“am”); index = 2Finds the starting index of otherStr inside str. In case of no match, npos is returned (defined as -1)
20
Vectors Just like Arrays, but much safer to use! Class: #include Namespace: using namespace std; Example: vector a; a.push_back(1); a.push_back(5); a.push_back(3); int aSize = a.size(); aSize = Full Information: http://www.cplusplus.com/reference/stl/vector/ 153 3
21
(some of) Vector Functions const int tmp[] = {1,3,5}; vector a(tmp,tmp+3); Full list of commands: http://www.cplusplus.com/reference/stl/vector/ FunctionExampleResultExplanation [index]a[0]1Returns element at index size()size = a.size()size=3Returns number of elements push_back(element)a.push_back(7)[1,3,5,7]Adds element at end of vector pop_back()a.pop_back()x=5Removes and returns last element of vector back()x=a.back()x=5Returns last element of vector at(index)x=a.at(0)x=1Returns element at index. begin()/end()iter1=a.begin()/ iter2=a.end() *iter1 = 1 *iter2 = 5 Returns iterator on first/last element of 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 vec; vec[0] = 1; //is illegal vec.resize(2); vec[1] = 2;// is legal vec: 01
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 HelloWorld.cppHelloWorld.h Run.cpp 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/PracticalSessio n01/Makefile http://www.cs.bgu.ac.il/~spl141/PracticalSessio n01/Makefile Compiling C++ (and makefile)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.