Subject:Object oriented programming

Slides:



Advertisements
Similar presentations
Dale/Weems/Headington
Advertisements

1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
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++.
Basic Elements of C++ Chapter 2.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
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.
IXA 1234: C++ PROGRAMMING CHAPTER 2: PROGRAM STRUCTURE.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Chapter 2 Overview of C++. A Sample Program // This is my first program. It calculates and outputs // how many fingers I have. #include using namespace.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
1 C++ Syntax and Semantics, and the Program Development Process.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
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++
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
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.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Programming in C++
C++ for Engineers and Scientists Second Edition
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Chapter 2 C++ Syntax and Semantics, and the Program Development Process Topics – Programs Composed of Several Functions – Syntax Templates – Legal C++
1 A Simple “Hello World” Example #include // input-output library using namespace std; int main() // function main { cout
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
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
C++ First Steps.
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Topic Pre-processor cout To output a message.
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
BASIC ELEMENTS OF A COMPUTER PROGRAM
Documentation Need to have documentation in all programs
Computing Fundamentals
Chapter 2 Topics Programs Composed of Several Functions
Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Basic Elements of C++
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
Chapter 2: Introduction to C++
Chapter 2 – Getting Started
2.1 Parts of a C++ Program.
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Chapter 1 c++ structure C++ Input / Output
Variables and Constants
Presentation transcript:

Subject:Object oriented programming Instructor: Asim shahzad Qualification: MS Computer engineering from UET Taxila MS Telecom engineering from institute of communication technologies. PhD Scholar

Lecture 2 Overview of C++

A Sample Program #include <iostream> using namespace std; int main () { int digits; digits = 5*2; cout << "I have " << digits << " fingers" << endl; return 0; }

The main function must have a function named main in any C or C++ program start of the main function is the marker for the start of the program execution

What is in a heading (header)? type of returned value name of function says no parameters int main( ) 5

The main function header is int main () // NOT "void main" empty () in header means no parameters braces {} around body must balance semicolons mark ends of statements return 0; at the end of the function "satisfies" the int return value in the header

Code and Data almost every statement in the program is concerned with manipulating data inputting it outputting it calculating with it making decisions based on its values storing it temporarily or permanently

Data Properties has a name - either itself or an identifier has a type - integer, character, float, etc. has space in RAM and an address = memory allocated has a value - 7.2, 3, 'd', true can be referred to as a variable (var1) OR a named constant (PI) OR a literal constant (4)

Identifiers - the names of data and functions syntax rules for making an identifier must start with a letter or underscore, and be followed by zero or more letters (A-Z, a-z), digits(0-9), or underscores case sensitive!! used for names of variables, named constants, or functions

Identifiers age_of_dog taxRateY2K VALID NOT VALID (Why?) PrintHeading ageOfHorse NOT VALID (Why?) age# 2000TaxRate Age-Of-Cat

Data types the type determines which values can be used and the operations you can perform types protect you from making logical mistakes - mixing types in ways that don't make sense - adding a string to a float "strongly typed" versus "weakly typed" C++ versus Visual Basic

Data types int, float, double - all numeric char - holds ONE character fractional part (float) versus no fractional part (int) exponential notation for floats / doubles overflow, underflow char - holds ONE character string - zero or more characters in an object bool - logical values (George Boole) - true, false

Declarations of data all identifiers must be DECLARED before use Syntax of variable declaration is “type identifier, identifier, ...;” Constants literal constants ('a', 4, 5.2, true, false, "John") don't need declarations, their names are their values named constants (PI, MAX) usually all caps Why use them? documentation, easy to edit later, prevents typing errors

What Does a Variable Declaration Do? int ageOfDog; float taxRate; char middleInitial; A declaration tells the compiler to allocate enough memory to hold a value of this data type and to associate the identifier with this location - its value can be changed many times during the program run 4 bytes for taxRate 1 byte for middleInitial

What is a Named Constant? A named constant is a location in memory that can be referred to by an identifier and in which a data value that cannot be changed is stored Valid constant declarations const string STARS = “****”; const float NORMAL_TEMP = 98.6; const char BLANK = ‘ ’; const int VOTING_AGE = 18; const float MAX_HOURS = 40.0;

Comments used to explain code to author and other readers /* */ (multi-line, "C style") // ("C++ style") USE them! There is a bad example on class page: Other Useful Links Don't comment out code unless you mean to

General program format comments at the top of the file ("header comment") - name, date, email, section, PURPOSE put only one statement per line use spaces, vertical and horizontal, to make it easier to read #include statement using namespace std; statement

Block(Compound Statement) A block is a sequence of zero or more statements enclosed by a pair of curly braces { } SYNTAX { Statement (optional) . . . } Braces should line up and statements inside should be indented Blocks are used for the body of a function, as well as grouping statements together

Executable statements (to start with) Output statements Assignment statements Return statement

Output statements cout "console output" insertion operator << name of the standard output stream always goes to the screen / console / monitor insertion operator << several can be chained in one statement outputting literals outputting variables and named constants endl - a manipulator (ends with "l" - ell, not 1)

Output Statements SYNTAX These examples yield the same output: cout << “The answer is “; cout << 3 * 4; cout << “The answer is “ << 3 * 4; cout << Expression << Expression . . .;

Return statement "return 0;" always at the end of the sequence of statements in the main function semantics: execution control turned back over to Operating System (Windows or Unix) means the program is over! return value 0 means "normal exit"

Assignment statement syntax "var = exp;" semantics - left side location gets the value of the expression on the right hand side expressions can use operators of appropriate types (arithmetic or string) expression must evaluate to ONE value this one value has a type either matches type of left hand side or doesn't type coercion or type casting happens

Examples: int x, y, z; x = 5; // expression 5 y = x * 2; // expression x * 2 z = x + y - 18; // expression x + y - 18 x = x + 2; // expression x + 2

Working with the IDE Build versus Rebuild versus Clean “build is up-to-date” warning - Watch out!! Errors and warnings Warning level W2 is what will be used to grade your programs

Syntax Errors The compiler may report lots of errors fix the FIRST one then recompile try to understand what the error was error messages are not clear keep a log of messages and what they mean do not ignore warning messages!

Logic (Semantic) Errors They are only found by testing Test plan more than one case each case tests different code input for each case expected output for the case worked out by hand

Runtime Errors something the program cannot control in its environment example: division by zero example: file not found usually crashes the program more advanced - read about 'exceptions'