CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Similar documents
Introduction to Programming

Chapter 1 Introduction to Computers and C++ Programming

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

C: How to Program. Week /Mar/05

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Chapter 2 - Introduction to C Programming

CS242 COMPUTER PROGRAMMING

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Chapter 15 - C++ As A "Better C"

Creating a C++ Program

6.096 Introduction to C++ January (IAP) 2009

Introduction to C# Applications

Lab # 02. Basic Elements of C++ _ Part1

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Fundamentals of Programming. Lecture 3: Introduction to C Programming

BITG 1233: Introduction to C++

Fundamentals of Programming Session 4

A First Program - Greeting.cpp

UEE1302 (1102) F10: Introduction to Computers and Programming

Computer Programming : C++

Chapter 1 & 2 Introduction to C Language

Programming. C++ Basics

Introduction to C++ Systems Programming

2 nd Week Lecture Notes

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Fundamental of Programming (C)

LECTURE 02 INTRODUCTION TO C++

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Introduction to Programming

IS 0020 Program Design and Software Tools

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

The C++ Language. Arizona State University 1

Chapter 2, Part I Introduction to C Programming

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

BASIC ELEMENTS OF A COMPUTER PROGRAM

Chapter 2 - Control Structures

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

IT 374 C# and Applications/ IT695 C# Data Structures

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

Chapter 2 - Control Structures

REVIEW. The C++ Programming Language. CS 151 Review #2

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Another Simple Program: Adding Two Integers

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Full file at C How to Program, 6/e Multiple Choice Test Bank

CSc Introduction to Computing

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Programming for Engineers Introduction to C

Chapter 2 - Control Structures

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Fundamentals of Programming CS-110. Lecture 2

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

CHAPTER 3 BASIC INSTRUCTION OF C++

Introduction to Programming EC-105. Lecture 2

Engineering Problem Solving with C++, Etter/Ingber

UNIT- 3 Introduction to C++

Chapter 1 Introduction to Computers and C++ Programming

Objectives. In this chapter, you will:

BTE2313. Chapter 2: Introduction to C++ Programming

Programming with C++ Language

Variables. Data Types.

download instant at Introduction to C++

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Basic Types, Variables, Literals, Constants

Computer Skills (2) for Science and Engineering Students

COMP 202 Java in one week

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Data types, variables, constants

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Chapter 2. Outline. Simple C++ Programs

Getting started with C++ (Part 2)

Your First C++ Program. September 1, 2010

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Chapter 2 Basic Elements of C++

UNIT-2 Introduction to C++

Manual. Subject Code: CS593. Computer Science and Engineering

Computer Programming C++ Classes and Objects 6 th Lecture

Chapter 2: Overview of C++

by Pearson Education, Inc. All Rights Reserved. 2

Exercise 1.1 Hello world

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Transcription:

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING Dr. Shady Yehia Elmashad

Outline 1. Introduction to C++ Programming 2. Comment 3. Variables and Constants 4. Basic C++ Data Types 5. Simple Program: Printing a Line of Text 6. Simple Program: Adding Two Integers 7. a Simple Program: Calculating the area of a Circle

1. Introduction to C++ Programming C++ language - Facilitates a structured and disciplined approach to computer program design Following are several examples - The examples illustrate many important features of C++ - Each example is analyzed one statement at a time.

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

2. Comment Message to everyone who reads source program and is used to document source code. Makes the program more readable and eye catching. Non executable statement in the C++. Always neglected by compiler. Can be written anywhere and any number of times. Use as many comments as possible in C++ program.

1. Single Line Comment 2. Comment starts with // symbol. Remaining line after // symbol is ignored by browser. End of Line is considered as End of the comment. 2. Multiple Line Comment (Block Comment) starts with /* symbol. ends with */ symbol. Types of comment

2. Comment Example /* this program calculate the sum of two numbers */ #include<iostream> // header file using namespace std; int main( ) الدالة الرئيسية // { int x, y, sum ; /* read the two numbers */ cin >> x >> y ; // calculate the sum sum = x + y ; // print the result cout << sum ; return 0; } // declaration part

3. Variables and Constants Variables Variables are memory location in computer's memory to store data. Each variable should be given a unique name called identifier, to indicate the memory location in addition to a data type. Variable names are just the symbolic representation of a memory location. Variable value can be changed during program execution

3. Variables and Constants Variables Declaration variable_type variable_name; Example: int a; - Declares a variable named a of type int int a, b, c; - Declares three variables, each of type int int a; float b;

3. Variables and Constants Constants Constant is the term that has a unique value and can't be changed during the program execution. Declaration: 1. #define constant_name constant_value Example: #define PI 3.14 2. const constant_type constant_name = constant_value ; Example: const float PI = 3.14;

3. Variables and Constants Variables and Constants Names Can be composed of letters (both uppercase and lowercase letters), digits and underscore '_' only. Must begin with a letter or underscore _. Don t contain space or special character: (#, *,?, -, @,!, $, %,&, space, ) Can t be one of the reserved words (they are used by the compiler so they are not available for re-definition or overloading.)

3. Variables and Constants Reserved Words Examples int float double char string short long signed for while if switch break default do else case return sizeof static continue goto true false const void private struct class cin cout new

3. Variables and Constants Reserved Words Examples Which of the following variable names are valid/not valid and why if not? Name Valid or not Name Valid or not area 10rate shoubra_faculty Shoubra faculty w234 W#d Ahmed 1233 A3 Cin A_3 Shoubra-faculty temp int

1 // Fig. 4.7: fig04_07.cpp 2 // A const object must be initialized 3 4 int main() 5 { 6 const int x; // Error: x must be initialized 7 Notice that const variables must be 8 x = 7; // Error: cannot modify a const 9 variable later. 10 return 0; 11} initialized because they cannot be modified Fig04_07.cpp: Error E2304 Fig04_07.cpp 6: Constant variable 'x' must be initialized in function main() Error E2024 Fig04_07.cpp 8: Cannot modify a const object in function main() *** 2 errors in Compile *** Outline 1. Initialize const variable 2. Attempt to modify variable Program Output 2000 Prentice Hall, Inc. All rights 14

4. Basic C++ Data Types Type Integer Real Character String Boolean Keyword short - int - long float - double - long double char string bool

4. Basic C++ Data Types Real: hold numbers that have fractional part with different levels of precision, depending on which of the three floating-point types is used. Example: float PI = 3.14; Character: hold a single character such as a, A and $. Example: char ch = a ; String: store sequences of characters, such as words or sentences. Example: string mystring = "This is a string"; Boolean: hold a Boolean value. It may be assigned an integer value 1 (true) or a value 0 (false). Example: bool status;

4. Basic C++ Data Types typedef Declarations You can rename an existing type using typedef. typedef type freshname; For example, this tells the compiler that number is another name for int: typedef int number; Therefore, the following declaration is perfectly legal and creates an integer variable called distance: number distance;

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

5. a Simple Program: Printing a Line of Text Escape Sequence Description \n Newline. Position the screen cursor to the beginning of the next line. \t Horizontal tab. Move the screen cursor to the next tab stop.

1 // Fig. 1.4: fig01_04.cpp 2 // Printing a line with multiple statements 3 #include <iostream> 4 5 int main() 6 { 7 cout << "Welcome "; 8 cout << "to C++!\n"; 9 10 return 0; // indicate that program ended successfully 11} Outline 1. Load <iostream> 2. main 2.1 Print "Welcome" 2.2 Print "to C++!" 2.3 newline 2.4 exit (return 0) Welcome to C++! Program Output Unless new line '\n' is specified, the text continues on the same line. 2000 Prentice Hall, Inc. All rights 20

1 // Fig. 1.5: fig01_05.cpp 2 // Printing multiple lines with a single statement 3 #include <iostream> 4 5 int main() 6 { 7 cout << "Welcome\nto\n\nC++!\n"; 8 9 return 0; // indicate that program ended successfully 10} Outline 1. Load <iostream> 2. main 2.1 Print "Welcome" 2.2 newline 2.3 Print "to" 2.4 newline 2.5 newline Welcome to C++! Multiple lines can be printed with one statement. 2.6 Print "C++!" 2.7 newline 2.8 exit (return 0) Program Output 2000 Prentice Hall, Inc. All rights 21

6. a Simple Program: Adding Two Integers (stream extraction operator) When used with std::cin, waits for the user to input a value and stores the value in the variable to the right of the operator The user types a value, then presses the Enter (Return) key to send the data to the computer Example: int myvariable; std::cin >> myvariable; - Waits for user input, then stores input in myvariable = (assignment operator) Assigns value to a variable Binary operator (has two operands) Example: sum = variable1 + variable2;

1 // Fig. 1.6: fig01_06.cpp 2 // Addition program 3 #include <iostream> 4 5 int main() 6 { 7 int integer1, integer2, sum; // declaration 8 9 cout << "Enter first integer\n"; // prompt 10 cin >> integer1; input. // read an 11 integer cout << "Enter second integer\n"; // prompt 12 cin >> integer2; // read an 13 integer sum = integer1 + integer2; // assignment 14 cout << of "Sum sum is " << sum << std::endl; // print 15 sum 16 return 0; // indicate that program ended successfully 17} Enter first integer 45 Enter second integer 72 Sum is 117 Outline Load <iostream>.1 2. main 2.1 Initialize variables integer1, Notice how std::cin is used integer2, to get user and sum 2.2 Print "Enter first integer" 2.2.1 Get input 2.3 Print "Enter second integer" std::endl flushes the buffer and 2.3.1 Get input prints a newline. 2.4 Add variables and put result into sum Variables can be output using std::cout << variablename. 2.5 Print "Sum is" 2.5.1 Output sum 2.6 exit (return 0) Program Output 2000 Prentice Hall, Inc. All rights 23

7. a Simple Program: Calculating the area of a Circle # include <iostream> # define PI 3.14 using namespace std; int main ( ) { /* This program asks the user to enter a radius then calculate the area */ float radius, Area; cout<< Please enter a radius: ; cin>> radius; Area = PI * radius * radius ; cout<< The area of the circle is << Area ; return 0; } Write a program to calculate the volume of a sphere.