Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
© 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.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Data types and variables
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Basic Elements of C++ Chapter 2.
Objectives You should be able to describe: Data Types
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 2.
2440: 211 Interactive Web Programming Expressions & Operators.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Instructor - C. BoyleFall Semester
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Bill Tucker Austin Community College COSC 1315
Chapter 2: Basic Elements of C++
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Computer Programming BCT 1113
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2 - Introduction to C Programming
Variables A variable is a placeholder for a value. It is a named memory location where that value is stored. Use the name of a variable to access or update.
Basic Elements of C++.
Chapter 2 - Introduction to C Programming
Basic Elements of C++ Chapter 2.
Chapter 2 - Introduction to C Programming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Programming Funamental slides
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
Review of C++ Language Basics
C++ Programming Basics
Introduction to C Programming
Presentation transcript:

Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming in C © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

2

Explain the functionality of each item in C++ program structure 1 3 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

// my first program in C++ #include using namespace std; int main () { cout << "Hello World!“<<endl; return 0; } comment Preprocessor Directives Main function braces output Return statement Header file 4 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

a) Preprocessor Directives ▫ such as #define, are typically used to make source programs easy to change and easy to compile. Directives in the source file tell the preprocessor to perform specific actions. ▫ Example: #define LENGTH 10 var = LENGTH * 20; 5 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

b) Header Files ▫.h files or "headers" are libraries of code you may insert in your program by including them by reference in the top block. ▫ Example: #include #include 6 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

The point of header files is to create libraries of code that can be used over and over ▫ #include is the Standard Input and Output header file. Expl : printf, scanf. ▫ #include is the Input and Output Stream header file. Expl : cout, cin. 7 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

Types of Header Files (cont’) 1.iostream.h -Input/Output, interaction with the program. Expl : getline.iostream.h 2.math.h –For using mathematic formulamath.h 3.string.h- C++ has no built-in string handling. Use this library to copy strings, find string length, etc.string.h 4.stdio.h- Similar to stdlib but also has some file functions. printf, scanf(for C language)stdio.h 8 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

c) Main Function Is the place where the code execution begins. int main() { return 0; } 9 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR void main() { }

d) Return Statements Terminates the execution of a function and returns control to the calling function (or, in the case of the main function, transfers control back to the operating system). Execution resumes in the calling function at the point immediately following the call. 10 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

11

Explain identifier, variable and constant. 1 State the naming convention rules for identifier. 2 Declare variables and constants. 3 Initializes variables 4 Determine identifier scope: local, global. 5 Explain keywords 6 12 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

Identifier : A name associated with a variable and constant, usually limited to letters,digits and underscores. Variables are identifiers whose value may change during the course of execution of a program. Variable represent memory location where you can store value such as characters, numbers and pointers. Constants are identifiers whose value is kept permanently for program to use Remain unchanged throughout a program 13 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

1. Identifiers must begin with a letter or underscore. ▫ Only letters(A-Z,a-z), digits(0-9), or underscore( _ ) may follow the initial letter. ▫ Ex: sum_of_squares, box_22A, GetData 2. The blank space cannot be used. ▫ Ex: Get Data cannot be an identifier as blanks are not allowed in identifiers 14 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

3. Identifiers cannot be reserved words. Reserved words or keywords are identifiers reserved for system use. ▫ Ex: int cannot be an identifier as it is a reserved word in C++ 4. Identifiers beginning with an underscore have special meaning in some C++ systems, so it is best not to start your identifiers with an underscore. 15 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

5. Most C++ compilers will recognize the first 32 characters in an identifier. Also, C++ is a case sensitive language. C++ will distinguish between upper and lower case letters in identifiers. Therefore: grade and Grade are different identifier 6. Avoid excessively long identifiers. 16 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

7. You can develop your own conventions like below: ▫ A single word identifier will be written in lower case only. Examples: grade, number, sum. ▫ If an identifier is made up of several words, the first letter will be lower case. Subsequent words will begin with upper case. Some examples are: stringType, passingScore, largestNum. ▫ Identifiers used as constants are often fully capitalized. Examples: PI, MAXSTRLEN. 17 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

A C++ variable is declared by telling the program: ▫ the data type to be used ▫ the name of the variable ▫ Syntax : dataType variableName; ▫ Examples: int Marks; Data TypeVariable 18 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

To declare more than one variable of the same data type. For example: int a, b, c; 19 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

Constants are expressions with a fixed value. ▫ To declare constant (#define) : #define constName value For example: #define PI © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

▫ To declare constant (const) ▫ Syntax: const dataType constName = value; Example: const int PATH = 100; 21 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

It is done by appending an equal sign followed by the value to which the variable will be initialized: type identifier = initial_value ; For example, : int a = 0; 22 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

A variable can be either of global or local scope. A global variable is a variable declared in the main body of the source code, outside all functions, while a local variable is one declared within the body of a function or a block. 23 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

24 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR #include int main() { int a; // This is a variable int calArea(int b,int c); //This is a function declaration cout << "Please enter a value : "; cin >> a; return 0; } #include int main() { int a; // This is a variable int calArea(int b,int c); //This is a function declaration cout << "Please enter a value : "; cin >> a; return 0; } Global variable Local variable

Keywords are predefined reserved identifiers that have special meanings. They cannot be used as identifiers in your program. Example: ▫ void ▫ main ▫ float 25 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

26

Explain the basic data types: char, int, double, float, bool 1 27 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

1.int: is used to declare integers, whole numbers either positive or negative. The following statement shows how the variables of int type are declared. int var1; int b = 10; 28 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

2. long: This long keyword is used for declaring longer numbers. 3. float: This keyword float is used to declare floating point decimal numbers. A sample declaration would be: float var2; //Sample declaration for float or float var2 = 2.34; 29 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

4. char: This keyword is used to declare characters. The characters that can be used with this data type are ASCII characters. char a[9]=“Malaysia”; char a[]=“Malaysia”; char noPhone[11]=“ ”; 30 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

5. String: Must include header file string.h #include Int main() { …… ….. string mystring = "This is a string"; …… return 0; } 31 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

6. Boolean To declare a boolean variable, we use the keyword bool. bool bValue; When assigning values to boolean variables, we use the keywords true and false. bool bValue1 = true; 32 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

33

Identify the syntax use for input and output. 1 Write algorithm and programs that use the input and output statements © 2010/2011 | PN NORHASLIZA BT MUHAMAD NOR

Use cin >> Must have header file iostream.h #include void main() { int n1, n2, n3; cout << "Enter 3 numbers : "; cin >> n1 >> n2 >> n3; } 35 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

#include void main() { int n1, n2, n3; cout << "Enter 3 numbers : "; cout << “First Number : "; cin >> n1 ; cout << “Second Number : "; cin >> n2 ; cout << “Third Number : "; cin >> n3 ; } 36 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

Use cout << Must have header file iostream.h #include void main() { int n1, n2, n3; cout << "Enter 3 numbers : "; cin >> n1 >> n2 >> n3; } 37 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

#include void main() { int n1; cout << "Enter 1 numbers : "; cin >> n1 ; cout << “The number you enter : “<< n1; } 38 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

39

Explain Arithmetic, Assignment, Increment & Decrement, Relational and logical Operator 1 Explain operators precedence 2 Write expression using operator 3 Use expression in program 4 40 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

The five arithmetical operations supported by the C++ language are: +addition - subtraction *multiplication / division % modulus 41 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

a=5 value+= increase value = value + increase; a -= 5a = a - 5; a /= ba = a / b; price *= units + 1price = price * (units + 1); Refer to the symbol (=),equal. 1)Use to give the value to variable 2)or to modify the value of a variable by performing an operation on the value currently stored in that variable : 42 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

1)Operasi Unari Prefix 2)Operasi Unari Postix 43 a ++; a --; ++ a; -- a; Cth : int x = 180, y =200; y= x++; cout<<“x:”<<x<<endl<<“y:”<<y<<endl; Output: x : 181 y : 180 Cth : int x = 180, y =200; y= ++x; cout<<“x:”<<x<<endl<<“y:”<<y<<endl; Output: x : 181 y : 181 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

OperatorMeaning Example ==Equal to 4 = =4 <Less than 2 < 7 >Greater than 8 > 3 <=Less than or equal to 6 < = 7 >=Greater than or equal to 13 > = 5 !=Not equal to 6 != 2 44 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

Assignment and Relational operators = is not the same as the operator == assigns the value at its right to the variable at its left the equality operator that compares whether both expressions in the two sides of it are equal to each other 45 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

XYx or y || x And y && Not x True False TrueFalseTrueFalse True FalseTrue False True 46 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

The conditional operator evaluates an expression returning a value if that expression is true and a different one if the expression is evaluated as false. Its format is: condition ? result1 : result2 7>5 ? 3 : 2 Answer : 3 47 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

48 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR