280 likes | 305 Views
Explore coding guidelines, Java to C++ conversion, Unix tutorials, and HW #1 assignment. Learn basic Linux commands, navigate the file system, and use fundamental C++ features.
E N D
Software System Design • Lecture 1 • CS240 • C++ and Linux
Administrative • Read handouts on coding guidelines • Read “Java to C++” handout • Read “Unix Tutorial for Beginners” (Intro, Tutorial 1, Tutorial 2 – links on class web page) • HW #1 assigned • Due Monday, 8/30 at 11:59pm
Documentation • Code standards and additional guidelines (class web page) • Java2C++ guide (supplemental materials – class web page) • Online Resources • UR CS Tools • Unix documentation (online manual pages) • C++ documentation (see class web page for links)
Linux • Linux accounts • Log in using UR netid and password • 10 machines - 6 in 225, 4 in 228 (locked) • ats-jps-g30-l1 aka mathcs01 • ats-jps-g30-l2 aka mathcs02 • ... • ats-jps-g30-l10 aka mathcs10
Linux • Linux is an operating system just like Windows and MacOS • Linux uses a command prompt • Lots of commands typed in window • Kind of like DOS command prompt used for compiling in CS150
Hierarchical File Systems / dev etc home usr console lbarnett blawson share cs240 cpp_examples README
Linux File System • Hierarchical • Special directory names: • Root directory: “/” • Current directory: “.” • Parent directory: “..” (allows you to go up) • User’s home directory: “~” • Some other user’s home: “~lbarnett” • Using given names, only need two operations to navigate entire name space: • cd <name> : move into directory “name” • ls : enumerate all names in current directory
Linux Documentation:man Pages Note: Also see Unix documentation placed on web page
C++ Intro:Arrays #include <iostream> using namespace std; int main() { int num[10]; //array of integers for(int i = 0; i < 10; i++){ //read each integer in from user cin >> num[i]; } cout << “User entered”; for(int i = 9; i >= 0; i--){ cout << num[i] << “ “; } } Reminder: Arrays do NOT have a length method
C++ Intro:Functions #include <iostream> using namespace std; intfoo(); // declaration int main() { cout << “Foo “<< foo(); //call } intfoo() //definition { return 5; } • Global functions • ex. main • Function declarations required if used before defined
C++ Intro:Functions #include <iostream> using namespace std; intfoo() //definition { return 5; } int main() { cout << “Foo “<<foo(); //call } • Global functions • ex. main • Function declarations required if used before defined
C++ Intro:Function Parameters Pass-by-copy • Pass-by-copy • Copy of variable sent to function • Changes to variable in function not reflected at caller • Also called “pass-by-value” void foo(int x) { x = 5; } int main() { int x = 3; foo(x); cout << “value of x is “<< x; }
C++ Intro:Function Parameters Pass-by-reference • Pass-by-reference • Reference to variable in caller sent to function • Changes to variable in function reflected at caller void foo(int &x) { x = 5; } int main() { int x = 3; foo(x); cout << “value of x is “<< x; }
C++ Intro:C-style strings int main() { char foo[10] = “bob”; //access individual chars foo[2] = ‘t’; // “bot” } • Array of null terminated chars • #include <string.h> to perform functions on these strings • strcpy(dest, src) • copies src into dest • strcat(dest, src) • appends src to dest • More functions in Standard C string library (man string)
C++ Intro:strings • To use, #include <string> • Declarations and initializations: • string s1(“Amsterdam, NY”); • string s2 = “Amsterdam, NY”; • Lots of other ways; see documentation • Can index into like c-style strings • s2[0] will return ‘A’
C++ Intro:string Methods Find more functions as part of C++ Standard Template Library
C++ Intro:strings • strings can be compared with standard relational operations • > , <, == • strings can be concatenated with “+” and “+=“
C++ Intro:Command-line Arguments • You can specify arguments to your executable when you execute it • Parameters to main() must be: • int argc • Number of arguments specified at command line • char *argv[] • Array of C-style strings • argv[0] is always executable name
C++ Intro:Input and Output • Streams • Input / output streams • #include <iostream> • std::cin / std::cout • File input / output streams • #include <fstream> • ifstream/ ofstream • Streams for console and files all work the same
std::cin/std::cout • The >> stream input operator reads whitespace-delimited words / numbers • To read entire line, use getline(cin, var) • The stream output operator << prints either text or variables
using namespace std; #include <iostream> #include <fstream> #include <string> int main() { ifstream infile; infile.open("phone.txt"); if(infile.is_open() == false){ cerr << "Unable to open file " << endl; exit(1); } while(infile.good()){ string firstName, lastName; int phoneNum; infile >> firstName >> lastName >> phoneNum; if(infile.fail()) break; cout << firstName << "'s number: " << phoneNum << endl; } infile.close(); }
Together • Write a main method that • is passed a single file name via command line arguments, • passes that C-style string to a method • which writes the values from 1 to 10 into the file using a loop.