Programming Introduction to C++.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.
Advertisements

Chapter 3: Beginning Problem Solving Concepts for the Computer Programming Computer Programming Skills /1436 Department of Computer Science.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
C++ Basics. COMP104 C++ Basics / Slide 2 Introduction to C++ * C++ is a programming language for manipulating numbers and user-defined objects. * C++
Three types of computer languages
Outline Java program structure Basic program elements
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Programming Introduction to C++.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
CH1 – A 1 st Program Using C#. Program Set of instructions which tell a computer what to do. Machine Language Basic language computers use to control.
Introduction COMP104: Fundamentals and Methodology.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Introduction to C++ Programming Introduction to C++ l C is a programming language developed in the 1970's alongside the UNIX operating system. l C provides.
© 2006 Pearson Education 1 Obj: cont 1.3 and 1.4, to become familiar with identifiers and to understand how programming languages work HW: p.51 #1.8 –
The Java Programming Language
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
1 Problem Solving with C++ The Object of Programming Walter Savitch Chapter 1 Introduction to Computers and C++ Programming Slides by David B. Teague,
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
Introduction to C++ Programming COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 2 Introduction to C++ l C is a programming language developed.
CPS120: Introduction to Computer Science Variables and Constants.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Software Engineering Algorithms, Compilers, & Lifecycle.
By Kundang K Juman Hardware & Software. COMP102 Prog. Fundamentals I: Software / Slide 2 l Four components of a computer system: n CPU - central processing.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
Chapter 1: Introduction to Computers and Programming
Bill Tucker Austin Community College COSC 1315
C++ Lesson 1.
Variables, Identifiers, Assignments, Input/Output
C++ First Steps.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Working with Java.
Chapter 1.2 Introduction to C++ Programming
Basics (Variables, Assignments, I/O)
Chapter 1: Introduction to computers and C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
CSCI-235 Micro-Computer Applications
Computing and Statistical Data Analysis Lecture 2
Computing Fundamentals
Basic Elements of C++.
Mr. Shaikh Amjad R. Asst. Prof in Dept. of Computer Sci. Mrs. K. S
Programming COMP104: Fundamentals and Methodology Introduction.
Basic Elements of C++ Chapter 2.
Basics (Variables, Assignments, I/O)
2.1 Parts of a C++ Program.
Variables in C Topics Naming Variables Declaring Variables
Chapter 1: Computer Systems
Hardware & Software Programming. COMP102 Prog. Fundamentals I: Software / Slide 2 l Four components of a computer system: n CPU - central processing unit.
Variables, Identifiers, Assignments, Input/Output
Units with – James tedder
Units with – James tedder
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C++ Programming
Focus of the Course Object-Oriented Software Development
Chapter 2: Introduction to C++.
C programming Language
Programming Introduction to C++.
Variables in C Topics Naming Variables Declaring Variables
Computer Programming-1 CSC 111
Variables in C Topics Naming Variables Declaring Variables
Chapter 1 c++ structure C++ Input / Output
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Programming Introduction to C++

What is a (programming) language? A sequence of instructions A program (in computer language) An algorthm (in human language) A program needs to be written in a language There are many programming languages Low-level, understandable by a computer High-level, needs a translator! C++ is a high level programming language High-level lang., easy Low-level, hard

Levels of programming language Machine binary language: unintelligible Low-level assembly language Mnemonic names for machine operations Explicit manipulation of memory addresses Machine-dependent High-level language Readable Machine-independent

An example: Machine binary language Low-level assembly High-level

How to translate? A program written in high-level programming language (for example, C++ program) COMPILER (for example, Visual C++) A low-level (machine language) program that is understandable by a computer (for example, a PC) Examples of compilers: Microsoft Visual C++, Eclipse, g++

What is a program? Work Worker Product Instructions Work Computer A computer program performs a specific task, and may interact with the user and the computer hardware. Human work model: Computer work model: Work Worker Product Instructions High-level lang., easy Low-level, hard Work Computer Product Program A program is a set of instructions

Introduction to C++ C is a programming language developed in the 1970's alongside the UNIX operating system. C provides a comprehensive set of features for handling a wide variety of applications, such as systems development and scientific computation. C++ is an “extension” of the C language, in that most C programs are also C++ programs. C++, as opposed to C, supports “object-oriented programming.”

We teach C++, Why? C++ is one of the most popular languages  C++ (originally C) Basic, Pascal, Java, Perl, Cobol, Scheme, Lisp, Smalltalk, Ada, … C++ is portable (runs on PC, Machintosh, Unix, Mainframes …) C++ is widely used in industries (almost by everyone)  C++ is not easy to learn  The most common versions of C++: Microsoft Visual C++ Eclipse g++ (for Unix machines) C++ C

Programming or Software Development Editing (to write the program) Compiling (creates .obj file) Linking with compiled files (creates .exe file) Object files Library modules Loading and executing Testing the program debug

Integrated Development Environments (IDE) Combine all of the capabilities that a programmer would want while developing software (VC++, Eclipse) Editor Compiler Linker Loader Debugger Viewer builder

General form of a C++ program // Program description #include directives int main() { constant declarations variable declarations executable statements return 0; }

C++ Features Classes User-defined types Operator overloading Attach different meaning to expressions such as a + b References Pass-by-reference function arguments Virtual Functions Dispatched depending on type at run time Templates Macro-like polymorphism for containers (e.g., arrays) Exceptions

C++ keywords Keywords appear in blue in Visual C++. Each keyword has a predefined purpose in the language. Do not use keywords as variable and constant names!! Keywords in this unit: bool, break, case, char, const, continue, do, default, double, else, extern, false, float, for, if, int, long, namespace, return, short, static, struct, switch, typedef, true, unsigned, void, while

Example 0 – adding 2 numbers Peter: Hey Frank, I just learned how to add two numbers together. Frank: Cool! Peter : Give me the first number. Frank: 2. Peter : Ok, and give me the second number. Frank: 5. Peter : Ok, here's the answer: 2 + 5 = 7. Frank: Wow! You are amazing! after Frank says “2”, Peter has to keep this number in his mind. after Frank says “5”, Peter also needs to keep this number in his mind. First number: 2 Second number: 5 7 Sum:

The Corresponding C++ Program #include <iostream> using namespace std; int main() { int first, second, sum; cout << "Peter: Hey Frank, I just learned how to add” << “ two numbers together."<< endl; cout << "Frank: Cool!" <<endl; cout << "Peter: Give me the first number."<< endl; cout << "Frank: "; cin >> first; cout << "Peter: Give me the second number."<< endl; cin >> second; sum = first + second; cout << "Peter: OK, here is the answer:"; cout << sum << endl; cout << "Frank: Wow! You are amazing!" << endl; return 0; }

Demo Example 1 #include <iostream> using namespace std; int main() { int number_of_pods, peas_per_pod, total_peas; cout << "Press return after entering a number.\n"; cout << "Enter the number of pods:\n"; cin >> number_of_pods; cout << "Enter the number of peas in a pod:\n"; cin >> peas_per_pod; total_peas = number_of_pods * peas_per_pod;

Demo Example 1 cout << "If you have "; cout << number_of_pods; cout << " pea pots\n"; cout << "and "; cout << peas_per_pod; cout << " pea in each pod, then \n"; cout << "you have "; cout << total_peas; cout << " peas in all the pods.\n"; return 0; }

C++ identifiers Identifiers appear in black in Visual C++. An identifier is a name for a variable, constant, function, etc. It consists of a letter followed by any sequence of letters, digits, and underscores. Examples of valid identifiers: First_name, age, y2000, y2k Examples of invalid identifiers: 2000y Identifiers cannot have special characters in them. For example: X=Y, J-20, ~Ricky,*Michael are invalid identifiers. Identifiers are case-sensitive. For example: Hello, hello, WHOAMI, WhoAmI, whoami are unique identifiers.

C++ comments Comments appear in green in Visual C++. Comments are explanatory notes; they are ignored by the compiler. There are two ways to include comments in a program: // A double slash marks the start of a //single line comment. /* A slash followed by an asterisk marks the start of a multiple line comment. It ends with an asterisk followed by a slash. */

C++ compiler directives Compiler directives appear in blue in Visual C++. The #include directive tells the compiler to include some already existing C++ code in your program. The included file is then linked with the program. There are two forms of #include statements: #include <iostream> //for pre-defined files #include "my_lib.h" //for user-defined files

Programming Style C++ is a free-format language, which means that: Extra blanks (spaces) or tabs before or after identifiers/operators are ignored. Blank lines are ignored by the compiler just like comments. Code can be indented in any way. There can be more than one statement on a single line. A single statement can continue over several lines.

Programming Style (cont. ) In order to improve the readability of your program, use the following conventions: Start the program with a header that tells what the program does. Use meaningful variable names. Document each variable declaration with a comment telling what the variable is used for. Place each executable statement on a single line. A segment of code is a sequence of executable statements that belong together. Use blank lines to separate different segments of code. Document each segment of code with a comment telling what the segment does.

What makes a bad program? Writing Code without detailed analysis and design Repeating trial and error without understanding the problem Debugging the program line by line, statement by statement Writing tricky and dirty programs Bad !! Bad !!