View File - UET Taxila






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
int main(
name of function
says no parameters
)
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


VALID
age_of_dog
taxRateY2K
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






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
const char
NORMAL_TEMP = 98.6;
BLANK = ‘ ’;
const int
const float
VOTING_AGE = 18;
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
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
cout << Expression
<< Expression . . .;
These examples yield the same output:
cout << “The answer is “;
cout << 3 * 4;
cout << “The answer is “ << 3 * 4;
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'