Presentation is loading. Please wait.

Presentation is loading. Please wait.

Subject:Object oriented programming

Similar presentations


Presentation on theme: "Subject:Object oriented programming"— Presentation transcript:

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

2 Lecture 2 Overview of C++

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

4 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

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

6 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

7 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

8 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)

9 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

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

11 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

12 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

13 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

14 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

15 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;

16 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

17 General program format
comments at the top of the file ("header comment") - name, date, , 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

18 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

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

20 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)

21 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 ;

22 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"

23 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

24 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

25 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

26 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!

27 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

28 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'


Download ppt "Subject:Object oriented programming"

Similar presentations


Ads by Google