Introduction to C++ Programming

Slides:



Advertisements
Similar presentations
Chapter 2 Part B CISS 241. Take a few moments and work with another student to develop an algorithm for a program that will add two whole numbers (integers)
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 1 – Introduction to Computers and C++ Programming Outline 1.6 Machine Languages, Assembly Languages,
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline History of C and C++ C++ Standard Library Object Technology Basics.
 2008 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
Chapter 2: Introduction to C++.
Introduction to C Programming
Introduction to C++ Programming
Basic Elements of C++ Chapter 2.
Chapter 01: Introduction to Computer Programming
Introduction to C++ Programming
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
 2008 Pearson Education, Inc. All rights reserved. 1 CISC 1600 – Computer Science I Fall 2010 Introduction to C++ Programming Chapters 1 and 2 (Deitel.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
Beginning C++ Through Game Programming, Second Edition
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
Announcements Starting next week class 6-8 on Thursday Homework 1 on the web  Due January 29 – next class meeting  Homework policy No late assignments.
 2006 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
 2008 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
Chapter 02 (Part II) Introduction to C++ Programming.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
© by Pearson Education, Inc. All Rights Reserved.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Introduction to C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Chapter 2 - Introduction to C Programming
Basic Elements of C++.
Chapter 2 - Introduction to C Programming
Basic Elements of C++ Chapter 2.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
2.1 Parts of a C++ Program.
Introduction to C++ Programming
1.13 The Key Software Trend: Object Technology
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Introduction to C++ Programming

Lesson Plan Knowledge of simple C++ program Write simple input/output statements Knowledge of data types, variable declaration, arithmetic operators

Simple C++ Program by example /* This is HelloWorld program in C++ */ #include <iostream> using namespace std; int main() { cout << "Hello World!\n This is my first C++ program\n"; return 0; }

Simple C++ Program by example /* This is HelloWorld program in C++ */ #include <iostream> using namespace std; int main() { cout << "Hello World!\n This is my first C++ program\n"; return 0; } This is C++ comment

C++ comments Comments: Must-have, to explain what a programmer is doing while coding Single line comments: // this is my comment Multiple line comments: /* This is my comment This also is my comment */

Simple C++ Program by example /* This is HelloWorld program in C++ */ #include <iostream> using namespace std; int main() { cout << "Hello World!\n This is my first C++ program\n"; return 0; } This is C++ Preprocessor directive 1. Comment out #include <iostream> from your hello.cpp 2. Save and Compile it 3. Describe the error

Simple C++ by example Preprocessor directive: This is processed by the preprocessor before the program is compiled. Notifies the preprocessor to include in the program the content of corresponding header file from C++ header library (i.e *.h file)

Simple C++ Program by example /* This is HelloWorld program in C++ */ #include <iostream> using namespace std; int main() { cout << "Hello World!\n This is my first C++ program\n"; return 0; } Using declarations 1. Comment out using std::cout from your hello.cpp 2. Save and Compile it 3. Describe the error 4. Change cout to be std:: cout, save and compile

Simple C++ Program by example Using declarations eliminates the needs to repeat std::<prefix> So we use: using std::cout; // for program uses cout using std::cin; // for program uses cin using std::endl; // for program uses endl; …. Or we can use: using namespace std;

Simple C++ Program by example /* This is HelloWorld program in C++ */ #include <iostream> using namespace std; int main() { cout << "Hello World!\n This is my first C++ program\n"; return 0; } Main function in C++

Simple C++ Program by example Syntax: int main() { } Main function is a part of every C++ Program Exactly one function in every program must be named main int: means return an integer (in this context only) 1. Rename main to Main in your hello.cpp 2. Save and Compile it 3. Describe the error

Simple C++ Program by example /* This is HelloWorld program in C++ */ #include <iostream> using namespace std; int main() { cout << "Hello World!\n This is my first C++ program\n"; return 0; } C++ statement

Simple C++ Program by example Every C++ statement must end with ; Preprocessor directives do not end with ; << operator: stream insertion operator Insert the value on the right to the output stream \n (escape sequence): new line

Simple C++ Program by example /* This is HelloWorld program in C++ */ #include <iostream> using namespace std; int main() { cout << "Hello World!\n This is my first C++ program\n"; return 0; } Return statement to exit a function

Steps to develop a C++ Program Editor Preprocessor Compiler Linker

“On-the-fly” questions /* This is a valid C++ comment /* True False

“On-the-fly” questions /* This is a valid C++ comment /* True False

“On-the-fly” question Every C++ statement ends with a(n): } { ; return

“On-the-fly” question Every C++ statement ends with a(n): } { ; return

“On-the-fly” question Every C++ program begins execution at the function Main MAIN static main main

“On-the-fly” question Every C++ program begins execution at the function Main MAIN static main main

Variable declaration Syntax: <data type> <variable_name>; If more than one variable has the same data type: <data type> <name1>, <name2>..;

Fundamental numerical data types

Variable names It must be a legal identifier. It must not be a keyword, a boolean literal (true or false), or the reserved word. It must be unique within its scope.

Identifier A valid identifier is a sequence of one or more letters, digits or underscore characters (_). Identifiers may only begin with a letter, or _. Keyword: http://www.cplusplus.com/doc/tutorial/variables/ C++ is case sensitive language

Example int firstNumber; int secondNumber; int sum; int firstNumber, secondNumber, sum; All variables in C++ MUST be declared before being used

Reading a value from keyboard Example: cin >> firstNumber; A cin statement uses the input stream object cin (of namespace std) and the stream extraction operator, >>, to obtain a value from the keyboard. Usually you will need a prompt it directs the user to take a specific action before putting cin statement

Assignment statement <variable name> = <expression>; Example: sum = firstNumber + secondNumber; Or sum = 6+7;

Arithmetic Expression Any expression involving numerical value is called arithmetic expression For example: x+15 y /16

Arithmetic operators Addition: + Subtraction: - Multiplication: * x+y Subtraction: - x-y Multiplication: * x*y Division: / x/y Modulo: % x%y

Arithmetic Most programs perform arithmetic calculations. The asterisk (*) indicates multiplication. The percent sign (%) is the modulus operator: C++ provides the modulus operator, %, that yields the remainder after integer division. The modulus operator can be used only with integer operands. Integer division (i.e., where both the numerator and the denominator are integers) yields an integer quotient. Any fractional part in integer division is discarded (i.e., truncated)—no rounding occurs.

Arithmetic C++ applies the operators in arithmetic expressions in a precise sequence determined by the following rules of operator precedence, which are generally the same as those followed in algebra.

Arithmetic There is no arithmetic operator for exponentiation in C++, so x2 is represented as x * x. As in algebra, it’s acceptable to place unnecessary parentheses in an expression to make the expression clearer. These are called redundant parentheses.

Constant and variables Value it contains doesn’t change Two ways to declare a constant in C++: #define <constant name> <value>; // using preprocessor directives const <datatype> <constant name>; // inside the program Variables: Value it contains may vary int sum; sum =0; sum = firstNumber + secondNumber;

Operator << Using multiple stream insertion operators (<<) in a single statement is referred to as concatenating, chaining or cascading stream insertion operations. Calculations can also be performed in output statements.

Memory Concepts Variable names such as firstNumber, secondNumber and sum actually correspond to locations in the computer’s memory. Every variable has a name, a type, a size and a value. When a value is placed in a memory location, the value overwrites the previous value in that location; thus, placing a new value into a memory location is said to be destructive.

©1992-2010 by Pearson Education, Inc. All Rights Reserved.