CS103 COMPUTER PROGRAMMING LECTURE 1 Hafiza Maria Maqsood

  • Slides: 16
Download presentation
CS-103 COMPUTER PROGRAMMING LECTURE 1 Hafiza Maria Maqsood FAST NU – CHINIOT FSD campus

CS-103 COMPUTER PROGRAMMING LECTURE 1 Hafiza Maria Maqsood FAST NU – CHINIOT FSD campus Hafiza Maria Maqsood, FAST-NU, Chiniot FSD Campus 1

Tentative Evaluation Breakdown Assignments 10 Quizzes 20 Project 5 Sessional - 1 12. 5

Tentative Evaluation Breakdown Assignments 10 Quizzes 20 Project 5 Sessional - 1 12. 5 Sessional - 11 12. 5 Final 40 Note: The evaluation Breakdown plus course outline for all sections will be same Grading will be combined so don’t rely on your class position look for the batch position Hafiza Maria Maqsood, FAST-NU, Chiniot - FSD Campus 2

Text Books “Object Oriented Programming in C++”, Author (s): Robert Lafore. “C++ How to

Text Books “Object Oriented Programming in C++”, Author (s): Robert Lafore. “C++ How to program”, Author (s): M. Deitel & Dietel. “Turbo C Programming”, Author (s): Robert Lafore. Hafiza Maria Maqsood, FAST-NU, Chiniot - FSD Campus 3

Load into Making Software memory and start Problem Develop Algorithm execution Executable file Compile

Load into Making Software memory and start Problem Develop Algorithm execution Executable file Compile the code Express Algorithm in the form of pseudo code or flow chart Write algorithm in C/C++/Java … Language Writing code in “C++” language means, we must have to follow a certain rules of “C++” language. This is called syntax of “C++” language. Hafiza Maria Maqsood, FAST-NU, Chiniot - FSD Campus 4

Load into Making Software memory and start Problem Develop Algorithm Compiler “Turbo C” in

Load into Making Software memory and start Problem Develop Algorithm Compiler “Turbo C” in DOS “gcc” or “g++” in Linux “Visual C++” in windows “Borland” in DOS/Windows. execution Executable file Compile the code Express Algorithm in the form of pseudo code or flow chart Write algorithm in C/C++/Java … Language Writing code in “C++” language means, we must have to follow a certain rules of “C++” language. This is called syntax of “C++” language. Hafiza Maria Maqsood, FAST-NU, Chiniot - FSD Campus 5

Basics of a Typical C++ Environment Phases of C++ Programs: 1. Edit 2. Preprocess

Basics of a Typical C++ Environment Phases of C++ Programs: 1. Edit 2. Preprocess 3. Compile 4. Link 5. Load 6. Execute Editor Preprocessor Compiler Linker Loader Program is created in the editor and stored on disk. Disk Preprocessor program processes the code. Disk Compiler creates object code and stores it on disk. Disk Linker links the object code with the libraries, creates a. out and stores it on disk Primary Memory Loader puts program in memory. Disk CPU Hafiza Maria Maqsood, FAST-NU, Chiniot - FSD Campus Disk . . . Primary Memory . . . CPU takes each instruction and executes it, possibly storing new data values as the program executes. 6

1 // Fig. 1. 2: fig 01_02. cpp 2 // A first program in

1 // Fig. 1. 2: fig 01_02. cpp 2 // A first program in C++ Comments Written between /* and */ or following a //. 3 #include <iostream> Improve program readability and do not cause the computer to perform any action. 4 5 int main() 6 { preprocessor directive to the C++ preprocessor. 7 std: : cout << "Welcome to Message C++!n"; Lines beginning with # are preprocessor directives. 8 #include <iostream> tells the preprocessor to include contents of the file <iostream>, 9 return 0; // indicate thatthe program which includes input/output operations (such as ended successfully 10} C++ programs one or more functions, one printing to thecontain screen). of which must be main Parenthesis are used to indicate a function Welcome to C++! int means that main "returns" an integer value. Prints the string of characters contained More in Chapter 3. between the quotation marks. return is a way to exit a function from a function. A left brace { begins The entire line, including std: : cout, the << body of every return 0, in this case, means that function and brace and } ends it. operator, the string "Welcome toa right C++!n" the program terminated thenormally. semicolon (; ), is called a statement. All statements must end with a semicolon. Hafiza Maria Maqsood, FAST-NU, Chiniot - FSD Campus 7

A Simple Program: Printing a Line of Text std: : cout Standard output stream

A Simple Program: Printing a Line of Text std: : cout Standard output stream object “Connected” to the screen std: : specifies the "namespace" which cout belongs to << std: : can be removed through the use of using statements Stream insertion operator Value to the right of the operator (right operand) inserted into output stream (which is connected to the screen) std: : cout << “Welcome to C++!n”; Escape character Indicates that a “special” character is to be output 8

A Simple Program: Printing a Line of Text 9

A Simple Program: Printing a Line of Text 9

File Handling stream - a sequence of characters interactive (iostream) cin - input stream

File Handling stream - a sequence of characters interactive (iostream) cin - input stream associated with keyboard. cout - output stream associated with display. file (fstream) ifstream - defines new input stream (normally associated with a file). ofstream - defines new output stream (normally associated with a file). Hafiza Maria Maqsood, FAST-NU, Chiniot - FSD Campus 10

To access file handling routines: #include <fstream. h> 2: To declare variables that can

To access file handling routines: #include <fstream. h> 2: To declare variables that can be used to access file: ifstream in_stream; ofstream out_stream; 3: To connect your program's variable (its internal name) to an external file : in_stream. open("infile. txt"); out_stream. open("outfile. txt"); 4: To see if the file opened successfully: if (in_stream. fail()) { cout << "Input file open failedn"; exit(1); // requires <stdlib. h>}

To get data from a file (one option), must declare a variable to hold

To get data from a file (one option), must declare a variable to hold the data and then read it using the extraction operator: int num; in_stream >> num; [Compare: cin >> num; ] 6: To put data into a file, use insertion operator: out_stream << num; [Compare: cout << num; ] NOTE: Streams are sequential – data is read and written in order – generally can't back up. 7: When done with the file: in_stream. close(); out_stream. close();

File Modes Name Description ios: : in Open file to read ios: : out

File Modes Name Description ios: : in Open file to read ios: : out Open file to write ios: : app All the date you write, is put at the end of the file. It calls ios: : out ios: : ate All the date you write, is put at the end of the file. It does not call ios: : out ios: : trunc Deletes all previous content in the file. (empties the file) ios: : nocreate If the file does not exist, opening it with the open() function gets impossible. ios: : noreplace If the file exists, trying to open it with the open() function, returns an error.

File I/O Example: Writing #include <fstream> using namespace std; int main(void) { ofstream out.

File I/O Example: Writing #include <fstream> using namespace std; int main(void) { ofstream out. File(“fout. txt"); out. File << "Hello World!"; out. File. close(); return 0; }

File I/O Example: Reading #include <iostream> #include <fstream> int main(void) { ifstream open. File(“data.

File I/O Example: Reading #include <iostream> #include <fstream> int main(void) { ifstream open. File(“data. txt"); //open a text file data. txt char ch; while(!Open. File. eof()) { Open. File. get(ch); cout << ch; } Open. File. close(); } return 0;

File I/O Example: Reading #include <iostream> #include <fstream> #include <string> int main(void) { ifstream

File I/O Example: Reading #include <iostream> #include <fstream> #include <string> int main(void) { ifstream open. File(“data. txt"); //open a text file data. txt string line; } if(open. File. is_open()){ // while(!open. File. eof()){ getline(open. File, line); //read a line from data. txt and put it in a string cout << line; } else{ cout<<“File does not exist!”<<endl; exit(1); } } open. File. close(); return 0;