C++ Programming, Namiq Sultan1 Chapter 2 Introduction to C++ Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

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

C++ Basics Variables, Identifiers, Assignments, Input/Output.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
© 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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Starting Out with C++, 3 rd Edition 1 Chapter 2. Introduction to C++
Basic Elements of C++ Chapter 2.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
CS 192 Lecture 3 Winter 2003 December 5, 2003 Dr. Shafay Shamail.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
Input & Output: Console
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
C Tokens Identifiers Keywords Constants Operators Special symbols.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
1 C++ Syntax and Semantics, and the Program Development Process.
Chapter 2: Java Fundamentals
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
C++ Programming: Basic Elements of C++.
Chapter 2: Introduction to C++. Resource: Starting Out with C++, Third Edition, Tony Gaddis The Parts of a C++ Program C++ programs have parts and components.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Starting Out with C++: From Control Structures through Objects 7 th edition By Tony Gaddis Source Code Chapter 2.
Lecture 3: The parts of a C++ program (Cont’d) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
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)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 Chapter 2 Introduction to C++. 2 Topics 2.1 Parts of a C++ Program 2.2 The cout Object 2.3 The #include Directive 2.4 Variables and Constants 2.5 Identifiers.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
1 CS102 Introduction to Computer Programming Week 2 Chapter 2, Introduction to C++
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
C++ Lesson 1.
Asst.Prof.Dr. Tayfun ÖZGÜR
Variables, Identifiers, Assignments, Input/Output
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.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Data types Data types Basic types
Chapter 2. Introduction to C++
Basic Elements of C++.
Starting Out with C++: From Control Structures through Objects
Chapter 2: Introduction to C++
Reserved Words.
Basic Elements of C++ Chapter 2.
Chapter 2: Introduction to C++
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
C++ Basics.
2.1 Parts of a C++ Program.
Basics of ‘C’.
Variables, Identifiers, Assignments, Input/Output
Chapter 2: Introduction to C++.
Presentation transcript:

C++ Programming, Namiq Sultan1 Chapter 2 Introduction to C++ Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference: Starting Out with C++, Tony Gaddis, 2 nd Ed.

C++ Programming, Namiq Sultan2 2.1 The Parts of a C++ Program C++ programs have parts and components that serve specific purposes.

C++ Programming, Namiq Sultan3 Program 2-1 //A simple C++ program #include using namespace std; int main () { cout<< “Programming is great fun!”; return 0; } Program Output: Programming is great fun!

C++ Programming, Namiq Sultan4 Table 2-1 Special Characters

C++ Programming, Namiq Sultan5 2.2The cout Object Use the cout object to display information on the computer’s screen. The cout object is referred to as the standard output object. l Its job is to output information using the standard output device

C++ Programming, Namiq Sultan6 Program 2-2 // A simple C++ program #include using namespace std; int main () { cout << “Programming is “ << “great fun!”; return 0; } Output: Programming is great fun!

C++ Programming, Namiq Sultan7 Program 2-3 // A simple C++ program #include using namespace std; int main () { cout<< “Programming is “; cout << “ great fun!”; return 0; } Output: Programming is great fun!

C++ Programming, Namiq Sultan8 Program 2-4 // An unruly printing program #include using namespace std; int main() { cout << "The following items were top sellers"; cout << "during the month of June:"; cout << "Computer games"; cout << "Coffee"; cout << "Aspirin"; return 0; } Program Output The following items were top sellersduring the month of June:Computer gamesCoffeeAspirin

C++ Programming, Namiq Sultan9 New lines cout does not produce a newline at the end of a statement To produce a newline, use either the stream manipulator endl or the escape sequence \n

C++ Programming, Namiq Sultan10 Program 2-5 // A well-adjusted printing program #include using namespace std; int main() { cout << "The following items were top sellers" << endl; cout << "during the month of June:" << endl; cout << "Computer games" << endl; cout << "Coffee" << endl; cout << "Aspirin" << endl; return 0; }

C++ Programming, Namiq Sultan11 Program Output The following items were top sellers during the month of June: Computer games Coffee Aspirin

C++ Programming, Namiq Sultan12 Program 2-6 // Another well-adjusted printing program #include using namespace std; int main() { cout << "The following items were top sellers" << endl; cout << "during the month of June:" << endl; cout << "Computer games" << endl << "Coffee"; cout << endl << "Aspirin" << endl; return 0; }

C++ Programming, Namiq Sultan13 Program Output The following items were top sellers during the month of June: Computer games Coffee Aspirin

C++ Programming, Namiq Sultan14 Program 2-7 // Yet another well-adjusted printing program #include using namespace std; int main() { cout << "The following items were top sellers\n"; cout << "during the month of June:\n"; cout << "Computer games\nCoffee"; cout << "\nAspirin\n"; return 0; }

C++ Programming, Namiq Sultan15 Program Output The following items were top sellers during the month of June: Computer games Coffee Aspirin

C++ Programming, Namiq Sultan16 Table 2-2

C++ Programming, Namiq Sultan The #include Directive The #include directive causes the contents of another file to be inserted into the program Preprocessor directives are not C++ statements and do not require semicolons at the end

C++ Programming, Namiq Sultan Variables and Constants Variables represent storage locations in the computer’s memory. Constants are data items whose values do not change while the program is running. Every variable must have a declaration.

C++ Programming, Namiq Sultan19 Program 2-8 #include using namespace std; int main() { int value; value = 5; cout << “The value is “ << value << endl; return 0; } Program Output: The value is 5

C++ Programming, Namiq Sultan20 Assignment statements: Value = 5; //This line is an assignment statement. The assignment statement evaluates the expression on the right of the equal sign then stores it into the variable named on the left of the equal sign The data type of the variable was in integer, so the data type of the expression on the right should evaluate to an integer as well.

C++ Programming, Namiq Sultan21 Constants A variable is called a “variable” because its value may be changed. A constant, on the other hand, is a data item whose value does not change during the program’s execution.

C++ Programming, Namiq Sultan Identifiers Must not be a keyword The first character must be a letter or an underscore The remaining may be letters, digits, or underscores Upper and lower case letters are distinct

C++ Programming, Namiq Sultan23 Table 2-3 The C++ Keywords Asm auto bool break case catch char class const const_cast continue default delete do double dynamic_cast else enum explicit export extern false float for friend goto if inline int long mutable namespace new operator private protected public register reinterpret_cast return short signed sizeof static static_cast struct switch template this throw true try typedef typeid Type name union unsigned using virtual void volatile wchar_t while

C++ Programming, Namiq Sultan24 Table 2-4 Some Variable Names Variable NameLegal or Illegal? dayOfWeekLegal 3dGraphIllegal. Cannot begin with a digit. _employee_numLegal june1997Legal Mixture#3Illegal. Cannot use # symbol.

C++ Programming, Namiq Sultan25 2.6Integer Data Types There are many different types of data. Variables are classified according to their data type, which determines the kind of information that may be stored in them. Integer variables only hold whole numbers.

C++ Programming, Namiq Sultan26 Program 2-12 // This program shows three variables declared on the same line. #include using namespace std; int main() { int floors, rooms, suites; floors = 15; rooms = 300; suites = 30; cout << "The Grande Hotel has " << floors << " floors\n"; cout << "with " << rooms << " rooms and " << suites; cout << " suites.\n"; return 0; }

C++ Programming, Namiq Sultan27 Program Output The Grande Hotel has 15 floors with 300 rooms and 30 suites.

C++ Programming, Namiq Sultan The char Data Type Usually 1 byte long Internally stored as an integer ASCII character set shows integer representation for each character ‘A’ == 65, ‘B’ == 66, ‘C’ == 67, etc. Single quotes denote a character, double quotes denote a string

C++ Programming, Namiq Sultan29 Program 2-14 // This program uses character constants #include using namespace std; int main() { char letter; letter = 'A'; cout << letter << endl; letter = 'B'; cout << letter << endl; return 0; }

C++ Programming, Namiq Sultan30 Program Output ABAB

C++ Programming, Namiq Sultan31 Strings Strings are consecutive sequences of characters and can occupy several bytes of memory. Strings always have a null terminator at the end. This marks the end of the string. Escape sequences are always stored internally as a single character.

C++ Programming, Namiq Sultan32 Let’s look at an example of how a string is stored in memory. "Sebastian" would be stored. ‘A’ is stored as “A” is stored as Sebats\0ian 65 0

C++ Programming, Namiq Sultan33 Review key points regarding characters, and strings: Printable characters are internally represented by numeric codes. Most computers use ASCII codes for this purpose. Characters occupy a single byte of memory. Strings are consecutive sequences of characters and can occupy several bytes of memory. Strings always have a null terminator at the end. This marks the end of the string. Character constants are always enclosed in single quotation marks. String constants are always enclosed in double quotation marks. Escape sequences are always stored internally as a single character.

C++ Programming, Namiq Sultan Floating Point Data Types Floating point data types are used to declare variables that can hold real numbers

C++ Programming, Namiq Sultan35 // This program uses floating point data types #include using namespace std; int main() { float distance; float mass; distance = ; mass = 1.989; cout << "The Distance is " << distance << " kilometers \n"; cout << "The mass is " << mass << " kilograms.\n"; return 0; }

C++ Programming, Namiq Sultan36 Program Output The Sun is kilometers The mass is kilograms.

C++ Programming, Namiq Sultan The bool Data Type Boolean variables are set to either true or false

C++ Programming, Namiq Sultan38 Program 2-17 #include using namespace std; int main () { bool boolValue; boolValue = true; cout << boolValue << endl; boolValue = false; cout << boolValue << endl; return 0; }

C++ Programming, Namiq Sultan39 Program Output 1 0 Internally, true is represented as the number 1 and false is represented by the number 0.

C++ Programming, Namiq Sultan Variable Assignment and Initialization An assignment operation assigns, or copies, a value into a variable. When a value is assigned to a variable as part of the variable’s declaration, it is called an initialization.

C++ Programming, Namiq Sultan41 Program 2-19 #include using namespace std; int main () { int month = 2, days = 28; cout << “Month “ << month << “ has “ << days << “ days.\n”; return 0; } Program output: Month 2 has 28 days.

C++ Programming, Namiq Sultan Arithmetic Operators There are many operators for manipulating numeric values and performing arithmetic operations. Generally, there are 3 types of operators: unary, binary, and ternary. l Unary operators operate on one operand l Binary operators require two operands l Ternary operators need three operands ( ?: in ch 4)

C++ Programming, Namiq Sultan43 Table 2-8

C++ Programming, Namiq Sultan44 Program 2-21 // This program calculates hourly wages. The variables are used as follows: // regWages: holds the calculated regular wages. // basePay: holds the base pay rate. // regHours: holds the number of hours worked less overtime. //otWages: holds the calculated overtime wages. // otPay: holds the payrate for overtime hours. // otHours: holds the number of overtime hours worked. //totalWages: holds the total wages. #include using namespace std; int main() { float regWages, basePay = 18.25, regHours = 40.0; float otWages, otPay = 27.78, otHours = 10; float totalWages;

C++ Programming, Namiq Sultan45 Program 2-21 regWages = basePay * regHours; otWages = otPay * otHours; totalWages = regWages + otWages; cout << "Wages for this week are $" << totalWages << endl; return 0; }

C++ Programming, Namiq Sultan Comments Comments are notes of explanation that document lines or sections of a program. Comments are part of a program, but the compiler ignores them. They are intended for people who may be reading the source code. Commenting the C++ Way // Commenting the C Way /* */

C++ Programming, Namiq Sultan47 Program 2-22 // PROGRAM: PAYROLL.CPP // Written by Herbert Dorfmann // This program calculates company payroll #include using namespace std; int main(void) { float payRate;// holds the hourly pay rate float hours;// holds the hours worked int empNum;// holds the employee number (The remainder of this program is left out.)

C++ Programming, Namiq Sultan48 Program 2-24 /* PROGRAM: PAYROLL.CPP Written by Herbert Dorfmann This program calculates company payroll */ #include using namespace std; int main(void) { float payRate;/* payRate holds hourly pay rate */ float hours;/* hours holds hours worked */ int empNum;/* empNum holds employee number */ (The remainder of this program is left out.)

C++ Programming, Namiq Sultan Programming Style Program style refers to the way a programmer uses identifiers, spaces, tabs, blank lines, and punctuation characters to visually arrange a program’s source code. l Generally, C++ ignores white space. l Indent inside a set of braces. l Include a blank line after variable declarations.

C++ Programming, Namiq Sultan50 Program 2-26 #include using namespace std; int main( ){float shares=220.0; float avgPrice=14.67; cout <<"There were "<<shares<<" shares sold at $"<<avgPrice<<" per share.\n";return 0;} Program Output There were 220 shares sold at $14.67 per share.

C++ Programming, Namiq Sultan51 Program 2-27 // This example is much more readable than Program #include using namespace std; int main(void) { float shares = 220.0; float avgPrice = 14.67; cout << "There were " << shares << " shares sold at $"; cout << avgPrice << " per share.\n"; return 0; } Program Output There were shares sold at $14.67 per share.