Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS-103 COMPUTER PROGRAMMING

Similar presentations


Presentation on theme: "CS-103 COMPUTER PROGRAMMING"— Presentation transcript:

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

2 Tentative Evaluation Breakdown
Assignments 10 Quizzes 20 Project 5 Sessional - 1 12.5 Sessional - 11 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

3 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

4 Making Software Load into memory and start execution Problem
Executable file Develop Algorithm 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 Making Software Load into memory and start execution Compiler
“Turbo C” in DOS “gcc” or “g++” in Linux “Visual C++” in windows “Borland” in DOS/Windows. Problem Executable file Develop Algorithm 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

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

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

8 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

9 A Simple Program: Printing a Line of Text

10 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

11 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 failed\n"; exit(1); // requires <stdlib.h>}

12 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();

13 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.

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

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

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


Download ppt "CS-103 COMPUTER PROGRAMMING"

Similar presentations


Ads by Google