Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming 1 (CS112) Dr. Mohamed Mostafa Zayed 1.

Similar presentations


Presentation on theme: "Programming 1 (CS112) Dr. Mohamed Mostafa Zayed 1."— Presentation transcript:

1 Programming 1 (CS112) Dr. Mohamed Mostafa Zayed mzayed@taibahu.edu.sa 1

2 Outline An idea about program and algorithm design and application of C++ language Variables, data types Input / Output functions Arithmetic and relational operators and mathematical expressions Control statements through the – conditional statement with its different types – looping with its types Matrices of one and two dimensions – Functions – Pointers – an Sturcts. 2

3 Books 3 Main Textbook: – C++: How To Program, Deitel and Deitel, 4th edition, Prentice Hall, 2002. – Available from Taibah University Library Alternative: – Any book that introduces computer science and programming principles using C++. – NB: should be an introductory book on C++.

4 Schedules & Assesment system Periodical test1 (20%) – ( Last week before mid-term holiday ) 3:00~4:00 – Tue 1/5/1432, 5/4/2011 Periodical test2 (20%) Projects (10%) Lab assignments and attendance (10%) Final (40%) 4

5 Programming with C++ 5

6 Learning C++ C++ joins three separate programming traditions – The procedural language tradition – The object-oriented language tradition, represented by the class enhancements C++ adds to C – Generic programming, supported by C++ templates. 6

7 Procedural Languages Earlier procedural languages, such as FORTRAN and BASIC, ran into organizational problems as programs grew larger. For example, programs often use branching statements, which route execution to one or another set of instructions depending upon the result of some sort of test. It was virtually impossible to understand a program by reading it 7

8 Structured programming structured programming limits branching (choosing which instruction to do next) to a small set of well-behaved constructions. C incorporates these constructions (the for loop, the while loop, the do while loop, and the if else statement) into its vocabulary. 8

9 Object Oriented Programming (OOP) Encapsulation – The technical term for combining data and functions together as a bundle is encapsulation. Inheritance – lets you derive new classes from old ones. Polymorphism – Poly refers many. So Polymorphism as the name suggests is a certain item appearing in different forms or ways. That is making a function or operator to act in different forms depending on the place they are present is called Polymorphism. 9

10 Generic Programming The term generic means to create code that is type- independent. generic programming provides tools for performing common tasks, such as sorting data or merging lists. for example, if you want to sort data of various types, you normally have to create a separate sorting function for each type. Generic programming involves extending the language so that you can write a function for a generic (that is, not specified) type once, and use it for a variety of actual types. C++ templates provide a mechanism for doing that. 10

11 Portability and Standards If you move the program to another platform Can you run your program on the new platform? Of course, you'll have to recompile the program using a C++ compiler designed for the new platform. But will you have to make any changes to the code you wrote? If you can recompile the program without making changes and it runs without a change, we say the program is portable. 11

12 Portability and Standards Cont. There are a couple of obstacles to portability, the first of which is hardware. The second obstacle to portability is language divergence. Although most implementers would like to make their versions of C++ compatible with others, it's difficult to do so without a published standard describing exactly how the language works. Therefore, the American National Standards Institute (ANSI) created a committee in 1990 to develop a standard for C++. 12

13 The Mechanics of Creating a Program 13

14 Creating the Source Code 14

15 Compiler?? 15 Compiler Is a computer program that translates a high-level language to an object program.

16 Hello World program #include using namespace std; int main() { cout << “Hello World"; return 0; } 16

17 Explanation of Hello World program There are other common header files. #include 17

18 #include Preprocessor directive Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. The directive #include tells the preprocessor to include the iostream standard header file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++. There are other common header files. #include

19 using namespace std; All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the name std. So in order to access its functionality we declare with this expression that we will be using these entities. This line is very frequent in C++ programs that use the standard library.

20 int main(void){ } – All C++ programs begin processing at the first executable statement in main. – The “()” indicate that main is a block of code called a function. – The int in front of main declares main to be a function that is expected to “return" an integer. – The lines of code in between the “{ }” is called the function body. 20

21 Using Cout To print a value to the screen, write the word cout, followed by the insertion operator (<<) cout << “Welcome to C++"; 21

22 Comments Comments are simply text that is ignored by the compiler, but that may inform the reader of what you are doing at any particular point in your program. Types of Comments – The double-slash (//) comment – The slash-star (/*) comment. 22

23 Escape Sequences 23 cout << "Here is a very big number:\t" << 70000 <<

24 Example1 #include using namespace std; int main() {cout<<“Welcome”; cout<<“to ”; cout<<“c++”; return 0; } Output ?? Welcome to c++ 24

25 Example2 Int main() {cout<<“Welcome\nto\nc++”; } Output Welcome To C++ 25

26 Case Sensitivity C++ is case-sensitive. In other words, uppercase and lowercase letters are considered to be different. A variable named age is different from Age, which is different from AGE. 26


Download ppt "Programming 1 (CS112) Dr. Mohamed Mostafa Zayed 1."

Similar presentations


Ads by Google