Overview of Previous Lesson(s) Over View  I nteractive development environment is a software application that provides comprehensive facilities to computer.

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

True or false A variable of type char can hold the value 301. ( F )
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
Basic Elements of C++ Chapter 2.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Objectives You should be able to describe: Data Types
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Chapter 2: Using Data.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Basic Elements of C++ Chapter 1.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
Slide 1. Slide 2 Chapter 1 C++ Basics Slide 3 Learning Objectives  Introduction to C++  Origins, Object-Oriented Programming, Terms  Variables, Expressions,
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming with Microsoft Visual Basic th Edition
Recap……Last Time [Variables, Data Types and Constants]
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Bill Tucker Austin Community College COSC 1315
Eine By: Avinash Reddy 09/29/2016.
Variables, Identifiers, Assignments, Input/Output
Chapter 1.2 Introduction to C++ Programming
LESSON 06.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Basic Elements of C++ Chapter 1.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Chap. 2. Types, Operators, and Expressions
Programming Fundamentals
Computing Fundamentals
Tokens in C Keywords Identifiers Constants
Basic Elements of C++.
Multiple variables can be created in one declaration
Introduction to C++.
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
Conversions of the type of the value of an expression
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Variables, Identifiers, Assignments, Input/Output
Variables Title slide variables.
Chapter 7 Conditional Statements
Lectures on Numerical Methods
elementary programming
Chapter 2: Introduction to C++.
C++ Programming Lecture 3 C++ Basics – Part I
Primitive Types and Expressions
C++ Programming Basics
Presentation transcript:

Overview of Previous Lesson(s)

Over View  I nteractive development environment is a software application that provides comprehensive facilities to computer programmers for software development.  Microsoft Visual Studio is an integrated development environment (IDE) from Microsoft Corporation.  Developed on.Net framework 3

Over View..  C++  KeywordsReserved wordsint, main, class, define  IdentifiersProgrammers defined variables  Variables A named storage location in the computer’s memory for holding a piece of data.  Data TypesWhen computer programs store data in variables, each variable must be assigned a specific data type. Some common data types include integers, floating point numbers, characters, strings, and arrays. 4

Over View...  Cout >> and Cin <<  A namespace is a part of the program in which certain names are recognized; outside of the namespace they’re unknown.  Escape Sequences/n, /t, //, /’, /” …  Library Functions  sqrt()  rand() 5

Over View...  Looping structures  For loop  While  Do While  Decision structures  If  If / else  Switch 6

7

Contents  Operator Precedence  Type Conversion and Casting  Auto Keyword  Discovering Types  Bitwise Operators  L Values and R Values  Storage Duration  Variable Scopes  Static Variables  Namespaces 8

Operator Precedence  Operator precedence orders the operators in a priority sequence.  In any expression, operators with the highest precedence are always executed first, followed by operators with the next highest precedence, and so on, down to those with the lowest precedence of all. 9

Operator Precedence..  In an expression with more than one operator, evaluate in this order: - (unary negation), in order, left to right * / %, in order, left to right + -, in order, left to right Example expression * 2 – 2 evaluate first evaluate second evaluate third

Operator Precedence... 11

Type Conversion and Casting  Operations in C++ can be carried out only between the values of the same data type.  So what if they are not of the same type ???  Implicit type conversion:  When an expression involving variables of different types then for each operation to be performed, the compiler has to arrange to convert the type of one of the operands to match that of the other. 12

Type Conversion and Casting..  Example  If you want to add a double value to a value of an integer type, the integer value is first converted to double, after which the addition is carried out.  The variable that contains the value to be converted is, itself, not changed. doubleval_double; Int val_int; cout << “Type Casting” << val_double – val_int ; 13

Type Conversion and Casting...  Any pair of operands of different types, the compiler decides which operand to convert to the other considering types to be in the following rank from high to low: 14

Type Conversion in Assignment  Example Int val_int; floatval_float = 3.5f; val_int = val_float; Result:val_int value is3 15 Single precision floating point

Explicit Type Conversion  With mixed expressions involving the basic types, C++ compiler automatically arranges casting where necessary, but it can also be forced for a conversion from one type to another by using an explicit type conversion.  Also referred as a cast.  To cast the value of an expression to a given type, you write the cast in the form: static_cast ( expression ) 16

Explicit Type Conversion..  The effect of the static_cast operation is to convert the value that results from evaluating expression to the type that you specify between the angled brackets.  Example double value1 = 10.5; double value2 = 15.5; int whole_number = static_cast (value1) + static_cast (value2); 17

Auto Keyword  Auto keyword is used as the type of a variable in a definition statement and have its type deduced from the initial value.  Examples: auto n = 16; // Type is int auto pi = ; // Type is double auto x = 3.5f; // Type is float auto found = false; // Type is bool  In each case, the type assigned to the defined variable is the same as that of the literal used as the initializer.  When auto is used in this way, you must supply an initial value must be assigned for the variable. 18

Auto Keyword..  Variables defined using the auto keyword can also be specified as constants: const auto e = L; // Type is const long double  Functional notation: const auto dozen(12); // Type is const int  The initial value for a defined variable using the auto keyword can also be an expression: auto factor(n*pi*pi); // Type is double  In this case, the definitions for the variables n and pi that are used in the initializing expression must precede this statement. 19

typeid Operator  The typeid operator enables to discover the type of an expression.  Syntaxtypeid(expression)  it results in an object of type type_info that encapsulates the type of the expression. #include cout < < "The type of x*y is " < < typeid(x*y).name() < < endl;  Output: The type of x*y is double 20

Bitwise Operators  The bitwise operators are useful in programming hardware devices, where the status of a device is often represented as a series of individual flags  The bitwise operators treat their operands as a series of individual bits rather than a numerical value.  It works only with the following data types:  short, int, long, long long, signed char, and char.  Unsigned variants of these can also be used. 21

Bitwise Operators..  There are six bitwise operators: 22

Bitwise AND &  If both corresponding bits are 1, the result is a 1 bit, and if either or both bits are 0 the result is a 0 bit.  The effect of a particular binary operator is often shown using what is called a truth table.  The truth table for & is as follows: 23

Bitwise OR |  The bitwise OR, |, sometimes called the inclusive OR, combines corresponding bits such that the result is a 1 if either operand bit is a 1, and 0 if both operand bits are 0.  The truth table for the bitwise OR is: 24

Bitwise Exclusive OR  The exclusive OR, ^, is so called because it operates similarly to the inclusive OR but produces 0 when both operand bits are 1.  The truth table for EOR is as follows: 25

Bitwise NOT  The bitwise NOT, ~, takes a single operand, for which it inverts the bits: 1 becomes 0, and 0 becomes 1.  For example if following statement is executed result = ~letter1;  if letter1 is , the variable result will have the value , which is 0xBE, or 190 as a decimal value. 26

L Values & R Values  An lvalue refers to an address in memory in which something is stored on an ongoing basis.  An rvalue is the result of an expression that is stored transiently.  Every expression in C++ results in either an lvalue or an rvalue.  An lvalue is so called because any expression that results in an l value can appear on the left of the equals sign in an assignment statement. If the result of an expression is not an lvalue, it is an rvalue. 27

L Values & R Values.. int a(0), b(1), c(2); a = b + c; b = ++a; c = a++;  The result of evaluating the expression b+c is stored temporarily in a memory location and the value is copied from this location to a. Once execution of the statement is complete, the memory location holding the result of evaluating b+c is discarded. Thus, the result of evaluating the expression b+c is an rvalue.  The expression ++a is an lvalue.  The expression a++ is an rvalue because it stores the value of a temporarily as the result of the expression and then increments a. 28

Storage Duration  All variables have a finite lifetime when your program executes.  They come into existence from the point at which you declare them and then, at some point, they disappear — at the latest, when your program terminates.  How long a particular variable lasts is determined by a property called its storage duration.  There are three different kinds of storage duration that a variable can have:  Automatic storage duration  Static storage duration  Dynamic storage duration  Which of these a variable will have depends on how you create it. 29

Variable Scopes  The scope of a variable is simply that part of your program over which the variable name is valid.  Within a variable ’ s scope, it can be legally referred, either to set its value or to use it in an expression.  Outside of the scope of a variable, it cannot be referred to its name — any attempt to do so will cause a compiler error. 30

Global Variables  Variables that are declared outside of all blocks and classes are called globals and have global scope.  They are accessible throughout all the functions in the file, following the point at which they are declared. If you declare them at the very top of your program, they will be accessible from anywhere in the file.  Global variables have storage class static, which means they exist for the life of the program.  Memory space is set aside for them when the program begins, and continues to exist until the program ends. 31

Static Variables  A static local variable has the visibility of an automatic local variable (that is, inside the function containing it).  Its lifetime is the same as that of a global variable, except that it doesn’t come into existence until the first call to the function containing it. static int count;  It remains in existence for the life of the program.  Static local variables are used when it’s necessary for a function to remember a value when it is not being executed; that is, between calls to the function. 32

Scope 33

Name Spaces  Namespaces provide a way to separate the names used in one part of a program from those used in another.  This thing seems to invaluable with large projects involving several teams of programmers working on different parts of the program.  Each team can have its own namespace name, and worries about two teams accidentally using the same name for different functions disappear. 34

Name Spaces using namespace std;  The effect of this is to import all the names from the std namespace into the source file so you can refer to anything that is defined in this namespace without qualifying the name in your program.  Programmer can use name cout instead of std::cout endl instead of std::endl.  This sounds like a big advantage, but the downside of this blanket using directive is that it effectively negates the primary reason for using a namespace, that is, preventing accidental name clashes. 35

Name Spaces  So a better solution is is to introduce just the names that you use in your code with using declarations. i.e using std::cout; // Allows cout usage without qualification using std::endl; // Allows endl usage without qualification  Each using declaration introduces a single name from the specified namespace and allows it to be used unqualified within the program code that follows. 36

Declaring a Namespace  namespace keyword is used to declare a namespace namespace myStuff { // Code that I want to have in the namespace myStuff... }  This defines a namespace with the name myStuff.  All name declarations in the code between the braces will be defined within the myStuff namespace.  To access any such name from a point Namespaces outside this namespace, the name must be qualifi ed by the namespace name, myStuff, or have a using declaration that identifies that the name is from the myStuff namespace. 37

Namespace Ex // Declaring a namespace #include namespace myStuff { int value = 0; } int main() { std::cout < < "enter an integer: "; std::cin > > myStuff::value; std::cout < < "\nYou entered " < < myStuff::value < < std::endl; return 0; } 38

Thank You 39